nokogumbo 1.4.8 → 1.4.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,132 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+ //
17
+ // This contains an implementation of a UTF8 iterator and decoder suitable for
18
+ // an HTML5 parser. This does a bit more than straight UTF-8 decoding. The
19
+ // HTML5 spec specifies that:
20
+ // 1. Decoding errors are parse errors.
21
+ // 2. Certain other codepoints (eg. control characters) are parse errors.
22
+ // 3. Carriage returns and CR/LF groups are converted to line feeds.
23
+ // http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#decoded-as-utf-8,-with-error-handling
24
+ //
25
+ // Also, we want to keep track of source positions for error handling. As a
26
+ // result, we fold all that functionality into this decoder, and can't use an
27
+ // off-the-shelf library.
28
+ //
29
+ // This header is internal-only, which is why we prefix functions with only
30
+ // utf8_ or utf8_iterator_ instead of gumbo_utf8_.
31
+
32
+ #ifndef GUMBO_UTF8_H_
33
+ #define GUMBO_UTF8_H_
34
+
35
+ #include <stdbool.h>
36
+ #include <stddef.h>
37
+
38
+ #include "gumbo.h"
39
+
40
+ #ifdef __cplusplus
41
+ extern "C" {
42
+ #endif
43
+
44
+ struct GumboInternalError;
45
+ struct GumboInternalParser;
46
+
47
+ // Unicode replacement char.
48
+ extern const int kUtf8ReplacementChar;
49
+
50
+ typedef struct GumboInternalUtf8Iterator {
51
+ // Points at the start of the code point most recently read into 'current'.
52
+ const char* _start;
53
+
54
+ // Points at the mark. The mark is initially set to the beginning of the
55
+ // input.
56
+ const char* _mark;
57
+
58
+ // Points past the end of the iter, like a past-the-end iterator in the STL.
59
+ const char* _end;
60
+
61
+ // The code point under the cursor.
62
+ int _current;
63
+
64
+ // The width in bytes of the current code point.
65
+ int _width;
66
+
67
+ // The SourcePosition for the current location.
68
+ GumboSourcePosition _pos;
69
+
70
+ // The SourcePosition for the mark.
71
+ GumboSourcePosition _mark_pos;
72
+
73
+ // Pointer back to the GumboParser instance, for configuration options and
74
+ // error recording.
75
+ struct GumboInternalParser* _parser;
76
+ } Utf8Iterator;
77
+
78
+ // Returns true if this Unicode code point is in the list of characters
79
+ // forbidden by the HTML5 spec, such as NUL bytes and undefined control chars.
80
+ bool utf8_is_invalid_code_point(int c);
81
+
82
+ // Initializes a new Utf8Iterator from the given byte buffer. The source does
83
+ // not have to be NUL-terminated, but the length must be passed in explicitly.
84
+ void utf8iterator_init(struct GumboInternalParser* parser, const char* source,
85
+ size_t source_length, Utf8Iterator* iter);
86
+
87
+ // Advances the current position by one code point.
88
+ void utf8iterator_next(Utf8Iterator* iter);
89
+
90
+ // Returns the current code point as an integer.
91
+ int utf8iterator_current(const Utf8Iterator* iter);
92
+
93
+ // Retrieves and fills the output parameter with the current source position.
94
+ void utf8iterator_get_position(
95
+ const Utf8Iterator* iter, GumboSourcePosition* output);
96
+
97
+ // Retrieves a character pointer to the start of the current character.
98
+ const char* utf8iterator_get_char_pointer(const Utf8Iterator* iter);
99
+
100
+ // Retrieves a character pointer to 1 past the end of the buffer. This is
101
+ // necessary for certain state machines and string comparisons that would like
102
+ // to look directly for ASCII text in the buffer without going through the
103
+ // decoder.
104
+ const char* utf8iterator_get_end_pointer(const Utf8Iterator* iter);
105
+
106
+ // If the upcoming text in the buffer matches the specified prefix (which has
107
+ // length 'length'), consume it and return true. Otherwise, return false with
108
+ // no other effects. If the length of the string would overflow the buffer,
109
+ // this returns false. Note that prefix should not contain null bytes because
110
+ // of the use of strncmp/strncasecmp internally. All existing use-cases adhere
111
+ // to this.
112
+ bool utf8iterator_maybe_consume_match(
113
+ Utf8Iterator* iter, const char* prefix, size_t length, bool case_sensitive);
114
+
115
+ // "Marks" a particular location of interest in the input stream, so that it can
116
+ // later be reset() to. There's also the ability to record an error at the
117
+ // point that was marked, as oftentimes that's more useful than the last
118
+ // character before the error was detected.
119
+ void utf8iterator_mark(Utf8Iterator* iter);
120
+
121
+ // Returns the current input stream position to the mark.
122
+ void utf8iterator_reset(Utf8Iterator* iter);
123
+
124
+ // Sets the position and original text fields of an error to the value at the
125
+ // mark.
126
+ void utf8iterator_fill_error_at_mark(
127
+ Utf8Iterator* iter, struct GumboInternalError* error);
128
+
129
+ #ifdef __cplusplus
130
+ }
131
+ #endif
132
+ #endif // GUMBO_UTF8_H_
@@ -0,0 +1,58 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+
17
+ #include "util.h"
18
+
19
+ #include <assert.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+ #include <strings.h>
23
+ #include <stdarg.h>
24
+ #include <stdio.h>
25
+
26
+ #include "gumbo.h"
27
+ #include "parser.h"
28
+
29
+ // TODO(jdtang): This should be elsewhere, but there's no .c file for
30
+ // SourcePositions and yet the constant needs some linkage, so this is as good
31
+ // as any.
32
+ const GumboSourcePosition kGumboEmptySourcePosition = {0, 0, 0};
33
+
34
+ void* gumbo_parser_allocate(GumboParser* parser, size_t num_bytes) {
35
+ return parser->_options->allocator(parser->_options->userdata, num_bytes);
36
+ }
37
+
38
+ void gumbo_parser_deallocate(GumboParser* parser, void* ptr) {
39
+ parser->_options->deallocator(parser->_options->userdata, ptr);
40
+ }
41
+
42
+ char* gumbo_copy_stringz(GumboParser* parser, const char* str) {
43
+ char* buffer = gumbo_parser_allocate(parser, strlen(str) + 1);
44
+ strcpy(buffer, str);
45
+ return buffer;
46
+ }
47
+
48
+ // Debug function to trace operation of the parser. Pass --copts=-DGUMBO_DEBUG
49
+ // to use.
50
+ void gumbo_debug(const char* format, ...) {
51
+ #ifdef GUMBO_DEBUG
52
+ va_list args;
53
+ va_start(args, format);
54
+ vprintf(format, args);
55
+ va_end(args);
56
+ fflush(stdout);
57
+ #endif
58
+ }
@@ -0,0 +1,60 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+ //
17
+ // This contains some utility functions that didn't fit into any of the other
18
+ // headers.
19
+
20
+ #ifndef GUMBO_UTIL_H_
21
+ #define GUMBO_UTIL_H_
22
+ #ifdef _MSC_VER
23
+ #define _CRT_SECURE_NO_WARNINGS
24
+ #endif
25
+ #include <stdbool.h>
26
+ #include <stddef.h>
27
+
28
+ #ifdef __cplusplus
29
+ extern "C" {
30
+ #endif
31
+
32
+ // Forward declaration since it's passed into some of the functions in this
33
+ // header.
34
+ struct GumboInternalParser;
35
+
36
+ // Utility function for allocating & copying a null-terminated string into a
37
+ // freshly-allocated buffer. This is necessary for proper memory management; we
38
+ // have the convention that all const char* in parse tree structures are
39
+ // freshly-allocated, so if we didn't copy, we'd try to delete a literal string
40
+ // when the parse tree is destroyed.
41
+ char* gumbo_copy_stringz(struct GumboInternalParser* parser, const char* str);
42
+
43
+ // Allocate a chunk of memory, using the allocator specified in the Parser's
44
+ // config options.
45
+ void* gumbo_parser_allocate(
46
+ struct GumboInternalParser* parser, size_t num_bytes);
47
+
48
+ // Deallocate a chunk of memory, using the deallocator specified in the Parser's
49
+ // config options.
50
+ void gumbo_parser_deallocate(struct GumboInternalParser* parser, void* ptr);
51
+
52
+ // Debug wrapper for printf, to make it easier to turn off debugging info when
53
+ // required.
54
+ void gumbo_debug(const char* format, ...);
55
+
56
+ #ifdef __cplusplus
57
+ }
58
+ #endif
59
+
60
+ #endif // GUMBO_UTIL_H_
@@ -0,0 +1,123 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+
17
+ #include "vector.h"
18
+
19
+ #include <assert.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+ #include <strings.h>
23
+
24
+ #include "util.h"
25
+
26
+ struct GumboInternalParser;
27
+
28
+ const GumboVector kGumboEmptyVector = {NULL, 0, 0};
29
+
30
+ void gumbo_vector_init(struct GumboInternalParser* parser,
31
+ size_t initial_capacity, GumboVector* vector) {
32
+ vector->length = 0;
33
+ vector->capacity = initial_capacity;
34
+ if (initial_capacity > 0) {
35
+ vector->data =
36
+ gumbo_parser_allocate(parser, sizeof(void*) * initial_capacity);
37
+ } else {
38
+ vector->data = NULL;
39
+ }
40
+ }
41
+
42
+ void gumbo_vector_destroy(
43
+ struct GumboInternalParser* parser, GumboVector* vector) {
44
+ if (vector->capacity > 0) {
45
+ gumbo_parser_deallocate(parser, vector->data);
46
+ }
47
+ }
48
+
49
+ static void enlarge_vector_if_full(
50
+ struct GumboInternalParser* parser, GumboVector* vector) {
51
+ if (vector->length >= vector->capacity) {
52
+ if (vector->capacity) {
53
+ size_t old_num_bytes = sizeof(void*) * vector->capacity;
54
+ vector->capacity *= 2;
55
+ size_t num_bytes = sizeof(void*) * vector->capacity;
56
+ void** temp = gumbo_parser_allocate(parser, num_bytes);
57
+ memcpy(temp, vector->data, old_num_bytes);
58
+ gumbo_parser_deallocate(parser, vector->data);
59
+ vector->data = temp;
60
+ } else {
61
+ // 0-capacity vector; no previous array to deallocate.
62
+ vector->capacity = 2;
63
+ vector->data =
64
+ gumbo_parser_allocate(parser, sizeof(void*) * vector->capacity);
65
+ }
66
+ }
67
+ }
68
+
69
+ void gumbo_vector_add(
70
+ struct GumboInternalParser* parser, void* element, GumboVector* vector) {
71
+ enlarge_vector_if_full(parser, vector);
72
+ assert(vector->data);
73
+ assert(vector->length < vector->capacity);
74
+ vector->data[vector->length++] = element;
75
+ }
76
+
77
+ void* gumbo_vector_pop(
78
+ struct GumboInternalParser* parser, GumboVector* vector) {
79
+ if (vector->length == 0) {
80
+ return NULL;
81
+ }
82
+ return vector->data[--vector->length];
83
+ }
84
+
85
+ int gumbo_vector_index_of(GumboVector* vector, const void* element) {
86
+ for (unsigned int i = 0; i < vector->length; ++i) {
87
+ if (vector->data[i] == element) {
88
+ return i;
89
+ }
90
+ }
91
+ return -1;
92
+ }
93
+
94
+ void gumbo_vector_insert_at(struct GumboInternalParser* parser, void* element,
95
+ unsigned int index, GumboVector* vector) {
96
+ assert(index >= 0);
97
+ assert(index <= vector->length);
98
+ enlarge_vector_if_full(parser, vector);
99
+ ++vector->length;
100
+ memmove(&vector->data[index + 1], &vector->data[index],
101
+ sizeof(void*) * (vector->length - index - 1));
102
+ vector->data[index] = element;
103
+ }
104
+
105
+ void gumbo_vector_remove(
106
+ struct GumboInternalParser* parser, void* node, GumboVector* vector) {
107
+ int index = gumbo_vector_index_of(vector, node);
108
+ if (index == -1) {
109
+ return;
110
+ }
111
+ gumbo_vector_remove_at(parser, index, vector);
112
+ }
113
+
114
+ void* gumbo_vector_remove_at(struct GumboInternalParser* parser,
115
+ unsigned int index, GumboVector* vector) {
116
+ assert(index >= 0);
117
+ assert(index < vector->length);
118
+ void* result = vector->data[index];
119
+ memmove(&vector->data[index], &vector->data[index + 1],
120
+ sizeof(void*) * (vector->length - index - 1));
121
+ --vector->length;
122
+ return result;
123
+ }
@@ -0,0 +1,67 @@
1
+ // Copyright 2010 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Author: jdtang@google.com (Jonathan Tang)
16
+
17
+ #ifndef GUMBO_VECTOR_H_
18
+ #define GUMBO_VECTOR_H_
19
+
20
+ #include "gumbo.h"
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ // Forward declaration since it's passed into some of the functions in this
27
+ // header.
28
+ struct GumboInternalParser;
29
+
30
+ // Initializes a new GumboVector with the specified initial capacity.
31
+ void gumbo_vector_init(struct GumboInternalParser* parser,
32
+ size_t initial_capacity, GumboVector* vector);
33
+
34
+ // Frees the memory used by an GumboVector. Does not free the contained
35
+ // pointers.
36
+ void gumbo_vector_destroy(
37
+ struct GumboInternalParser* parser, GumboVector* vector);
38
+
39
+ // Adds a new element to an GumboVector.
40
+ void gumbo_vector_add(
41
+ struct GumboInternalParser* parser, void* element, GumboVector* vector);
42
+
43
+ // Removes and returns the element most recently added to the GumboVector.
44
+ // Ownership is transferred to caller. Capacity is unchanged. If the vector is
45
+ // empty, NULL is returned.
46
+ void* gumbo_vector_pop(struct GumboInternalParser* parser, GumboVector* vector);
47
+
48
+ // Inserts an element at a specific index. This is potentially O(N) time, but
49
+ // is necessary for some of the spec's behavior.
50
+ void gumbo_vector_insert_at(struct GumboInternalParser* parser, void* element,
51
+ unsigned int index, GumboVector* vector);
52
+
53
+ // Removes an element from the vector, or does nothing if the element is not in
54
+ // the vector.
55
+ void gumbo_vector_remove(
56
+ struct GumboInternalParser* parser, void* element, GumboVector* vector);
57
+
58
+ // Removes and returns an element at a specific index. Note that this is
59
+ // potentially O(N) time and should be used sparingly.
60
+ void* gumbo_vector_remove_at(struct GumboInternalParser* parser,
61
+ unsigned int index, GumboVector* vector);
62
+
63
+ #ifdef __cplusplus
64
+ }
65
+ #endif
66
+
67
+ #endif // GUMBO_VECTOR_H_
@@ -0,0 +1,4 @@
1
+ /*Dummy file to satisfy source file dependencies on Windows platform*/
2
+ #define strcasecmp _stricmp
3
+ #define strncasecmp _strnicmp
4
+ #define inline __inline