nokogumbo 0.9 → 0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +15 -10
- data/ext/nokogumboc/extconf.rb +34 -0
- data/ext/nokogumboc/nokogumbo.c +192 -0
- data/gumbo-parser/src/attribute.c +3 -3
- data/gumbo-parser/src/attribute.h +2 -2
- data/gumbo-parser/src/char_ref.c +8 -8
- data/gumbo-parser/src/char_ref.h +3 -3
- data/gumbo-parser/src/error.h +13 -13
- data/gumbo-parser/src/gumbo.h +21 -22
- data/gumbo-parser/src/insertion_mode.h +1 -1
- data/gumbo-parser/src/parser.c +1 -1
- data/gumbo-parser/src/parser.h +9 -9
- data/gumbo-parser/src/string_buffer.c +8 -8
- data/gumbo-parser/src/string_buffer.h +10 -11
- data/gumbo-parser/src/string_piece.c +2 -2
- data/gumbo-parser/src/string_piece.h +2 -2
- data/gumbo-parser/src/token_type.h +1 -1
- data/gumbo-parser/src/tokenizer.c +2 -2
- data/gumbo-parser/src/tokenizer.h +12 -12
- data/gumbo-parser/src/tokenizer_states.h +1 -1
- data/gumbo-parser/src/utf8.h +7 -7
- data/gumbo-parser/src/util.h +5 -4
- data/gumbo-parser/src/vector.c +11 -9
- data/gumbo-parser/src/vector.h +11 -8
- metadata +27 -31
- data/work/extconf.rb +0 -21
- data/work/nokogumbo.c +0 -100
@@ -22,7 +22,7 @@ extern "C" {
|
|
22
22
|
#endif
|
23
23
|
|
24
24
|
// http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#insertion-mode
|
25
|
-
typedef enum
|
25
|
+
typedef enum {
|
26
26
|
GUMBO_INSERTION_MODE_INITIAL,
|
27
27
|
GUMBO_INSERTION_MODE_BEFORE_HTML,
|
28
28
|
GUMBO_INSERTION_MODE_BEFORE_HEAD,
|
data/gumbo-parser/src/parser.c
CHANGED
@@ -340,7 +340,7 @@ typedef struct _TextNodeBufferState {
|
|
340
340
|
GumboNodeType _type;
|
341
341
|
} TextNodeBufferState;
|
342
342
|
|
343
|
-
typedef struct
|
343
|
+
typedef struct GumboInternalParserState {
|
344
344
|
// http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#insertion-mode
|
345
345
|
GumboInsertionMode _insertion_mode;
|
346
346
|
|
data/gumbo-parser/src/parser.h
CHANGED
@@ -24,30 +24,30 @@
|
|
24
24
|
extern "C" {
|
25
25
|
#endif
|
26
26
|
|
27
|
-
struct
|
28
|
-
struct
|
29
|
-
struct
|
30
|
-
struct
|
27
|
+
struct GumboInternalParserState;
|
28
|
+
struct GumboInternalOutput;
|
29
|
+
struct GumboInternalOptions;
|
30
|
+
struct GumboInternalTokenizerState;
|
31
31
|
|
32
32
|
// An overarching struct that's threaded through (nearly) all functions in the
|
33
33
|
// library, OOP-style. This gives each function access to the options and
|
34
34
|
// output, along with any internal state needed for the parse.
|
35
|
-
typedef struct
|
35
|
+
typedef struct GumboInternalParser {
|
36
36
|
// Settings for this parse run.
|
37
|
-
const struct
|
37
|
+
const struct GumboInternalOptions* _options;
|
38
38
|
|
39
39
|
// Output for the parse.
|
40
|
-
struct
|
40
|
+
struct GumboInternalOutput* _output;
|
41
41
|
|
42
42
|
// The internal tokenizer state, defined as a pointer to avoid a cyclic
|
43
43
|
// dependency on html5tokenizer.h. The main parse routine is responsible for
|
44
44
|
// initializing this on parse start, and destroying it on parse end.
|
45
45
|
// End-users will never see a non-garbage value in this pointer.
|
46
|
-
struct
|
46
|
+
struct GumboInternalTokenizerState* _tokenizer_state;
|
47
47
|
|
48
48
|
// The internal parser state. Initialized on parse start and destroyed on
|
49
49
|
// parse end; end-users will never see a non-garbage value in this pointer.
|
50
|
-
struct
|
50
|
+
struct GumboInternalParserState* _parser_state;
|
51
51
|
} GumboParser;
|
52
52
|
|
53
53
|
#ifdef __cplusplus
|
@@ -24,12 +24,12 @@
|
|
24
24
|
#include "string_piece.h"
|
25
25
|
#include "util.h"
|
26
26
|
|
27
|
-
struct
|
27
|
+
struct GumboInternalParser;
|
28
28
|
|
29
29
|
static const size_t kDefaultStringBufferSize = 10;
|
30
30
|
|
31
31
|
static void maybe_resize_string_buffer(
|
32
|
-
struct
|
32
|
+
struct GumboInternalParser* parser, size_t additional_chars,
|
33
33
|
GumboStringBuffer* buffer) {
|
34
34
|
size_t new_length = buffer->length + additional_chars;
|
35
35
|
size_t new_capacity = buffer->capacity;
|
@@ -46,20 +46,20 @@ static void maybe_resize_string_buffer(
|
|
46
46
|
}
|
47
47
|
|
48
48
|
void gumbo_string_buffer_init(
|
49
|
-
struct
|
49
|
+
struct GumboInternalParser* parser, GumboStringBuffer* output) {
|
50
50
|
output->data = gumbo_parser_allocate(parser, kDefaultStringBufferSize);
|
51
51
|
output->length = 0;
|
52
52
|
output->capacity = kDefaultStringBufferSize;
|
53
53
|
}
|
54
54
|
|
55
55
|
void gumbo_string_buffer_reserve(
|
56
|
-
struct
|
56
|
+
struct GumboInternalParser* parser, size_t min_capacity,
|
57
57
|
GumboStringBuffer* output) {
|
58
58
|
maybe_resize_string_buffer(parser, min_capacity - output->length, output);
|
59
59
|
}
|
60
60
|
|
61
61
|
void gumbo_string_buffer_append_codepoint(
|
62
|
-
struct
|
62
|
+
struct GumboInternalParser* parser, int c, GumboStringBuffer* output) {
|
63
63
|
// num_bytes is actually the number of continuation bytes, 1 less than the
|
64
64
|
// total number of bytes. This is done to keep the loop below simple and
|
65
65
|
// should probably change if we unroll it.
|
@@ -85,7 +85,7 @@ void gumbo_string_buffer_append_codepoint(
|
|
85
85
|
}
|
86
86
|
|
87
87
|
void gumbo_string_buffer_append_string(
|
88
|
-
struct
|
88
|
+
struct GumboInternalParser* parser, GumboStringPiece* str,
|
89
89
|
GumboStringBuffer* output) {
|
90
90
|
maybe_resize_string_buffer(parser, str->length, output);
|
91
91
|
memcpy(output->data + output->length, str->data, str->length);
|
@@ -93,7 +93,7 @@ void gumbo_string_buffer_append_string(
|
|
93
93
|
}
|
94
94
|
|
95
95
|
char* gumbo_string_buffer_to_string(
|
96
|
-
struct
|
96
|
+
struct GumboInternalParser* parser, GumboStringBuffer* input) {
|
97
97
|
char* buffer = gumbo_parser_allocate(parser, input->length + 1);
|
98
98
|
memcpy(buffer, input->data, input->length);
|
99
99
|
buffer[input->length] = '\0';
|
@@ -101,6 +101,6 @@ char* gumbo_string_buffer_to_string(
|
|
101
101
|
}
|
102
102
|
|
103
103
|
void gumbo_string_buffer_destroy(
|
104
|
-
struct
|
104
|
+
struct GumboInternalParser* parser, GumboStringBuffer* buffer) {
|
105
105
|
gumbo_parser_deallocate(parser, buffer->data);
|
106
106
|
}
|
@@ -20,21 +20,20 @@
|
|
20
20
|
#include <stdbool.h>
|
21
21
|
#include <stddef.h>
|
22
22
|
|
23
|
+
#include "gumbo.h"
|
24
|
+
|
23
25
|
#ifdef __cplusplus
|
24
26
|
extern "C" {
|
25
27
|
#endif
|
26
28
|
|
27
|
-
|
28
|
-
// header.
|
29
|
-
struct _GumboParser;
|
30
|
-
struct _GumboStringPiece;
|
29
|
+
struct GumboInternalParser;
|
31
30
|
|
32
31
|
// A struct representing a mutable, growable string. This consists of a
|
33
32
|
// heap-allocated buffer that may grow (by doubling) as necessary. When
|
34
33
|
// converting to a string, this allocates a new buffer that is only as long as
|
35
34
|
// it needs to be. Note that the internal buffer here is *not* nul-terminated,
|
36
35
|
// so be sure not to use ordinary string manipulation functions on it.
|
37
|
-
typedef struct
|
36
|
+
typedef struct {
|
38
37
|
// A pointer to the beginning of the string. NULL iff length == 0.
|
39
38
|
char* data;
|
40
39
|
|
@@ -47,33 +46,33 @@ typedef struct _GumboStringBuffer {
|
|
47
46
|
|
48
47
|
// Initializes a new GumboStringBuffer.
|
49
48
|
void gumbo_string_buffer_init(
|
50
|
-
struct
|
49
|
+
struct GumboInternalParser* parser, GumboStringBuffer* output);
|
51
50
|
|
52
51
|
// Ensures that the buffer contains at least a certain amount of space. Most
|
53
52
|
// useful with snprintf and the other length-delimited string functions, which
|
54
53
|
// may want to write directly into the buffer.
|
55
54
|
void gumbo_string_buffer_reserve(
|
56
|
-
struct
|
55
|
+
struct GumboInternalParser* parser, size_t min_capacity,
|
57
56
|
GumboStringBuffer* output);
|
58
57
|
|
59
58
|
// Appends a single Unicode codepoint onto the end of the GumboStringBuffer.
|
60
59
|
// This is essentially a UTF-8 encoder, and may add 1-4 bytes depending on the
|
61
60
|
// value of the codepoint.
|
62
61
|
void gumbo_string_buffer_append_codepoint(
|
63
|
-
struct
|
62
|
+
struct GumboInternalParser* parser, int c, GumboStringBuffer* output);
|
64
63
|
|
65
64
|
// Appends a string onto the end of the GumboStringBuffer.
|
66
65
|
void gumbo_string_buffer_append_string(
|
67
|
-
struct
|
66
|
+
struct GumboInternalParser* parser, GumboStringPiece* str,
|
68
67
|
GumboStringBuffer* output);
|
69
68
|
|
70
69
|
// Converts this string buffer to const char*, alloctaing a new buffer for it.
|
71
70
|
char* gumbo_string_buffer_to_string(
|
72
|
-
struct
|
71
|
+
struct GumboInternalParser* parser, GumboStringBuffer* input);
|
73
72
|
|
74
73
|
// Deallocates this GumboStringBuffer.
|
75
74
|
void gumbo_string_buffer_destroy(
|
76
|
-
struct
|
75
|
+
struct GumboInternalParser* parser, GumboStringBuffer* buffer);
|
77
76
|
|
78
77
|
#ifdef __cplusplus
|
79
78
|
}
|
@@ -23,7 +23,7 @@
|
|
23
23
|
|
24
24
|
#include "util.h"
|
25
25
|
|
26
|
-
struct
|
26
|
+
struct GumboInternalParser;
|
27
27
|
|
28
28
|
const GumboStringPiece kGumboEmptyString = { NULL, 0 };
|
29
29
|
|
@@ -40,7 +40,7 @@ bool gumbo_string_equals_ignore_case(
|
|
40
40
|
}
|
41
41
|
|
42
42
|
void gumbo_string_copy(
|
43
|
-
struct
|
43
|
+
struct GumboInternalParser* parser, GumboStringPiece* dest,
|
44
44
|
const GumboStringPiece* source) {
|
45
45
|
dest->length = source->length;
|
46
46
|
char* buffer = gumbo_parser_allocate(parser, source->length);
|
@@ -23,13 +23,13 @@
|
|
23
23
|
extern "C" {
|
24
24
|
#endif
|
25
25
|
|
26
|
-
struct
|
26
|
+
struct GumboInternalParser;
|
27
27
|
|
28
28
|
// Performs a deep-copy of an GumboStringPiece, allocating a fresh buffer in the
|
29
29
|
// destination and copying over the characters from source. Dest should be
|
30
30
|
// empty, with no buffer allocated; otherwise, this leaks it.
|
31
31
|
void gumbo_string_copy(
|
32
|
-
struct
|
32
|
+
struct GumboInternalParser* parser, GumboStringPiece* dest,
|
33
33
|
const GumboStringPiece* source);
|
34
34
|
|
35
35
|
#ifdef __cplusplus
|
@@ -75,7 +75,7 @@ typedef enum {
|
|
75
75
|
|
76
76
|
// This is a struct containing state necessary to build up a tag token,
|
77
77
|
// character by character.
|
78
|
-
typedef struct
|
78
|
+
typedef struct GumboInternalTagState {
|
79
79
|
// A buffer to accumulate characters for various GumboStringPiece fields.
|
80
80
|
GumboStringBuffer _buffer;
|
81
81
|
|
@@ -121,7 +121,7 @@ typedef struct _GumboTagState {
|
|
121
121
|
|
122
122
|
// This is the main tokenizer state struct, containing all state used by in
|
123
123
|
// tokenizing the input stream.
|
124
|
-
typedef struct
|
124
|
+
typedef struct GumboInternalTokenizerState {
|
125
125
|
// The current lexer state. Starts in GUMBO_LEX_DATA.
|
126
126
|
GumboTokenizerEnum _state;
|
127
127
|
|
@@ -31,10 +31,10 @@
|
|
31
31
|
extern "C" {
|
32
32
|
#endif
|
33
33
|
|
34
|
-
struct
|
34
|
+
struct GumboInternalParser;
|
35
35
|
|
36
36
|
// Struct containing all information pertaining to doctype tokens.
|
37
|
-
typedef struct
|
37
|
+
typedef struct GumboInternalTokenDocType {
|
38
38
|
const char* name;
|
39
39
|
const char* public_identifier;
|
40
40
|
const char* system_identifier;
|
@@ -47,7 +47,7 @@ typedef struct _GumboTokenDocType {
|
|
47
47
|
} GumboTokenDocType;
|
48
48
|
|
49
49
|
// Struct containing all information pertaining to start tag tokens.
|
50
|
-
typedef struct
|
50
|
+
typedef struct GumboInternalTokenStartTag {
|
51
51
|
GumboTag tag;
|
52
52
|
GumboVector /* GumboAttribute */ attributes;
|
53
53
|
bool is_self_closing;
|
@@ -56,7 +56,7 @@ typedef struct _GumboTokenStartTag {
|
|
56
56
|
// A data structure representing a single token in the input stream. This
|
57
57
|
// contains an enum for the type, the source position, a GumboStringPiece
|
58
58
|
// pointing to the original text, and then a union for any parsed data.
|
59
|
-
typedef struct
|
59
|
+
typedef struct GumboInternalToken {
|
60
60
|
GumboTokenType type;
|
61
61
|
GumboSourcePosition position;
|
62
62
|
GumboStringPiece original_text;
|
@@ -72,31 +72,31 @@ typedef struct _GumboToken {
|
|
72
72
|
// Initializes the tokenizer state within the GumboParser object, setting up a
|
73
73
|
// parse of the specified text.
|
74
74
|
void gumbo_tokenizer_state_init(
|
75
|
-
struct
|
75
|
+
struct GumboInternalParser* parser, const char* text, size_t text_length);
|
76
76
|
|
77
77
|
// Destroys the tokenizer state within the GumboParser object, freeing any
|
78
78
|
// dynamically-allocated structures within it.
|
79
|
-
void gumbo_tokenizer_state_destroy(struct
|
79
|
+
void gumbo_tokenizer_state_destroy(struct GumboInternalParser* parser);
|
80
80
|
|
81
81
|
// Sets the tokenizer state to the specified value. This is needed by some
|
82
82
|
// parser states, which alter the state of the tokenizer in response to tags
|
83
83
|
// seen.
|
84
84
|
void gumbo_tokenizer_set_state(
|
85
|
-
struct
|
85
|
+
struct GumboInternalParser* parser, GumboTokenizerEnum state);
|
86
86
|
|
87
87
|
// Flags whether the current node is a foreign content element. This is
|
88
88
|
// necessary for the markup declaration open state, where the tokenizer must be
|
89
89
|
// aware of the state of the parser to properly tokenize bad comment tags.
|
90
90
|
// http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#markup-declaration-open-state
|
91
91
|
void gumbo_tokenizer_set_is_current_node_foreign(
|
92
|
-
struct
|
92
|
+
struct GumboInternalParser* parser, bool is_foreign);
|
93
93
|
|
94
94
|
// Lexes a single token from the specified buffer, filling the output with the
|
95
95
|
// parsed GumboToken data structure. Returns true for a successful
|
96
96
|
// tokenization, false if a parse error occurs.
|
97
97
|
//
|
98
98
|
// Example:
|
99
|
-
// struct
|
99
|
+
// struct GumboInternalParser parser;
|
100
100
|
// GumboToken output;
|
101
101
|
// gumbo_tokenizer_state_init(&parser, text, strlen(text));
|
102
102
|
// while (gumbo_lex(&parser, &output)) {
|
@@ -104,17 +104,17 @@ void gumbo_tokenizer_set_is_current_node_foreign(
|
|
104
104
|
// gumbo_token_destroy(&parser, &token);
|
105
105
|
// }
|
106
106
|
// gumbo_tokenizer_state_destroy(&parser);
|
107
|
-
bool gumbo_lex(struct
|
107
|
+
bool gumbo_lex(struct GumboInternalParser* parser, GumboToken* output);
|
108
108
|
|
109
109
|
// Frees the internally-allocated pointers within an GumboToken. Note that this
|
110
110
|
// doesn't free the token itself, since oftentimes it will be allocated on the
|
111
|
-
// stack. A simple call to free() (or
|
111
|
+
// stack. A simple call to free() (or GumboParser->deallocator, if
|
112
112
|
// appropriate) can handle that.
|
113
113
|
//
|
114
114
|
// Note that if you are handing over ownership of the internal strings to some
|
115
115
|
// other data structure - for example, a parse tree - these do not need to be
|
116
116
|
// freed.
|
117
|
-
void gumbo_token_destroy(struct
|
117
|
+
void gumbo_token_destroy(struct GumboInternalParser* parser, GumboToken* token);
|
118
118
|
|
119
119
|
#ifdef __cplusplus
|
120
120
|
}
|
@@ -29,7 +29,7 @@
|
|
29
29
|
|
30
30
|
// The ordering of this enum is also used to build the dispatch table for the
|
31
31
|
// tokenizer state machine, so if it is changed, be sure to update that too.
|
32
|
-
typedef enum
|
32
|
+
typedef enum {
|
33
33
|
GUMBO_LEX_DATA,
|
34
34
|
GUMBO_LEX_CHAR_REF_IN_DATA,
|
35
35
|
GUMBO_LEX_RCDATA,
|
data/gumbo-parser/src/utf8.h
CHANGED
@@ -41,13 +41,13 @@
|
|
41
41
|
extern "C" {
|
42
42
|
#endif
|
43
43
|
|
44
|
-
struct
|
45
|
-
struct
|
44
|
+
struct GumboInternalError;
|
45
|
+
struct GumboInternalParser;
|
46
46
|
|
47
47
|
// Unicode replacement char.
|
48
48
|
extern const int kUtf8ReplacementChar;
|
49
49
|
|
50
|
-
typedef struct
|
50
|
+
typedef struct GumboInternalUtf8Iterator {
|
51
51
|
// Points at the start of the code point most recently read into 'current'.
|
52
52
|
const char* _start;
|
53
53
|
|
@@ -72,7 +72,7 @@ typedef struct _Utf8Iterator {
|
|
72
72
|
|
73
73
|
// Pointer back to the GumboParser instance, for configuration options and
|
74
74
|
// error recording.
|
75
|
-
struct
|
75
|
+
struct GumboInternalParser* _parser;
|
76
76
|
} Utf8Iterator;
|
77
77
|
|
78
78
|
// Returns true if this Unicode code point is in the list of characters
|
@@ -82,8 +82,8 @@ bool utf8_is_invalid_code_point(int c);
|
|
82
82
|
// Initializes a new Utf8Iterator from the given byte buffer. The source does
|
83
83
|
// not have to be NUL-terminated, but the length must be passed in explicitly.
|
84
84
|
void utf8iterator_init(
|
85
|
-
struct
|
86
|
-
Utf8Iterator* iter);
|
85
|
+
struct GumboInternalParser* parser, const char* source,
|
86
|
+
size_t source_length, Utf8Iterator* iter);
|
87
87
|
|
88
88
|
// Advances the current position by one code point.
|
89
89
|
void utf8iterator_next(Utf8Iterator* iter);
|
@@ -119,7 +119,7 @@ void utf8iterator_reset(Utf8Iterator* iter);
|
|
119
119
|
// Sets the position and original text fields of an error to the value at the
|
120
120
|
// mark.
|
121
121
|
void utf8iterator_fill_error_at_mark(
|
122
|
-
Utf8Iterator* iter, struct
|
122
|
+
Utf8Iterator* iter, struct GumboInternalError* error);
|
123
123
|
|
124
124
|
#ifdef __cplusplus
|
125
125
|
}
|
data/gumbo-parser/src/util.h
CHANGED
@@ -29,22 +29,23 @@ extern "C" {
|
|
29
29
|
|
30
30
|
// Forward declaration since it's passed into some of the functions in this
|
31
31
|
// header.
|
32
|
-
struct
|
32
|
+
struct GumboInternalParser;
|
33
33
|
|
34
34
|
// Utility function for allocating & copying a null-terminated string into a
|
35
35
|
// freshly-allocated buffer. This is necessary for proper memory management; we
|
36
36
|
// have the convention that all const char* in parse tree structures are
|
37
37
|
// freshly-allocated, so if we didn't copy, we'd try to delete a literal string
|
38
38
|
// when the parse tree is destroyed.
|
39
|
-
char* gumbo_copy_stringz(struct
|
39
|
+
char* gumbo_copy_stringz(struct GumboInternalParser* parser, const char* str);
|
40
40
|
|
41
41
|
// Allocate a chunk of memory, using the allocator specified in the Parser's
|
42
42
|
// config options.
|
43
|
-
void* gumbo_parser_allocate(
|
43
|
+
void* gumbo_parser_allocate(
|
44
|
+
struct GumboInternalParser* parser, size_t num_bytes);
|
44
45
|
|
45
46
|
// Deallocate a chunk of memory, using the deallocator specified in the Parser's
|
46
47
|
// config options.
|
47
|
-
void gumbo_parser_deallocate(struct
|
48
|
+
void gumbo_parser_deallocate(struct GumboInternalParser* parser, void* ptr);
|
48
49
|
|
49
50
|
// Debug wrapper for printf, to make it easier to turn off debugging info when
|
50
51
|
// required.
|
data/gumbo-parser/src/vector.c
CHANGED
@@ -23,12 +23,12 @@
|
|
23
23
|
|
24
24
|
#include "util.h"
|
25
25
|
|
26
|
-
struct
|
26
|
+
struct GumboInternalParser;
|
27
27
|
|
28
28
|
const GumboVector kGumboEmptyVector = { NULL, 0, 0 };
|
29
29
|
|
30
30
|
void gumbo_vector_init(
|
31
|
-
struct
|
31
|
+
struct GumboInternalParser* parser, size_t initial_capacity, GumboVector* vector) {
|
32
32
|
vector->length = 0;
|
33
33
|
vector->capacity = initial_capacity;
|
34
34
|
if (initial_capacity > 0) {
|
@@ -39,14 +39,14 @@ void gumbo_vector_init(
|
|
39
39
|
}
|
40
40
|
}
|
41
41
|
|
42
|
-
void gumbo_vector_destroy(struct
|
42
|
+
void gumbo_vector_destroy(struct GumboInternalParser* parser, GumboVector* vector) {
|
43
43
|
if (vector->capacity > 0) {
|
44
44
|
gumbo_parser_deallocate(parser, vector->data);
|
45
45
|
}
|
46
46
|
}
|
47
47
|
|
48
48
|
static void enlarge_vector_if_full(
|
49
|
-
struct
|
49
|
+
struct GumboInternalParser* parser, GumboVector* vector) {
|
50
50
|
if (vector->length >= vector->capacity) {
|
51
51
|
if (vector->capacity) {
|
52
52
|
size_t old_num_bytes = sizeof(void*) * vector->capacity;
|
@@ -66,14 +66,15 @@ static void enlarge_vector_if_full(
|
|
66
66
|
}
|
67
67
|
|
68
68
|
void gumbo_vector_add(
|
69
|
-
struct
|
69
|
+
struct GumboInternalParser* parser, void* element, GumboVector* vector) {
|
70
70
|
enlarge_vector_if_full(parser, vector);
|
71
71
|
assert(vector->data);
|
72
72
|
assert(vector->length < vector->capacity);
|
73
73
|
vector->data[vector->length++] = element;
|
74
74
|
}
|
75
75
|
|
76
|
-
void* gumbo_vector_pop(
|
76
|
+
void* gumbo_vector_pop(
|
77
|
+
struct GumboInternalParser* parser, GumboVector* vector) {
|
77
78
|
if (vector->length == 0) {
|
78
79
|
return NULL;
|
79
80
|
}
|
@@ -90,7 +91,8 @@ int gumbo_vector_index_of(GumboVector* vector, void* element) {
|
|
90
91
|
}
|
91
92
|
|
92
93
|
void gumbo_vector_insert_at(
|
93
|
-
struct
|
94
|
+
struct GumboInternalParser* parser, void* element, int index,
|
95
|
+
GumboVector* vector) {
|
94
96
|
assert(index >= 0);
|
95
97
|
assert(index <= vector->length);
|
96
98
|
enlarge_vector_if_full(parser, vector);
|
@@ -101,7 +103,7 @@ void gumbo_vector_insert_at(
|
|
101
103
|
}
|
102
104
|
|
103
105
|
void gumbo_vector_remove(
|
104
|
-
struct
|
106
|
+
struct GumboInternalParser* parser, void* node, GumboVector* vector) {
|
105
107
|
int index = gumbo_vector_index_of(vector, node);
|
106
108
|
if (index == -1) {
|
107
109
|
return;
|
@@ -110,7 +112,7 @@ void gumbo_vector_remove(
|
|
110
112
|
}
|
111
113
|
|
112
114
|
void* gumbo_vector_remove_at(
|
113
|
-
struct
|
115
|
+
struct GumboInternalParser* parser, int index, GumboVector* vector) {
|
114
116
|
assert(index >= 0);
|
115
117
|
assert(index < vector->length);
|
116
118
|
void* result = vector->data[index];
|
data/gumbo-parser/src/vector.h
CHANGED
@@ -25,39 +25,42 @@ extern "C" {
|
|
25
25
|
|
26
26
|
// Forward declaration since it's passed into some of the functions in this
|
27
27
|
// header.
|
28
|
-
struct
|
28
|
+
struct GumboInternalParser;
|
29
29
|
|
30
30
|
// Initializes a new GumboVector with the specified initial capacity.
|
31
31
|
void gumbo_vector_init(
|
32
|
-
struct
|
32
|
+
struct GumboInternalParser* parser, size_t initial_capacity,
|
33
|
+
GumboVector* vector);
|
33
34
|
|
34
35
|
// Frees the memory used by an GumboVector. Does not free the contained
|
35
36
|
// pointers.
|
36
|
-
void gumbo_vector_destroy(
|
37
|
+
void gumbo_vector_destroy(
|
38
|
+
struct GumboInternalParser* parser, GumboVector* vector);
|
37
39
|
|
38
40
|
// Adds a new element to an GumboVector.
|
39
41
|
void gumbo_vector_add(
|
40
|
-
struct
|
42
|
+
struct GumboInternalParser* parser, void* element, GumboVector* vector);
|
41
43
|
|
42
44
|
// Removes and returns the element most recently added to the GumboVector.
|
43
45
|
// Ownership is transferred to caller. Capacity is unchanged. If the vector is
|
44
46
|
// empty, NULL is returned.
|
45
|
-
void* gumbo_vector_pop(struct
|
47
|
+
void* gumbo_vector_pop(struct GumboInternalParser* parser, GumboVector* vector);
|
46
48
|
|
47
49
|
// Inserts an element at a specific index. This is potentially O(N) time, but
|
48
50
|
// is necessary for some of the spec's behavior.
|
49
51
|
void gumbo_vector_insert_at(
|
50
|
-
struct
|
52
|
+
struct GumboInternalParser* parser, void* element, int index,
|
53
|
+
GumboVector* vector);
|
51
54
|
|
52
55
|
// Removes an element from the vector, or does nothing if the element is not in
|
53
56
|
// the vector.
|
54
57
|
void gumbo_vector_remove(
|
55
|
-
struct
|
58
|
+
struct GumboInternalParser* parser, void* element, GumboVector* vector);
|
56
59
|
|
57
60
|
// Removes and returns an element at a specific index. Note that this is
|
58
61
|
// potentially O(N) time and should be used sparingly.
|
59
62
|
void* gumbo_vector_remove_at(
|
60
|
-
struct
|
63
|
+
struct GumboInternalParser* parser, int index, GumboVector* vector);
|
61
64
|
|
62
65
|
#ifdef __cplusplus
|
63
66
|
}
|