nokogiri 1.11.7 → 1.12.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +243 -22
  3. data/LICENSE.md +1 -1
  4. data/README.md +6 -5
  5. data/ext/nokogiri/depend +35 -34
  6. data/ext/nokogiri/extconf.rb +181 -103
  7. data/ext/nokogiri/gumbo.c +611 -0
  8. data/ext/nokogiri/{html_document.c → html4_document.c} +8 -8
  9. data/ext/nokogiri/{html_element_description.c → html4_element_description.c} +20 -18
  10. data/ext/nokogiri/{html_entity_lookup.c → html4_entity_lookup.c} +7 -7
  11. data/ext/nokogiri/{html_sax_parser_context.c → html4_sax_parser_context.c} +5 -5
  12. data/ext/nokogiri/{html_sax_push_parser.c → html4_sax_push_parser.c} +4 -4
  13. data/ext/nokogiri/libxml2_backwards_compat.c +30 -30
  14. data/ext/nokogiri/nokogiri.c +51 -38
  15. data/ext/nokogiri/nokogiri.h +16 -9
  16. data/ext/nokogiri/xml_document.c +13 -13
  17. data/ext/nokogiri/xml_element_content.c +2 -0
  18. data/ext/nokogiri/xml_encoding_handler.c +11 -6
  19. data/ext/nokogiri/xml_namespace.c +2 -0
  20. data/ext/nokogiri/xml_node.c +102 -102
  21. data/ext/nokogiri/xml_node_set.c +20 -20
  22. data/ext/nokogiri/xml_reader.c +2 -0
  23. data/ext/nokogiri/xml_sax_parser.c +6 -6
  24. data/ext/nokogiri/xml_sax_parser_context.c +2 -0
  25. data/ext/nokogiri/xml_schema.c +2 -0
  26. data/ext/nokogiri/xml_xpath_context.c +67 -65
  27. data/ext/nokogiri/xslt_stylesheet.c +2 -1
  28. data/gumbo-parser/CHANGES.md +63 -0
  29. data/gumbo-parser/Makefile +101 -0
  30. data/gumbo-parser/THANKS +27 -0
  31. data/gumbo-parser/src/Makefile +17 -0
  32. data/gumbo-parser/src/README.md +41 -0
  33. data/gumbo-parser/src/ascii.c +75 -0
  34. data/gumbo-parser/src/ascii.h +115 -0
  35. data/gumbo-parser/src/attribute.c +42 -0
  36. data/gumbo-parser/src/attribute.h +17 -0
  37. data/gumbo-parser/src/char_ref.c +22225 -0
  38. data/gumbo-parser/src/char_ref.h +29 -0
  39. data/gumbo-parser/src/char_ref.rl +2154 -0
  40. data/gumbo-parser/src/error.c +626 -0
  41. data/gumbo-parser/src/error.h +148 -0
  42. data/gumbo-parser/src/foreign_attrs.c +104 -0
  43. data/gumbo-parser/src/foreign_attrs.gperf +27 -0
  44. data/gumbo-parser/src/gumbo.h +943 -0
  45. data/gumbo-parser/src/insertion_mode.h +33 -0
  46. data/gumbo-parser/src/macros.h +91 -0
  47. data/gumbo-parser/src/parser.c +4886 -0
  48. data/gumbo-parser/src/parser.h +41 -0
  49. data/gumbo-parser/src/replacement.h +33 -0
  50. data/gumbo-parser/src/string_buffer.c +103 -0
  51. data/gumbo-parser/src/string_buffer.h +68 -0
  52. data/gumbo-parser/src/string_piece.c +48 -0
  53. data/gumbo-parser/src/svg_attrs.c +174 -0
  54. data/gumbo-parser/src/svg_attrs.gperf +77 -0
  55. data/gumbo-parser/src/svg_tags.c +137 -0
  56. data/gumbo-parser/src/svg_tags.gperf +55 -0
  57. data/gumbo-parser/src/tag.c +222 -0
  58. data/gumbo-parser/src/tag_lookup.c +382 -0
  59. data/gumbo-parser/src/tag_lookup.gperf +169 -0
  60. data/gumbo-parser/src/tag_lookup.h +13 -0
  61. data/gumbo-parser/src/token_buffer.c +79 -0
  62. data/gumbo-parser/src/token_buffer.h +71 -0
  63. data/gumbo-parser/src/token_type.h +17 -0
  64. data/gumbo-parser/src/tokenizer.c +3463 -0
  65. data/gumbo-parser/src/tokenizer.h +112 -0
  66. data/gumbo-parser/src/tokenizer_states.h +339 -0
  67. data/gumbo-parser/src/utf8.c +245 -0
  68. data/gumbo-parser/src/utf8.h +164 -0
  69. data/gumbo-parser/src/util.c +68 -0
  70. data/gumbo-parser/src/util.h +30 -0
  71. data/gumbo-parser/src/vector.c +111 -0
  72. data/gumbo-parser/src/vector.h +45 -0
  73. data/lib/nokogiri.rb +31 -29
  74. data/lib/nokogiri/css.rb +14 -14
  75. data/lib/nokogiri/css/parser.rb +1 -1
  76. data/lib/nokogiri/css/parser.y +1 -1
  77. data/lib/nokogiri/css/syntax_error.rb +1 -1
  78. data/lib/nokogiri/extension.rb +2 -2
  79. data/lib/nokogiri/gumbo.rb +14 -0
  80. data/lib/nokogiri/html.rb +31 -27
  81. data/lib/nokogiri/html4.rb +40 -0
  82. data/lib/nokogiri/{html → html4}/builder.rb +2 -2
  83. data/lib/nokogiri/{html → html4}/document.rb +4 -4
  84. data/lib/nokogiri/{html → html4}/document_fragment.rb +3 -3
  85. data/lib/nokogiri/{html → html4}/element_description.rb +1 -1
  86. data/lib/nokogiri/{html → html4}/element_description_defaults.rb +1 -1
  87. data/lib/nokogiri/{html → html4}/entity_lookup.rb +1 -1
  88. data/lib/nokogiri/{html → html4}/sax/parser.rb +11 -14
  89. data/lib/nokogiri/html4/sax/parser_context.rb +19 -0
  90. data/lib/nokogiri/{html → html4}/sax/push_parser.rb +5 -5
  91. data/lib/nokogiri/html5.rb +473 -0
  92. data/lib/nokogiri/html5/document.rb +74 -0
  93. data/lib/nokogiri/html5/document_fragment.rb +80 -0
  94. data/lib/nokogiri/html5/node.rb +93 -0
  95. data/lib/nokogiri/version/constant.rb +1 -1
  96. data/lib/nokogiri/version/info.rb +11 -2
  97. data/lib/nokogiri/xml.rb +35 -36
  98. data/lib/nokogiri/xml/node.rb +6 -5
  99. data/lib/nokogiri/xml/parse_options.rb +2 -0
  100. data/lib/nokogiri/xml/pp.rb +2 -2
  101. data/lib/nokogiri/xml/sax.rb +4 -4
  102. data/lib/nokogiri/xml/sax/document.rb +24 -30
  103. data/lib/nokogiri/xml/xpath.rb +2 -2
  104. data/lib/nokogiri/xslt.rb +16 -16
  105. data/lib/nokogiri/xslt/stylesheet.rb +1 -1
  106. metadata +102 -60
  107. data/lib/nokogiri/html/sax/parser_context.rb +0 -17
