on master help sign in
Remove printf debugging, fix a few off-by-one errors
commit024be8b7888a13ca2e5885484838f1423766c7dc authorNeil Mitchell <ndmitchell@gmail.com> dateMon 28 Nov 2016, 09:11 (10 years ago) parent13e2f41
1 file changed+21−26
hexml.c+21−26
hexml.c+21−26
@@ -124,7 +124,6 @@ void render_content(render* r, node* n)
124124 bound_str("render_conent attrs", n->attrs, 0, r->d->attrs.used);
125125
126126 int done = n->inner.start;
127− printf("%i\n", n->nodes.length);
128127 for (int i = 0; i < n->nodes.length; i++)
129128 {
130129 node* x = &r->d->nodes.nodes[n->nodes.start + i];
@@ -164,8 +163,6 @@ int document_render(document* d, char* buffer, int length)
164163 r.buffer = buffer;
165164 r.length = length;
166165 r.cursor = 0;
167− node* me = document_node(d);
168− assert(me->nodes.start == 1 && me->nodes.length == 1);
169166 render_content(&r, document_node(d));
170167 return r.cursor;
171168}
@@ -241,8 +238,10 @@ void static inline skip(document* d, int i){d->cursor += i;}
241238char static inline peek(document* d){return peekAt(d, 0);}
242239char static inline get(document* d){char c = peek(d); skip(d, 1); return c;}
243240
241+bool static inline is_space(char c){ return c == ' ' || c == '\t' || c == '\r' || c == '\n';}
242+
244243// Remove whitespace characters from the cursor while they are still whitespace
245−void trim(document* d)
244+void static inline trim(document* d)
246245{
247246 while (isspace(peek(d)))
248247 skip(d, 1);
@@ -287,15 +286,13 @@ void attr_alloc(attr_buffer* b, int ask)
287286 b->attrs = buf2;
288287}
289288
290−void lexeme(char* msg, document* d, str s)
291−{
292− if (1)
293− printf("Lexeme: %s, %i:%i, %.*s\n", msg, s.start, s.length, s.length, &d->body[s.start]);
294−}
295−
296−int isName(char c)
289+int static inline isName(char c)
297290{
298− return isalnum(c) || c == '?';
291+ return
292+ (c >= 'a' && c <= 'z') ||
293+ (c >= 'A' && c <= 'Z') ||
294+ (c >= '0' && c <= '9') ||
295+ c == '_' || c == '-';
299296}
300297
301298// you now expect a name, perhaps preceeded by whitespace
@@ -308,7 +305,6 @@ str parse_name(document* d)
308305 while (isName(peek(d)))
309306 get(d);
310307 res.length = d->cursor - res.start;
311− lexeme("parse_name", d, res);
312308 return res;
313309}
314310
@@ -395,15 +391,18 @@ void parse_tag(document* d)
395391 if (peek(d) == '<' && peekAt(d, 1) == '/')
396392 {
397393 skip(d, 2);
398− str close = parse_name(d);
399− lexeme("openning tag", d, d->nodes.nodes[me].name);
400− lexeme("closing tag", d, close);
401394 trim(d);
402− d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, d->cursor).length;
403− if (close.length == d->nodes.nodes[me].name.length &&
404− memcmp(&d->body[close.start], &d->body[d->nodes.nodes[me].name.start], close.length) == 0 &&
405− get(d) == '>');
406− return;
395+ if (d->length - d->cursor >= d->nodes.nodes[me].name.length &&
396+ memcmp(&d->body[d->cursor], &d->body[d->nodes.nodes[me].name.start], d->nodes.nodes[me].name.length) == 0)
397+ {
398+ skip(d, d->nodes.nodes[me].name.length);
399+ trim(d);
400+ if (get(d) == '>')
401+ {
402+ d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, d->cursor).length;
403+ return;
404+ }
405+ }
407406 d->error_message = _strdup("Mismatch in closing tags");
408407 return;
409408 }
@@ -428,17 +427,14 @@ str parse_content(document* d)
428427 }
429428 else
430429 {
431− printf("parsing a tag\n");
432430 parse_tag(d);
433431 }
434432 }
435433 int diff = d->nodes.used_back - before;
436434 node_alloc(&d->nodes, diff);
437435 str res = start_length(d->nodes.used_front, diff);
438−
439− printf("res %i %i\n", res.start, res.length);
440436 for (int i = 0; i < diff; i++)
441− d->nodes.nodes[d->nodes.used_front + i] = d->nodes.nodes[d->nodes.size - d->nodes.used_back - i];
437+ d->nodes.nodes[d->nodes.used_front + i] = d->nodes.nodes[d->nodes.size - d->nodes.used_back + diff - 1 - i];
442438 d->nodes.used_front += diff;
443439 d->nodes.used_back -= diff;
444440 return res;
@@ -469,7 +465,6 @@ document* document_parse(char* s, int slen)
469465
470466 if (d->cursor < d->length && d->error_message == NULL)
471467 {
472− printf("%i vs %i\n", d->cursor, d->length);
473468 d->error_message = _strdup("Trailing junk at the end of the document");
474469 }
475470 return d;