Skip to content

Commit

Permalink
Add better error messages to sajson, and fix a bug where UTF-8 would …
Browse files Browse the repository at this point in the history
…fail to parse in the slow path
  • Loading branch information
chadaustin committed Apr 18, 2015
1 parent 2c64930 commit b660ac4
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions include/sajson.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
Expand Down Expand Up @@ -519,10 +520,40 @@ namespace sajson {
}
}

error_result error(const char* message) {
error_result error(const char* format, ...) {
error_line = 1;
error_column = 1;
error_message = message;

char* c = input.get_data();
while (c < p) {
if (*c == '\r') {
if (c + 1 < p && c[1] == '\n') {
++error_line;
error_column = 1;
++c;
} else {
++error_line;
error_column = 1;
}
} else if (*c == '\n') {
++error_line;
error_column = 1;
} else {
// TODO: count UTF-8 characters
++error_column;
}
++c;
}


char buf[1024];
buf[1023] = 0;
va_list ap;
va_start(ap, format);
vsnprintf(buf, 1023, format, ap);
va_end(ap);

error_message = buf;
return error_result();
}

Expand Down Expand Up @@ -964,7 +995,7 @@ namespace sajson {
}

if (SAJSON_UNLIKELY(*p >= 0 && *p < 0x20)) {
return error("illegal unprintable codepoint in string");
return error("illegal unprintable codepoint in string: %d", static_cast<int>(*p));
}

switch (*p) {
Expand Down Expand Up @@ -1032,8 +1063,8 @@ namespace sajson {
return error("unexpected end of input");
}

if (SAJSON_UNLIKELY(*p < 0x20)) {
return error("illegal unprintable codepoint in string");
if (SAJSON_UNLIKELY(*p >= 0 && *p < 0x20)) {
return error("illegal unprintable codepoint in string: %d", static_cast<int>(*p));
}

switch (*p) {
Expand Down

0 comments on commit b660ac4

Please sign in to comment.