@@ -1,5 +1,5 @@
1#include "hexml.h"
1#include "hexml.h"
2−#include <malloc.h>
2+#include <stdlib.h>
3#include <string.h>
3#include <string.h>
4#include <assert.h>
4#include <assert.h>
5#include <ctype.h>
5#include <ctype.h>
@@ -10,18 +10,18 @@ typedef int bool;
10/////////////////////////////////////////////////////////////////////
10/////////////////////////////////////////////////////////////////////
13−int static inline end(str s) { return s.start + s.length; }
13+static inline int end(str s) { return s.start + s.length; }
15−str static inline start_length(int32_t start, int32_t length)
15+static inline str start_length(int32_t start, int32_t length)
17− if (start < 0 || length < 0) assert(0);
17+ assert(start >= 0 && length >= 0);
19 res.start = start;
19 res.start = start;
20 res.length = length;
20 res.length = length;
21 return res;
21 return res;
24−str static inline start_end(int32_t start, int32_t end)
24+static inline str start_end(int32_t start, int32_t end)
26 return start_length(start, end - start);
26 return start_length(start, end - start);
@@ -40,7 +40,7 @@ typedef struct
40 // have a cursor at the end, which is stack scoped, so children write out, then I do
40 // have a cursor at the end, which is stack scoped, so children write out, then I do
41 // when you commit, you copy over from end to front
41 // when you commit, you copy over from end to front
43− // nodes
43+ // nodes
45 int used_front; // front entries, stored for good
45 int used_front; // front entries, stored for good
46 int used_back; // back entries, stack based, copied into front
46 int used_back; // back entries, stack based, copied into front
@@ -51,11 +51,11 @@ typedef struct
52struct document
52struct document
54− char* body; // pointer to initial argument, not owned by us
54+ const char* body; // pointer to initial argument, not owned by us
56 // things only used while parsing
56 // things only used while parsing
57− char* cursor; // pointer to where we are in body
57+ const char* cursor; // pointer to where we are in body
58− char* end; // pointer to one past the last char
58+ const char* end; // pointer to one past the last char
59 // if cursor is > end we have gone past the end
59 // if cursor is > end we have gone past the end
61 char* error_message;
61 char* error_message;
@@ -63,8 +63,8 @@ struct document
63 attr_buffer attrs;
63 attr_buffer attrs;
66−int static inline doc_length(document* d) { return d->end - d->body; }
66+static inline int doc_length(const document* d) { return (int) (d->end - d->body); }
67−int static inline doc_position(document* d) { return d->cursor - d->body; }
67+static inline int doc_position(const document* d) { return (int) (d->cursor - d->body); }
70/////////////////////////////////////////////////////////////////////
70/////////////////////////////////////////////////////////////////////
@@ -72,49 +72,45 @@ int static inline doc_position(document* d) { return d->cursor - d->body; }
73typedef struct
73typedef struct
75− document* d;
75+ const document* d;
76 char* buffer;
76 char* buffer;
77 int length;
77 int length;
78 int cursor;
78 int cursor;
81−void static inline render_char(render* r, char c)
81+static inline void render_char(render* r, char c)
83 if (r->cursor < r->length)
83 if (r->cursor < r->length)
84 r->buffer[r->cursor] = c;
84 r->buffer[r->cursor] = c;
85 r->cursor++;
85 r->cursor++;
88−void static inline bound(char* msg, int index, int mn, int mx)
88+static inline void bound(int idx, int mn, int mx)
90− if (index < mn || index > mx)
92− //printf("Bounds checking failed %s, got %i, which should be in %i:%i\n", msg, index, mn, mx);
93− assert(0);
90+ assert(idx >= mn && idx <= mx);
97−void static inline bound_str(char* msg, str s, int mn, int mx)
93+static inline void bound_str(str s, int mn, int mx)
99− if (s.length < 0) assert(0);
95+ assert(s.length >= 0);
100− bound(msg, s.start, mn, mx);
96+ bound(s.start, mn, mx);
101− bound(msg, end(s), mn, mx);
97+ bound(end(s), mn, mx);
104−void render_str(render* r, str s)
100+static void render_str(render* r, str s)
106− bound_str("render_str", s, 0, doc_length(r->d));
102+ bound_str(s, 0, doc_length(r->d));
107 for (int i = 0; i < s.length; i++)
103 for (int i = 0; i < s.length; i++)
108 render_char(r, r->d->body[s.start + i]);
104 render_char(r, r->d->body[s.start + i]);
111−void render_tag(render* r, node* n);
107+static void render_tag(render* r, const node* n);
113−void render_content(render* r, node* n)
109+static void render_content(render* r, const node* n)
115− bound_str("render_conent inner", n->inner, 0, doc_length(r->d));
111+ bound_str(n->inner, 0, doc_length(r->d));
116− bound_str("render_conent nodes", n->nodes, 0, r->d->nodes.used_front);
112+ bound_str(n->nodes, 0, r->d->nodes.used_front);
117− bound_str("render_conent attrs", n->attrs, 0, r->d->attrs.used);
113+ bound_str(n->attrs, 0, r->d->attrs.used);
119 int done = n->inner.start;
115 int done = n->inner.start;
120 for (int i = 0; i < n->nodes.length; i++)
116 for (int i = 0; i < n->nodes.length; i++)
@@ -127,7 +123,7 @@ void render_content(render* r, node* n)
127 render_str(r, start_end(done, end(n->inner)));
123 render_str(r, start_end(done, end(n->inner)));
130−void render_tag(render* r, node* n)
126+static void render_tag(render* r, const node* n)
132 render_char(r, '<');
128 render_char(r, '<');
133 render_str(r, n->name);
129 render_str(r, n->name);
@@ -149,14 +145,18 @@ void render_tag(render* r, node* n)
149 render_char(r, '>');
145 render_char(r, '>');
152−int document_render(document* d, char* buffer, int length)
148+int hexml_node_render(const document* d, const node* n, char* buffer, int length)
154 render r;
150 render r;
156 r.buffer = buffer;
152 r.buffer = buffer;
157 r.length = length;
153 r.length = length;
158 r.cursor = 0;
154 r.cursor = 0;
155+ // The root node (and only the root node) has an empty length, so just render its innards
156+ if (n->name.length == 0)
159− render_content(&r, document_node(d));
157+ render_content(&r, n);
160 return r.cursor;
160 return r.cursor;
@@ -164,10 +164,10 @@ int document_render(document* d, char* buffer, int length)
164/////////////////////////////////////////////////////////////////////
164/////////////////////////////////////////////////////////////////////
165// NOT THE PARSER
165// NOT THE PARSER
167−char* document_error(document* d){return d->error_message;}
167+char* hexml_document_error(const document* d){return d->error_message;}
168−node* document_node(document* d){return &d->nodes.nodes[0];}
168+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
170−void document_free(document* d)
170+void hexml_document_free(document* d)
172 free(d->error_message);
172 free(d->error_message);
173 free(d->nodes.alloc);
173 free(d->nodes.alloc);
@@ -175,25 +175,24 @@ void document_free(document* d)
178−node* node_children(document* d, node* n, int* res)
178+node* hexml_node_children(const document* d, const node* n, int* res)
180 *res = n->nodes.length;
180 *res = n->nodes.length;
181 return &d->nodes.nodes[n->nodes.start];
181 return &d->nodes.nodes[n->nodes.start];
184−attr* node_attributes(document* d, node* n, int* res)
184+attr* hexml_node_attributes(const document* d, const node* n, int* res)
186 *res = n->attrs.length;
186 *res = n->attrs.length;
187 return &d->attrs.attrs[n->attrs.start];
187 return &d->attrs.attrs[n->attrs.start];
191−attr* node_attributeBy(document* d, node* n, char* s, int slen)
191+attr* hexml_node_attribute(const document* d, const node* n, const char* s, int slen)
193− if (slen == -1) slen = strlen(s);
193+ if (slen == -1) slen = (int) strlen(s);
194− int i = n->attrs.start;
195− int limit = end(n->attrs);
194+ const int limit = end(n->attrs);
196− for (i = n->attrs.start; i < limit; i++)
195+ for (int i = n->attrs.start; i < limit; i++)
198 attr* r = &d->attrs.attrs[i];
197 attr* r = &d->attrs.attrs[i];
199 if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
198 if (r->name.length == slen && memcmp(s, &d->body[r->name.start], slen) == 0)
@@ -203,11 +202,11 @@ attr* node_attributeBy(document* d, node* n, char* s, int slen)
205// Search for given strings within a node
204// Search for given strings within a node
206−node* node_childBy(document* d, node* parent, node* prev, char* s, int slen)
205+node* hexml_node_child(const document* d, const node* parent, const node* prev, const char* s, int slen)
208− if (slen == -1) slen = strlen(s);
207+ if (slen == -1) slen = (int) strlen(s);
209− int i = prev == NULL ? parent->nodes.start : prev + 1 - d->nodes.nodes;
208+ int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
210− int limit = end(parent->nodes);
209+ const int limit = end(parent->nodes);
211 for (; i < limit; i++)
210 for (; i < limit; i++)
213 node* r = &d->nodes.nodes[i];
212 node* r = &d->nodes.nodes[i];
@@ -221,49 +220,49 @@ node* node_childBy(document* d, node* parent, node* prev, char* s, int slen)
221/////////////////////////////////////////////////////////////////////
220/////////////////////////////////////////////////////////////////////
222// PARSE TABLE
221// PARSE TABLE
224−const char tag_name1 = 0x1;
223+static const char tag_name1 = 0x1;
225−const char tag_name = 0x2;
224+static const char tag_name = 0x2;
226−const char tag_space = 0x4;
225+static const char tag_space = 0x4;
228−char parse_table[256];
227+static char parse_table[256];
230−void init_parse_table()
229+static void init_parse_table()
232 static bool done = 0;
231 static bool done = 0;
233 if (done) return;
232 if (done) return;
234 for (int i = 0; i < 256; i++)
233 for (int i = 0; i < 256; i++)
236 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
235 bool name1 = i == ':' || i == '_' || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z');
237− bool name = name1 || i == '-' || (i >= '0' && i <= '9');
236+ bool name = name1 || i == '-' || i == '.' || (i >= '0' && i <= '9');
238 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
237 bool space = i == ' ' || i == '\t' || i == '\r' || i == '\n';
239 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
238 parse_table[i] = (name1 ? tag_name1 : 0) | (name ? tag_name : 0) | (space ? tag_space : 0);
241 done = 1;
240 done = 1;
244−bool static inline is(char c, char tag) { return parse_table[c] & tag; }
243+static inline bool is(char c, char tag) { return parse_table[(unsigned char) c] & tag; }
245−bool static inline is_name1(char c) { return is(c, tag_name1); }
244+static inline bool is_name1(char c) { return is(c, tag_name1); }
246−bool static inline is_name(char c) { return is(c, tag_name); }
245+static inline bool is_name(char c) { return is(c, tag_name); }
247−bool static inline is_space(char c) { return is(c, tag_space); }
246+static inline bool is_space(char c) { return is(c, tag_space); }
250/////////////////////////////////////////////////////////////////////
249/////////////////////////////////////////////////////////////////////
251// PARSER COMBINATORS
250// PARSER COMBINATORS
253−char static inline peekAt(document* d, int i) { return d->cursor[i]; }
252+static inline char peek_at(const document* d, int i) { return d->cursor[i]; }
253+static inline char peek(const document* d) { return peek_at(d, 0); }
254−void static inline skip(document* d, int i) { d->cursor += i; }
254+static inline void skip(document* d, int i) { d->cursor += i; }
255−char static inline peek(document* d) { return peekAt(d, 0); }
256−char static inline get(document* d) { char c = peek(d); skip(d, 1); return c; }
255+static inline char get(document* d) { char c = peek(d); skip(d, 1); return c; }
258// Remove whitespace characters from the cursor while they are still whitespace
257// Remove whitespace characters from the cursor while they are still whitespace
259−void static inline trim(document* d)
258+static inline void trim(document* d)
261 while (is_space(peek(d)))
260 while (is_space(peek(d)))
262 skip(d, 1);
261 skip(d, 1);
265// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
264// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
266−bool find(document* d, char c)
265+static bool find(document* d, char c)
268 char* x = memchr(d->cursor, c, d->end - d->cursor);
267 char* x = memchr(d->cursor, c, d->end - d->cursor);
269 if (x == NULL)
268 if (x == NULL)
@@ -282,37 +281,47 @@ bool find(document* d, char c)
282/////////////////////////////////////////////////////////////////////
281/////////////////////////////////////////////////////////////////////
283// PARSING CODE
282// PARSING CODE
285−void static inline node_alloc(node_buffer* b, int ask)
284+static inline void node_alloc(node_buffer* b, int ask)
287 int space = b->size - b->used_back - b->used_front;
286 int space = b->size - b->used_back - b->used_front;
288 if (space >= ask) return;
287 if (space >= ask) return;
288+ int size2 = (b->size + 1000 + ask) * 2;
289+ node* buf2 = malloc(size2 * sizeof(node));
289− assert(0);
290+ assert(buf2);
291+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
292+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
292−void static inline attr_alloc(attr_buffer* b, int ask)
299+static inline void attr_alloc(attr_buffer* b, int ask)
294 int space = b->size - b->used;
301 int space = b->size - b->used;
295 if (space >= ask) return;
302 if (space >= ask) return;
296 int size2 = (b->size + 1000 + ask) * 2;
303 int size2 = (b->size + 1000 + ask) * 2;
297 attr* buf2 = malloc(size2 * sizeof(attr));
304 attr* buf2 = malloc(size2 * sizeof(attr));
298 memcpy(buf2, b->attrs, b->used * sizeof(attr));
306 memcpy(buf2, b->attrs, b->used * sizeof(attr));
299 free(b->alloc);
307 free(b->alloc);
300 b->attrs = buf2;
309 b->attrs = buf2;
301 b->alloc = buf2;
310 b->alloc = buf2;
304−void set_error(document* d, char* msg)
313+static void set_error(document* d, const char* msg)
306 if (d->error_message != NULL) return; // keep the first error message
315 if (d->error_message != NULL) return; // keep the first error message
307 d->error_message = malloc(strlen(msg)+1);
316 d->error_message = malloc(strlen(msg)+1);
317+ assert(d->error_message);
308 strcpy(d->error_message, msg);
318 strcpy(d->error_message, msg);
311// you now expect a name, perhaps preceeded by whitespace
321// you now expect a name, perhaps preceeded by whitespace
312// the name may be empty
322// the name may be empty
313−str static inline parse_name(document* d)
323+static inline str parse_name(document* d)
316 int start = doc_position(d);
325 int start = doc_position(d);
317 if (!is_name1(peek(d)))
326 if (!is_name1(peek(d)))
318 return start_length(start, 0);
327 return start_length(start, 0);
@@ -322,10 +331,14 @@ str static inline parse_name(document* d)
322 return start_end(start, doc_position(d));
331 return start_end(start, doc_position(d));
325−str static inline parse_attrval(document* d)
334+static inline str parse_attrval(document* d)
339+ set_error(d, "Expected = in attribute, but missing");
328− if (peek(d) != '=') return start_length(0,0);
340+ return start_length(0, 0);
329 skip(d, 1);
342 skip(d, 1);
331 char c = peek(d);
344 char c = peek(d);
@@ -342,19 +355,22 @@ str static inline parse_attrval(document* d)
342 return start_end(start, doc_position(d) - 1);
355 return start_end(start, doc_position(d) - 1);
345− return parse_name(d);
358+ {
359+ set_error(d, "Invalid attribute");
360+ return start_length(0, 0);
350// seen a tag name, now looking for attributes terminated by >
366// seen a tag name, now looking for attributes terminated by >
351// puts the attributes it finds in the attribute buffer
367// puts the attributes it finds in the attribute buffer
352−str static inline parse_attributes(document* d)
368+static inline str parse_attributes(document* d)
355− res.start = d->attrs.used;
370+ int start = d->attrs.used;
356− for (int i = 0; ; i++)
371+ while (d->error_message == NULL)
358 str name = parse_name(d);
374 str name = parse_name(d);
359 if (name.length == 0) break;
375 if (name.length == 0) break;
360 attr_alloc(&d->attrs, 1);
376 attr_alloc(&d->attrs, 1);
@@ -362,34 +378,39 @@ str static inline parse_attributes(document* d)
362 d->attrs.attrs[d->attrs.used].value = parse_attrval(d);
378 d->attrs.attrs[d->attrs.used].value = parse_attrval(d);
363 d->attrs.used++;
379 d->attrs.used++;
365− res.length = d->attrs.used - res.start;
381+ return start_end(start, d->attrs.used);
369−str parse_content(document* d);
384+static str parse_content(document* d);
372// Add a new entry into tag, am at a '<'
387// Add a new entry into tag, am at a '<'
373−void static inline parse_tag(document* d)
388+static inline void parse_tag(document* d)
375 node_alloc(&d->nodes, 1);
390 node_alloc(&d->nodes, 1);
376 d->nodes.used_back++;
391 d->nodes.used_back++;
377− int me = d->nodes.size - d->nodes.used_back;
392+ node* me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back];
379− d->nodes.nodes[me].outer.start = doc_position(d);
394+ me->outer.start = doc_position(d);
380 char c = get(d);
395 char c = get(d);
381 assert(c == '<');
396 assert(c == '<');
382 if (peek(d) == '?') skip(d, 1);
397 if (peek(d) == '?') skip(d, 1);
383− d->nodes.nodes[me].name = parse_name(d);
398+ me->name = parse_name(d);
399+ if (me->name.length == 0)
401+ set_error(d, "Missing tag name");
384− d->nodes.nodes[me].attrs = parse_attributes(d);
404+ me->attrs = parse_attributes(d);
405+ if (d->error_message != NULL) return;
386− c = get(d);
407+ c = peek(d);
387− if ((c == '/' || c == '?') && peek(d) == '>')
408+ if ((c == '/' || c == '?') && peek_at(d, 1) == '>')
389− skip(d, 1);
410+ skip(d, 2);
390− d->nodes.nodes[me].nodes = start_length(0, 0);
411+ me->nodes = start_length(0, 0);
391− d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, doc_position(d)).length;
412+ me->outer.length = start_end(me->outer.start, doc_position(d)).length;
392− d->nodes.nodes[me].inner = start_length(doc_position(d), 0);
413+ me->inner = start_length(doc_position(d), 0);
395 else if (c != '>')
416 else if (c != '>')
@@ -397,23 +418,28 @@ void static inline parse_tag(document* d)
397 set_error(d, "Gunk at the end of the tag");
418 set_error(d, "Gunk at the end of the tag");
400− d->nodes.nodes[me].inner.start = doc_position(d);
423+ me->inner.start = doc_position(d);
401− d->nodes.nodes[me].nodes = parse_content(d);
424+ str content = parse_content(d);
426+ // parse_content may have allocated more nodes, so recompute me
427+ me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back];
428+ me->nodes = content;
402− d->nodes.nodes[me].inner.length = start_end(d->nodes.nodes[me].inner.start, doc_position(d)).length;
429+ me->inner.length = start_end(me->inner.start, doc_position(d)).length;
404 if (d->error_message != NULL) return;
431 if (d->error_message != NULL) return;
405− if (peek(d) == '<' && peekAt(d, 1) == '/')
432+ if (peek(d) == '<' && peek_at(d, 1) == '/')
407 skip(d, 2);
434 skip(d, 2);
409− if (d->end - d->cursor >= d->nodes.nodes[me].name.length &&
435+ if (d->end - d->cursor >= me->name.length &&
410− memcmp(d->cursor, &d->body[d->nodes.nodes[me].name.start], d->nodes.nodes[me].name.length) == 0)
436+ memcmp(d->cursor, &d->body[me->name.start], me->name.length) == 0)
412− skip(d, d->nodes.nodes[me].name.length);
438+ skip(d, me->name.length);
414 if (get(d) == '>')
440 if (get(d) == '>')
416− d->nodes.nodes[me].outer.length = start_end(d->nodes.nodes[me].outer.start, doc_position(d)).length;
442+ me->outer.length = start_end(me->outer.start, doc_position(d)).length;
@@ -425,7 +451,7 @@ void static inline parse_tag(document* d)
426// Parser until </, return the index of your node children
452// Parser until </, return the index of your node children
427// Not inline as it is recursive
453// Not inline as it is recursive
428−str parse_content(document* d)
454+static str parse_content(document* d)
430 int before = d->nodes.used_back;
456 int before = d->nodes.used_back;
431 while (d->error_message == NULL)
457 while (d->error_message == NULL)
@@ -435,11 +461,33 @@ str parse_content(document* d)
438− if (peekAt(d, 1) == '/')
464+ else if (peek_at(d, 1) == '/')
440 // have found a </
466 // have found a </
469+ else if (peek_at(d, 1) == '!' && peek_at(d, 2) == '-' && peek_at(d, 3) == '-')
472+ // you can't reuse the two '-' characters for the closing as well
473+ if (peek_at(d, 0) == '\0' || peek_at(d, 1) == '\0')
475+ set_error(d, "Didn't get a closing comment");
476+ return start_end(0, 0);
483+ set_error(d, "Didn't get a closing comment");
484+ return start_end(0, 0);
487+ if (peek_at(d, -3) == '-' && peek_at(d, -2) == '-')
445 parse_tag(d);
493 parse_tag(d);
@@ -455,20 +503,23 @@ str parse_content(document* d)
455 return res;
503 return res;
506+// Based on looking at ~50Kb XML documents, they seem to have ~700 attributes
507+// and ~300 nodes, so size appropriately to cope with that.
458typedef struct
508typedef struct
460 document document;
510 document document;
461 attr attrs[1000];
511 attr attrs[1000];
462− node nodes[1000];
512+ node nodes[500];
465−document* document_parse(char* s, int slen)
515+document* hexml_document_parse(const char* s, int slen)
467− if (slen == -1) slen = strlen(s);
517+ if (slen == -1) slen = (int) strlen(s);
468 assert(s[slen] == 0);
518 assert(s[slen] == 0);
469 init_parse_table();
519 init_parse_table();
471 buffer* buf = malloc(sizeof(buffer));
521 buffer* buf = malloc(sizeof(buffer));
472 document* d = &buf->document;
523 document* d = &buf->document;
473 d->body = s;
524 d->body = s;
474 d->cursor = s;
525 d->cursor = s;
@@ -478,7 +529,7 @@ document* document_parse(char* s, int slen)
478 d->attrs.used = 0;
529 d->attrs.used = 0;
479 d->attrs.attrs = buf->attrs;
530 d->attrs.attrs = buf->attrs;
480 d->attrs.alloc = NULL;
531 d->attrs.alloc = NULL;
481− d->nodes.size = 1000;
532+ d->nodes.size = 500;
482 d->nodes.used_back = 0;
533 d->nodes.used_back = 0;
483 d->nodes.used_front = 1;
534 d->nodes.used_front = 1;
484 d->nodes.nodes = buf->nodes;
535 d->nodes.nodes = buf->nodes;
@@ -488,11 +539,13 @@ document* document_parse(char* s, int slen)
488 d->nodes.nodes[0].outer = start_length(0, slen);
539 d->nodes.nodes[0].outer = start_length(0, slen);
489 d->nodes.nodes[0].inner = start_length(0, slen);
540 d->nodes.nodes[0].inner = start_length(0, slen);
490 d->nodes.nodes[0].attrs = start_length(0, 0);
541 d->nodes.nodes[0].attrs = start_length(0, 0);
491− d->nodes.nodes[0].nodes = parse_content(d);
543+ // Introduce an intermediate result, otherwise behaviour is undefined
544+ // because there is no guaranteed ordering between LHS and RHS evaluation
545+ str content = parse_content(d);
546+ d->nodes.nodes[0].nodes = content;
493− if (d->cursor < d->end && d->error_message == NULL)
548+ if (d->cursor < d->end)
495 set_error(d, "Trailing junk at the end of the document");
549 set_error(d, "Trailing junk at the end of the document");
497 return d;
550 return d;