source/myhtml/parser.c

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 267 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« source/myhtml/encoding_detect.c ↑ Summary source/myhtml/perf.c »
1 /*
2 Copyright 2015-2016 Alexander Borisov
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 http://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 Author: lex.borisov@gmail.com (Alexander Borisov)
17 */
18  
19 #include "myhtml/parser.h"
20  
21 0 void myhtml_parser_index(mythread_id_t thread_id, mythread_queue_node_t *qnode)
22 {
23 // TODO: Create index for attributes
24 0 }
25  
26 0 void myhtml_parser_stream(mythread_id_t thread_id, mythread_queue_node_t *qnode)
27 {
28 0 ▶ while(myhtml_rules_tree_dispatcher(qnode->tree, qnode->token)){}
Branch: 1 branch
BranchTrueFalseTotal
while(myhtml_rules_tree_dispatcher(qnode->tree, qnode->token)){} 0 0 0
29 0 }
30  
31 0 myhtml_incoming_buf_t * myhtml_parser_find_first_buf(myhtml_tree_t *tree, size_t begin)
32 {
33 0 myhtml_incoming_buf_t *inc_buf = tree->incoming_buf_first;
34 0 ▶ while (inc_buf && inc_buf->next && inc_buf->next->offset < begin) {
Branch: 3 branches
BranchTrueFalseTotal
while (inc_buf && inc_buf->next && inc_buf->next->offset < begin) { 0 0 0
while (inc_buf && inc_buf->next && inc_buf->next->offset < begin) { 0 0 0
while (inc_buf && inc_buf->next && inc_buf->next->offset < begin) { 0 0 0
35 0 inc_buf = inc_buf->next;
36 }
37
38 0 return inc_buf;
39 }
40  
41 0 void myhtml_parser_drop_first_newline(myhtml_tree_t *tree, size_t *begin, size_t *length)
42 {
43 0 ▶ if(*length == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(*length == 0) 0 0 0
44 0 return;
45
46 0 myhtml_incoming_buf_t *inc_buf = myhtml_parser_find_first_buf(tree, *begin);
47
48 0 size_t current_buf_offset = *begin - inc_buf->offset;
49
50 0 ▶ if(inc_buf->data[current_buf_offset] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if(inc_buf->data[current_buf_offset] == '\n') { 0 0 0
51 0 (*begin)++;
52 0 (*length)--;
53 }
54 0 ▶ else if(inc_buf->data[current_buf_offset] == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
else if(inc_buf->data[current_buf_offset] == '\r') { 0 0 0
55 0 current_buf_offset++;
56 0 (*begin)++;
57 0 (*length)--;
58
59 0 ▶ if(*length > 1 && inc_buf->data[current_buf_offset] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if(*length > 1 && inc_buf->data[current_buf_offset] == '\n') { 0 0 0
if(*length > 1 && inc_buf->data[current_buf_offset] == '\n') { 0 0 0
60 0 (*begin)++;
61 0 (*length)--;
62 }
63 }
64 }
65  
66 0 size_t myhtml_parser_add_text(myhtml_tree_t *tree, myhtml_string_t* string, const char *text, size_t begin, size_t length, myhtml_string_char_ref_chunk_t *str_chunk)
67 {
68 0 myhtml_incoming_buf_t *inc_buf = myhtml_parser_find_first_buf(tree, begin);
69
70 0 size_t current_buf_offset = begin - inc_buf->offset;
71 0 size_t save_str_len = string->length;
72
73 0 ▶ if((current_buf_offset + length) <= inc_buf->size)
Branch: 1 branch
BranchTrueFalseTotal
if((current_buf_offset + length) <= inc_buf->size) 0 0 0
74 {
75 0 ▶ if(tree->encoding == MyHTML_ENCODING_UTF_8)
Branch: 1 branch
BranchTrueFalseTotal
if(tree->encoding == MyHTML_ENCODING_UTF_8) 0 0 0
76 0 myhtml_string_append_with_preprocessing(string, &inc_buf->data[current_buf_offset], length, str_chunk->emit_null_char);
77 else
78 0 myhtml_string_append_with_convert_encoding(string,
79 0 &inc_buf->data[current_buf_offset],
80 length, tree->encoding);
81
82 0 return (string->length - save_str_len);
83 }
84
85 0 size_t buf_next_offset = inc_buf->size - current_buf_offset;
86
87 myhtml_encoding_result_t res;
88 0 myhtml_encoding_result_clean(&res);
89
90 0 ▶ if(tree->encoding == MyHTML_ENCODING_UTF_8)
Branch: 1 branch
BranchTrueFalseTotal
if(tree->encoding == MyHTML_ENCODING_UTF_8) 0 0 0
91 0 myhtml_string_append_with_preprocessing(string, &inc_buf->data[current_buf_offset], buf_next_offset, str_chunk->emit_null_char);
92 else
93 0 myhtml_string_append_chunk_with_convert_encoding(string, &res, &inc_buf->data[current_buf_offset],
94 buf_next_offset, tree->encoding);
95
96 0 length = length - buf_next_offset;
97 0 inc_buf = inc_buf->next;
98
99 0 ▶ if(tree->encoding == MyHTML_ENCODING_UTF_8) {
Branch: 1 branch
BranchTrueFalseTotal
if(tree->encoding == MyHTML_ENCODING_UTF_8) { 0 0 0
100 0 ▶ while (inc_buf && length)
Branch: 2 branches
BranchTrueFalseTotal
while (inc_buf && length) 0 0 0
while (inc_buf && length) 0 0 0
101 {
102 0 ▶ if(length > inc_buf->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > inc_buf->size) { 0 0 0
103 0 myhtml_string_append_with_preprocessing(string, inc_buf->data, inc_buf->size, str_chunk->emit_null_char);
104 0 length -= inc_buf->size;
105 }
106 else {
107 0 myhtml_string_append_with_preprocessing(string, inc_buf->data, length, str_chunk->emit_null_char);
108 0 break;
109 }
110
111 0 inc_buf = inc_buf->next;
112 }
113 }
114 else {
115 0 ▶ while (inc_buf && length)
Branch: 2 branches
BranchTrueFalseTotal
while (inc_buf && length) 0 0 0
while (inc_buf && length) 0 0 0
116 {
117 0 ▶ if(length > inc_buf->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > inc_buf->size) { 0 0 0
118 0 myhtml_string_append_chunk_with_convert_encoding(string, &res, inc_buf->data,
119 inc_buf->size, tree->encoding);
120 0 length -= inc_buf->size;
121 }
122 else {
123 0 myhtml_string_append_chunk_with_convert_encoding(string, &res, inc_buf->data,
124 length, tree->encoding);
125 0 break;
126 }
127
128 0 inc_buf = inc_buf->next;
129 }
130 }
131
132 0 return (string->length - save_str_len);
133 }
134  
135 0 size_t myhtml_parser_add_text_lowercase(myhtml_tree_t *tree, myhtml_string_t* string, const char *text, size_t begin, size_t length, myhtml_string_char_ref_chunk_t *str_chunk)
136 {
137 0 myhtml_incoming_buf_t *inc_buf = myhtml_parser_find_first_buf(tree, begin);
138 0 myhtml_encoding_result_clean(&str_chunk->res);
139
140 0 size_t current_buf_offset = begin - inc_buf->offset;
141 0 size_t save_str_len = string->length;
142
143 0 ▶ if((current_buf_offset + length) <= inc_buf->size)
Branch: 1 branch
BranchTrueFalseTotal
if((current_buf_offset + length) <= inc_buf->size) 0 0 0
144 {
145 0 ▶ if(tree->encoding == MyHTML_ENCODING_UTF_8)
Branch: 1 branch
BranchTrueFalseTotal
if(tree->encoding == MyHTML_ENCODING_UTF_8) 0 0 0
146 0 myhtml_string_append_lowercase_with_preprocessing(string, &inc_buf->data[current_buf_offset], length);
147 else
148 0 myhtml_string_append_chunk_lowercase_ascii_with_convert_encoding(string, &str_chunk->res, &inc_buf->data[current_buf_offset], length, tree->encoding);
149
150 0 return (string->length - save_str_len);
151 }
152
153 0 size_t buf_next_offset = inc_buf->size - current_buf_offset;
154
155 0 ▶ if(tree->encoding == MyHTML_ENCODING_UTF_8)
Branch: 1 branch
BranchTrueFalseTotal
if(tree->encoding == MyHTML_ENCODING_UTF_8) 0 0 0
156 0 myhtml_string_append_lowercase_with_preprocessing(string, &inc_buf->data[current_buf_offset], buf_next_offset);
157 else
158 0 myhtml_string_append_chunk_lowercase_ascii_with_convert_encoding(string, &str_chunk->res, &inc_buf->data[current_buf_offset], buf_next_offset, tree->encoding);
159
160 0 length = length - buf_next_offset;
161 0 inc_buf = inc_buf->next;
162
163 0 ▶ if(tree->encoding == MyHTML_ENCODING_UTF_8) {
Branch: 1 branch
BranchTrueFalseTotal
if(tree->encoding == MyHTML_ENCODING_UTF_8) { 0 0 0
164 0 ▶ while (inc_buf && length)
Branch: 2 branches
BranchTrueFalseTotal
while (inc_buf && length) 0 0 0
while (inc_buf && length) 0 0 0
165 {
166 0 ▶ if(length > inc_buf->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > inc_buf->size) { 0 0 0
167 0 myhtml_string_append_lowercase_with_preprocessing(string, inc_buf->data, inc_buf->size);
168 0 length -= inc_buf->size;
169 }
170 else {
171 0 myhtml_string_append_lowercase_with_preprocessing(string, inc_buf->data, length);
172 0 break;
173 }
174
175 0 inc_buf = inc_buf->next;
176 }
177 }
178 else {
179 0 ▶ while (inc_buf && length)
Branch: 2 branches
BranchTrueFalseTotal
while (inc_buf && length) 0 0 0
while (inc_buf && length) 0 0 0
180 {
181 0 ▶ if(length > inc_buf->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > inc_buf->size) { 0 0 0
182 0 myhtml_string_append_chunk_lowercase_ascii_with_convert_encoding(string, &str_chunk->res, inc_buf->data, inc_buf->size, tree->encoding);
183 0 length -= inc_buf->size;
184 }
185 else {
186 0 myhtml_string_append_chunk_lowercase_ascii_with_convert_encoding(string, &str_chunk->res, inc_buf->data, length, tree->encoding);
187 0 break;
188 }
189
190 0 inc_buf = inc_buf->next;
191 }
192 }
193
194 0 return (string->length - save_str_len);
195 }
196  
197 0 size_t myhtml_parser_add_text_with_charef(myhtml_tree_t *tree, myhtml_string_t* string, const char *text, size_t begin, size_t length, myhtml_string_char_ref_chunk_t *str_chunk)
198 {
199 0 myhtml_incoming_buf_t *inc_buf = myhtml_parser_find_first_buf(tree, begin);
200 0 myhtml_encoding_result_clean(&str_chunk->res);
201
202 0 size_t current_buf_offset = begin - inc_buf->offset;
203 0 size_t save_str_len = string->length;
204
205 0 ▶ if((current_buf_offset + length) <= inc_buf->size)
Branch: 1 branch
BranchTrueFalseTotal
if((current_buf_offset + length) <= inc_buf->size) 0 0 0
206 {
207 0 myhtml_string_append_charef(str_chunk, string, &inc_buf->data[current_buf_offset], length);
208 0 myhtml_string_append_charef_end(str_chunk, string);
209 0 return (string->length - save_str_len);
210 }
211
212 0 size_t buf_next_offset = inc_buf->size - current_buf_offset;
213
214 0 myhtml_string_append_charef(str_chunk, string, &inc_buf->data[current_buf_offset], buf_next_offset);
215
216 0 length = length - buf_next_offset;
217 0 inc_buf = inc_buf->next;
218
219 0 ▶ while (inc_buf && length)
Branch: 2 branches
BranchTrueFalseTotal
while (inc_buf && length) 0 0 0
while (inc_buf && length) 0 0 0
220 {
221 0 ▶ if(length > inc_buf->size) {
Branch: 1 branch
BranchTrueFalseTotal
if(length > inc_buf->size) { 0 0 0
222 0 myhtml_string_append_charef(str_chunk, string, inc_buf->data, inc_buf->size);
223 0 length -= inc_buf->size;
224 }
225 else {
226 0 myhtml_string_append_charef(str_chunk, string, inc_buf->data, length);
227 0 break;
228 }
229
230 0 inc_buf = inc_buf->next;
231 }
232
233 0 myhtml_string_append_charef_end(str_chunk, string);
234 0 return (string->length - save_str_len);
235 }
236  
237 0 void myhtml_parser_worker(mythread_id_t thread_id, mythread_queue_node_t *qnode)
238 {
239 0 myhtml_token_node_t* token = qnode->token;
240
241 0 size_t mchar_node_id = qnode->tree->async_args[thread_id].mchar_node_id;
242
243 0 ▶ if(token->tag_ctx_idx == MyHTML_TAG__TEXT ||
Branch: 1 branch
BranchTrueFalseTotal
if(token->tag_ctx_idx == MyHTML_TAG__TEXT || 0 0 0
244 0 ▶ token->tag_ctx_idx == MyHTML_TAG__COMMENT)
Branch: 1 branch
BranchTrueFalseTotal
token->tag_ctx_idx == MyHTML_TAG__COMMENT) 0 0 0
245 0 {
246 0 myhtml_string_init(qnode->tree->mchar, mchar_node_id, &token->my_str_tm, (qnode->length + 4));
247
248 0 token->begin = token->my_str_tm.length;
249 0 token->length = qnode->length;
250 0 token->attr_first = NULL;
251 0 token->attr_last = NULL;
252
253 0 myhtml_string_char_ref_chunk_t str_chunk = {0, 0, 0, {0}, false, false, qnode->tree->encoding};
254
255 0 ▶ if(token->type & MyHTML_TOKEN_TYPE_DATA) {
Branch: 1 branch
BranchTrueFalseTotal
if(token->type & MyHTML_TOKEN_TYPE_DATA) { 0 0 0
256 0 str_chunk.emit_null_char = true;
257
258 0 token->length = myhtml_parser_add_text_with_charef(qnode->tree, &token->my_str_tm, qnode->text, qnode->begin, qnode->length, &str_chunk);
259 }
260 0 ▶ else if(token->type & MyHTML_TOKEN_TYPE_RCDATA || token->type & MyHTML_TOKEN_TYPE_CDATA) {
Branch: 2 branches
BranchTrueFalseTotal
else if(token->type & MyHTML_TOKEN_TYPE_RCDATA || token->type & MyHTML_TOKEN_TYPE_CDATA) { 0 0 0
else if(token->type & MyHTML_TOKEN_TYPE_RCDATA || token->type & MyHTML_TOKEN_TYPE_CDATA) { 0 0 0
261 0 token->length = myhtml_parser_add_text_with_charef(qnode->tree, &token->my_str_tm, qnode->text, qnode->begin, qnode->length, &str_chunk);
262 }
263 else
264 0 token->length = myhtml_parser_add_text(qnode->tree, &token->my_str_tm, qnode->text, qnode->begin, qnode->length, &str_chunk);
265 }
266 0 ▶ else if(token->attr_first)
Branch: 1 branch
BranchTrueFalseTotal
else if(token->attr_first) 0 0 0
267 {
268 0 token->my_str_tm.data = NULL;
269 0 token->my_str_tm.mchar = NULL;
270 0 token->my_str_tm.node_idx = 0;
271 0 token->my_str_tm.length = 0;
272 0 token->my_str_tm.size = 0;
273
274 0 token->begin = 0;
275 0 token->length = 0;
276
277 0 myhtml_token_attr_t* attr = token->attr_first;
278 0 myhtml_string_char_ref_chunk_t str_chunk = {0, 0, 0, {0}, false, false, qnode->tree->encoding};
279
280 0 ▶ while(attr)
Branch: 1 branch
BranchTrueFalseTotal
while(attr) 0 0 0
281 {
282 0 myhtml_string_init(qnode->tree->mchar, mchar_node_id, &attr->entry, (attr->name_length + attr->value_length + 8));
283
284 0 ▶ if(attr->name_length)
Branch: 1 branch
BranchTrueFalseTotal
if(attr->name_length) 0 0 0
285 {
286 0 size_t begin = attr->name_begin;
287 0 attr->name_begin = attr->entry.length;
288
289 0 attr->name_length = myhtml_parser_add_text_lowercase(qnode->tree, &attr->entry, qnode->text, begin, attr->name_length, &str_chunk);
290 }
291 else {
292 0 attr->name_begin = 0;
293 }
294
295 0 ▶ if(attr->value_length)
Branch: 1 branch
BranchTrueFalseTotal
if(attr->value_length) 0 0 0
296 {
297 0 size_t begin = attr->value_begin;
298 0 attr->value_begin = attr->entry.length;
299
300 0 str_chunk.is_attributes = true;
301
302 0 attr->value_length = myhtml_parser_add_text_with_charef(qnode->tree, &attr->entry, qnode->text, begin, attr->value_length, &str_chunk);
303 }
304 else {
305 0 attr->value_begin = 0;
306 }
307
308 0 attr->my_namespace = MyHTML_NAMESPACE_HTML;
309
310 0 attr = attr->next;
311 }
312 }
313 else {
314 0 token->begin = 0;
315 0 token->length = 0;
316 0 token->attr_first = NULL;
317 0 token->attr_last = NULL;
318
319 0 token->my_str_tm.data = NULL;
320 0 token->my_str_tm.mchar = NULL;
321 0 token->my_str_tm.node_idx = 0;
322 0 token->my_str_tm.length = 0;
323 0 token->my_str_tm.size = 0;
324 }
325
326 0 token->type |= MyHTML_TOKEN_TYPE_DONE;
327 0 }
328  
329 0 void myhtml_parser_worker_index_stream(mythread_id_t thread_id, mythread_queue_node_t *qnode)
330 {
331 0 myhtml_parser_worker(thread_id, qnode);
332 0 myhtml_parser_index(thread_id, qnode);
333 0 myhtml_parser_stream(thread_id, qnode);
334 0 }
335  
336 0 void myhtml_parser_worker_stream(mythread_id_t thread_id, mythread_queue_node_t *qnode)
337 {
338 0 myhtml_parser_worker(thread_id, qnode);
339 0 myhtml_parser_stream(thread_id, qnode);
340 0 }
341  
342 0 void myhtml_parser_worker_index(mythread_id_t thread_id, mythread_queue_node_t *qnode)
343 {
344 0 myhtml_parser_worker(thread_id, qnode);
345 0 myhtml_parser_index(thread_id, qnode);
346 0 }
347  
348 0 void myhtml_parser_stream_index(mythread_id_t thread_id, mythread_queue_node_t *qnode)
349 {
350 0 myhtml_parser_stream(thread_id, qnode);
351 0 myhtml_parser_index(thread_id, qnode);
352 0 }
353  
« source/myhtml/encoding_detect.c ↑ Summary source/myhtml/perf.c »