@@ -0,0 +1,245 @@
1
+ /*
2
+ Copyright 2018 Craig Barnes.
3
+ Copyright 2010 Google Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ https://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ #include "utf8.h"
19
+
20
+ #include <assert.h>
21
+ #include <stdint.h>
22
+ #include <string.h>
23
+
24
+ #include "error.h"
25
+ #include "gumbo.h"
26
+ #include "parser.h"
27
+ #include "ascii.h"
28
+ #include "vector.h"
29
+
30
+ // References:
31
+ // * https://tools.ietf.org/html/rfc3629
32
+ // * https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
33
+
34
+ // The following code is a DFA-based UTF-8 decoder by Bjoern Hoehrmann.
35
+ // We wrap the inner table-based decoder routine in our own handling for
36
+ // newlines, tabs, invalid continuation bytes, and other conditions that
37
+ // the HTML5 spec fully specifies but normal UTF-8 decoders do not handle.
38
+ // See https://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
39
+
40
+ // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
41
+ //
42
+ // Permission is hereby granted, free of charge, to any person obtaining a
43
+ // copy of this software and associated documentation files (the "Software"),
44
+ // to deal in the Software without restriction, including without limitation
45
+ // the rights to use, copy, modify, merge, publish, distribute, sublicense,
46
+ // and/or sell copies of the Software, and to permit persons to whom the
47
+ // Software is furnished to do so, subject to the following conditions:
48
+ //
49
+ // The above copyright notice and this permission notice shall be included in
50
+ // all copies or substantial portions of the Software.
51
+
52
+ #define UTF8_ACCEPT 0
53
+ #define UTF8_REJECT 12
54
+
55
+ static const uint8_t utf8d[] = {
56
+ // The first part of the table maps bytes to character classes that
57
+ // to reduce the size of the transition table and create bitmasks.
58
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
60
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
61
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
62
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
63
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
64
+ 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
65
+ 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
66
+
67
+ // The second part is a transition table that maps a combination
68
+ // of a state of the automaton and a character class to a state.
69
+ 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
70
+ 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
71
+ 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
72
+ 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
73
+ 12,36,12,12,12,12,12,12,12,12,12,12,
74
+ };
75
+
76
+ static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
77
+ uint32_t type = utf8d[byte];
78
+
79
+ *codep =
80
+ (*state != UTF8_ACCEPT)
81
+ ? (byte & 0x3fu) | (*codep << 6)
82
+ : (0xff >> type) & (byte);
83
+
84
+ *state = utf8d[256 + *state + type];
85
+ return *state;
86
+ }
87
+
88
+ // END COPIED CODE.
89
+
90
+ // Adds a decoding error to the parser's error list, based on the current state
91
+ // of the Utf8Iterator.
92
+ static void add_error(Utf8Iterator* iter, GumboErrorType type) {
93
+ GumboParser* parser = iter->_parser;
94
+
95
+ GumboError* error = gumbo_add_error(parser);
96
+ if (!error) {
97
+ return;
98
+ }
99
+ error->type = type;
100
+ error->position = iter->_pos;
101
+ error->original_text.data = iter->_start;
102
+ error->original_text.length = iter->_width;
103
+ error->v.tokenizer.codepoint = iter->_current;
104
+ }
105
+
106
+ // Reads the next UTF-8 character in the iter.
107
+ // This assumes that iter->_start points to the beginning of the character.
108
+ // When this method returns, iter->_width and iter->_current will be set
109
+ // appropriately, as well as any error flags.
110
+ static void read_char(Utf8Iterator* iter) {
111
+ if (iter->_start >= iter->_end) {
112
+ // No input left to consume; emit an EOF and set width = 0.
113
+ iter->_current = -1;
114
+ iter->_width = 0;
115
+ return;
116
+ }
117
+
118
+ uint32_t code_point = 0;
119
+ uint32_t state = UTF8_ACCEPT;
120
+ for (const char* c = iter->_start; c < iter->_end; ++c) {
121
+ decode(&state, &code_point, (uint32_t)(unsigned char) (*c));
122
+ if (state == UTF8_ACCEPT) {
123
+ iter->_width = c - iter->_start + 1;
124
+ // This is the special handling for carriage returns that is mandated by
125
+ // the HTML5 spec. Since we're looking for particular 7-bit literal
126
+ // characters, we operate in terms of chars and only need a check for iter
127
+ // overrun, instead of having to read in a full next code point.
128
+ // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
129
+ if (code_point == '\r') {
130
+ assert(iter->_width == 1);
131
+ const char* next = c + 1;
132
+ if (next < iter->_end && *next == '\n') {
133
+ // Advance the iter, as if the carriage return didn't exist.
134
+ ++iter->_start;
135
+ // Preserve the true offset, since other tools that look at it may be
136
+ // unaware of HTML5's rules for converting \r into \n.
137
+ ++iter->_pos.offset;
138
+ }
139
+ code_point = '\n';
140
+ }
141
+ iter->_current = code_point;
142
+ if (utf8_is_surrogate(code_point)) {
143
+ add_error(iter, GUMBO_ERR_SURROGATE_IN_INPUT_STREAM);
144
+ } else if (utf8_is_noncharacter(code_point)) {
145
+ add_error(iter, GUMBO_ERR_NONCHARACTER_IN_INPUT_STREAM);
146
+ } else if (utf8_is_control(code_point)
147
+ && !(gumbo_ascii_isspace(code_point) || code_point == 0)) {
148
+ add_error(iter, GUMBO_ERR_CONTROL_CHARACTER_IN_INPUT_STREAM);
149
+ }
150
+ return;
151
+ } else if (state == UTF8_REJECT) {
152
+ // We don't want to consume the invalid continuation byte of a multi-byte
153
+ // run, but we do want to skip past an invalid first byte.
154
+ iter->_width = c - iter->_start + (c == iter->_start);
155
+ iter->_current = kUtf8ReplacementChar;
156
+ add_error(iter, GUMBO_ERR_UTF8_INVALID);
157
+ return;
158
+ }
159
+ }
160
+ // If we got here without exiting early, then we've reached the end of the
161
+ // iterator. Add an error for truncated input, set the width to consume the
162
+ // rest of the iterator, and emit a replacement character. The next time we
163
+ // enter this method, it will detect that there's no input to consume and
164
+ // output an EOF.
165
+ iter->_width = iter->_end - iter->_start;
166
+ iter->_current = kUtf8ReplacementChar;
167
+ add_error(iter, GUMBO_ERR_UTF8_TRUNCATED);
168
+ }
169
+
170
+ static void update_position(Utf8Iterator* iter) {
171
+ iter->_pos.offset += iter->_width;
172
+ if (iter->_current == '\n') {
173
+ ++iter->_pos.line;
174
+ iter->_pos.column = 1;
175
+ } else if (iter->_current == '\t') {
176
+ int tab_stop = iter->_parser->_options->tab_stop;
177
+ iter->_pos.column = ((iter->_pos.column / tab_stop) + 1) * tab_stop;
178
+ } else if (iter->_current != -1) {
179
+ ++iter->_pos.column;
180
+ }
181
+ }
182
+
183
+ void utf8iterator_init (
184
+ GumboParser* parser,
185
+ const char* source,
186
+ size_t source_length,
187
+ Utf8Iterator* iter
188
+ ) {
189
+ iter->_start = source;
190
+ iter->_end = source + source_length;
191
+ iter->_pos.line = 1;
192
+ iter->_pos.column = 1;
193
+ iter->_pos.offset = 0;
194
+ iter->_parser = parser;
195
+ read_char(iter);
196
+ if (iter->_current == kUtf8BomChar) {
197
+ iter->_start += iter->_width;
198
+ iter->_pos.offset += iter->_width;
199
+ read_char(iter);
200
+ }
201
+ }
202
+
203
+ void utf8iterator_next(Utf8Iterator* iter) {
204
+ // We update positions based on the *last* character read, so that the first
205
+ // character following a newline is at column 1 in the next line.
206
+ update_position(iter);
207
+ iter->_start += iter->_width;
208
+ read_char(iter);
209
+ }
210
+
211
+ bool utf8iterator_maybe_consume_match (
212
+ Utf8Iterator* iter,
213
+ const char* prefix,
214
+ size_t length,
215
+ bool case_sensitive
216
+ ) {
217
+ bool matched =
218
+ (iter->_start + length <= iter->_end)
219
+ && (
220
+ case_sensitive
221
+ ? !strncmp(iter->_start, prefix, length)
222
+ : !gumbo_ascii_strncasecmp(iter->_start, prefix, length)
223
+ )
224
+ ;
225
+ if (matched) {
226
+ for (size_t i = 0; i < length; ++i) {
227
+ utf8iterator_next(iter);
228
+ }
229
+ return true;
230
+ } else {
231
+ return false;
232
+ }
233
+ }
234
+
235
+ void utf8iterator_mark(Utf8Iterator* iter) {
236
+ iter->_mark = iter->_start;
237
+ iter->_mark_pos = iter->_pos;
238
+ }
239
+
240
+ // Returns the current input stream position to the mark.
241
+ void utf8iterator_reset(Utf8Iterator* iter) {
242
+ iter->_start = iter->_mark;
243
+ iter->_pos = iter->_mark_pos;
244
+ read_char(iter);
245
+ }
@@ -0,0 +1,164 @@
1
+ #ifndef GUMBO_UTF8_H_
2
+ #define GUMBO_UTF8_H_
3
+
4
+ // This contains an implementation of a UTF-8 iterator and decoder suitable for
5
+ // a HTML5 parser. This does a bit more than straight UTF-8 decoding. The
6
+ // HTML5 spec specifies that:
7
+ // 1. Decoding errors are parse errors.
8
+ // 2. Certain other codepoints (e.g. control characters) are parse errors.
9
+ // 3. Carriage returns and CR/LF groups are converted to line feeds.
10
+ // https://encoding.spec.whatwg.org/#utf-8-decode
11
+ //
12
+ // Also, we want to keep track of source positions for error handling. As a
13
+ // result, we fold all that functionality into this decoder, and can't use an
14
+ // off-the-shelf library.
15
+ //
16
+ // This header is internal-only, which is why we prefix functions with only
17
+ // utf8_ or utf8_iterator_ instead of gumbo_utf8_.
18
+
19
+ #include <stdbool.h>
20
+ #include <stddef.h>
21
+
22
+ #include "gumbo.h"
23
+ #include "macros.h"
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ struct GumboInternalError;
30
+ struct GumboInternalParser;
31
+
32
+ // Unicode replacement char.
33
+ #define kUtf8ReplacementChar 0xFFFD
34
+ #define kUtf8BomChar 0xFEFF
35
+ #define kUtf8MaxChar 0x10FFFF
36
+
37
+ typedef struct GumboInternalUtf8Iterator {
38
+ // Points at the start of the code point most recently read into 'current'.
39
+ const char* _start;
40
+
41
+ // Points at the mark. The mark is initially set to the beginning of the
42
+ // input.
43
+ const char* _mark;
44
+
45
+ // Points past the end of the iter, like a past-the-end iterator in the STL.
46
+ const char* _end;
47
+
48
+ // The code point under the cursor.
49
+ int _current;
50
+
51
+ // The width in bytes of the current code point.
52
+ size_t _width;
53
+
54
+ // The SourcePosition for the current location.
55
+ GumboSourcePosition _pos;
56
+
57
+ // The SourcePosition for the mark.
58
+ GumboSourcePosition _mark_pos;
59
+
60
+ // Pointer back to the GumboParser instance, for configuration options and
61
+ // error recording.
62
+ struct GumboInternalParser* _parser;
63
+ } Utf8Iterator;
64
+
65
+ // Returns true if this Unicode code point is a surrogate.
66
+ CONST_FN static inline bool utf8_is_surrogate(int c) {
67
+ return c >= 0xD800 && c <= 0xDFFF;
68
+ }
69
+
70
+ // Returns true if this Unicode code point is a noncharacter.
71
+ CONST_FN static inline bool utf8_is_noncharacter(int c) {
72
+ return
73
+ (c >= 0xFDD0 && c <= 0xFDEF)
74
+ || ((c & 0xFFFF) == 0xFFFE)
75
+ || ((c & 0xFFFF) == 0xFFFF);
76
+ }
77
+
78
+ // Returns true if this Unicode code point is a control.
79
+ CONST_FN static inline bool utf8_is_control(int c) {
80
+ return ((unsigned int)c < 0x1Fu) || (c >= 0x7F && c <= 0x9F);
81
+ }
82
+
83
+ // Initializes a new Utf8Iterator from the given byte buffer. The source does
84
+ // not have to be NUL-terminated, but the length must be passed in explicitly.
85
+ void utf8iterator_init (
86
+ struct GumboInternalParser* parser,
87
+ const char* source,
88
+ size_t source_length,
89
+ Utf8Iterator* iter
90
+ );
91
+
92
+ // Advances the current position by one code point.
93
+ void utf8iterator_next(Utf8Iterator* iter);
94
+
95
+ // Returns the current code point as an integer.
96
+ static inline int utf8iterator_current(const Utf8Iterator* iter) {
97
+ return iter->_current;
98
+ }
99
+
100
+ // Retrieves and fills the output parameter with the current source position.
101
+ static inline void utf8iterator_get_position (
102
+ const Utf8Iterator* iter,
103
+ GumboSourcePosition* output
104
+ ) {
105
+ *output = iter->_pos;
106
+ }
107
+
108
+ // Retrieves the marked position.
109
+ static inline GumboSourcePosition utf8iterator_get_mark_position (
110
+ const Utf8Iterator* iter
111
+ ) {
112
+ return iter->_mark_pos;
113
+ }
114
+
115
+ // Retrieves a character pointer to the start of the current character.
116
+ static inline const char* utf8iterator_get_char_pointer(const Utf8Iterator* iter) {
117
+ return iter->_start;
118
+ }
119
+
120
+ // Retrieves the width of the current character.
121
+ static inline size_t utf8iterator_get_width(const Utf8Iterator* iter) {
122
+ return iter->_width;
123
+ }
124
+
125
+ // Retrieves a character pointer to 1 past the end of the buffer. This is
126
+ // necessary for certain state machines and string comparisons that would like
127
+ // to look directly for ASCII text in the buffer without going through the
128
+ // decoder.
129
+ static inline const char* utf8iterator_get_end_pointer(const Utf8Iterator* iter) {
130
+ return iter->_end;
131
+ }
132
+
133
+ // Retrieves a character pointer to the marked position.
134
+ static inline const char* utf8iterator_get_mark_pointer(const Utf8Iterator* iter) {
135
+ return iter->_mark;
136
+ }
137
+
138
+ // If the upcoming text in the buffer matches the specified prefix (which has
139
+ // length 'length'), consume it and return true. Otherwise, return false with
140
+ // no other effects. If the length of the string would overflow the buffer,
141
+ // this returns false. Note that prefix should not contain null bytes because
142
+ // of the use of strncmp/strncasecmp internally. All existing use-cases adhere
143
+ // to this.
144
+ bool utf8iterator_maybe_consume_match (
145
+ Utf8Iterator* iter,
146
+ const char* prefix,
147
+ size_t length,
148
+ bool case_sensitive
149
+ );
150
+
151
+ // "Marks" a particular location of interest in the input stream, so that it can
152
+ // later be reset() to. There's also the ability to record an error at the
153
+ // point that was marked, as oftentimes that's more useful than the last
154
+ // character before the error was detected.
155
+ void utf8iterator_mark(Utf8Iterator* iter);
156
+
157
+ // Returns the current input stream position to the mark.
158
+ void utf8iterator_reset(Utf8Iterator* iter);
159
+
160
+ #ifdef __cplusplus
161
+ }
162
+ #endif
163
+
164
+ #endif // GUMBO_UTF8_H_
@@ -0,0 +1,68 @@
1
+ /*
2
+ Copyright 2017-2018 Craig Barnes.
3
+ Copyright 2010 Google Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ https://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ #include <stdio.h>
19
+ #include <stdlib.h>
20
+ #include <string.h>
21
+ #include "util.h"
22
+ #include "gumbo.h"
23
+
24
+ void* gumbo_alloc(size_t size) {
25
+ void* ptr = malloc(size);
26
+ if (unlikely(ptr == NULL)) {
27
+ perror(__func__);
28
+ abort();
29
+ }
30
+ return ptr;
31
+ }
32
+
33
+ void* gumbo_realloc(void* ptr, size_t size) {
34
+ ptr = realloc(ptr, size);
35
+ if (unlikely(ptr == NULL)) {
36
+ perror(__func__);
37
+ abort();
38
+ }
39
+ return ptr;
40
+ }
41
+
42
+ void gumbo_free(void* ptr) {
43
+ free(ptr);
44
+ }
45
+
46
+ char* gumbo_strdup(const char* str) {
47
+ const size_t size = strlen(str) + 1;
48
+ // The strdup(3) function isn't available in strict "-std=c99" mode
49
+ // (it's part of POSIX, not C99), so use malloc(3) and memcpy(3)
50
+ // instead:
51
+ char* buffer = gumbo_alloc(size);
52
+ return memcpy(buffer, str, size);
53
+ }
54
+
55
+ #ifdef GUMBO_DEBUG
56
+ #include <stdarg.h>
57
+ // Debug function to trace operation of the parser
58
+ // (define GUMBO_DEBUG to use).
59
+ void gumbo_debug(const char* format, ...) {
60
+ va_list args;
61
+ va_start(args, format);
62
+ vprintf(format, args);
63
+ va_end(args);
64
+ fflush(stdout);
65
+ }
66
+ #else
67
+ void gumbo_debug(const char* UNUSED_ARG(format), ...) {}
68
+ #endif