Coverage Report

Created: 2026-11-12 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nokogiri/gumbo-parser/src/token_buffer.c
Line
Count
Source
1
/*
2
 Copyright 2018 Stephen Checkoway
3
4
 Licensed under the Apache License, Version 2.0 (the "License");
5
 you may not use this file except in compliance with the License.
6
 You may obtain a copy of the License at
7
8
    https://www.apache.org/licenses/LICENSE-2.0
9
10
 Unless required by applicable law or agreed to in writing, software
11
 distributed under the License is distributed on an "AS IS" BASIS,
12
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 See the License for the specific language governing permissions and
14
 limitations under the License.
15
*/
16
17
#include <assert.h>
18
19
#include "ascii.h"
20
#include "token_buffer.h"
21
#include "tokenizer.h"
22
#include "util.h"
23
24
struct GumboInternalCharacterToken {
25
  GumboSourcePosition position;
26
  GumboStringPiece original_text;
27
  int c;
28
};
29
30
54.9k
void gumbo_character_token_buffer_init(GumboCharacterTokenBuffer* buffer) {
31
54.9k
  buffer->data = NULL;
32
54.9k
  buffer->length = 0;
33
54.9k
  buffer->capacity = 0;
34
54.9k
}
35
36
void gumbo_character_token_buffer_append (
37
  const GumboToken* token,
38
  GumboCharacterTokenBuffer* buffer
39
458k
) {
40
458k
  assert(token->type == GUMBO_TOKEN_WHITESPACE
41
458k
         || token->type == GUMBO_TOKEN_CHARACTER);
42
458k
  if (buffer->length == buffer->capacity) {
43
3.78k
    if (buffer->capacity == 0)
44
1.94k
      buffer->capacity = 10;
45
1.84k
    else
46
1.84k
      buffer->capacity *= 2;
47
3.78k
    size_t bytes = sizeof(*buffer->data) * buffer->capacity;
48
3.78k
    buffer->data = gumbo_realloc(buffer->data, bytes);
49
3.78k
  }
50
458k
  size_t index = buffer->length++;
51
458k
  buffer->data[index].position = token->position;
52
458k
  buffer->data[index].original_text = token->original_text;
53
458k
  buffer->data[index].c = token->v.character;
54
458k
}
55
56
void gumbo_character_token_buffer_get (
57
  const GumboCharacterTokenBuffer* buffer,
58
  size_t index,
59
  struct GumboInternalToken* output
60
457k
) {
61
457k
  assert(index < buffer->length);
62
457k
  int c = buffer->data[index].c;
63
457k
  output->type = gumbo_ascii_isspace(c)?
64
453k
    GUMBO_TOKEN_WHITESPACE : GUMBO_TOKEN_CHARACTER;
65
457k
  output->position = buffer->data[index].position;
66
457k
  output->original_text = buffer->data[index].original_text;
67
457k
  output->v.character = c;
68
457k
}
69
70
16.0k
void gumbo_character_token_buffer_clear(GumboCharacterTokenBuffer* buffer) {
71
16.0k
  buffer->length = 0;
72
16.0k
}
73
74
54.9k
void gumbo_character_token_buffer_destroy(GumboCharacterTokenBuffer* buffer) {
75
54.9k
  gumbo_free(buffer->data);
76
  buffer->data = NULL;
77
54.9k
  buffer->length = 0;
78
54.9k
  buffer->capacity = 0;
79
54.9k
}