diff --git a/src/test.cpp b/src/test.cpp index 3b93de9..0ae4276 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -192,14 +192,21 @@ constexpr static struct Tables { whitespace['\n'] = true; whitespace['\r'] = true; whitespace['\t'] = true; + for (int i = 0; i < 10; ++i) { + number['0' + i] = true; + } + number['.'] = true; + number['+'] = true; + number['-'] = true; } alignas(16) bool whitespace[256]{}; + alignas(16) bool number[256]{}; } tables; namespace { // Straightforward recursive descent that doesn't handle string escaping and -// treats numbers as [0-9.]+. May stack overflow on deeply nested json documents +// all numbers. May stack overflow on deeply nested json documents struct Parser1 { Parser1(char *buf, int len, const Callbacks *callbacks, void *data) : buf(buf), bufEnd(buf + len), callbacks(callbacks), data(data) {} @@ -396,7 +403,7 @@ private: if (len() == 0) { return false; } - if ('0' <= *buf && *buf <= '9' || (*buf == '.')) { + if (tables.number[*buf]) { ++buf; } else { break; @@ -445,8 +452,8 @@ private: #define MUSTTAIL #endif -// Table-based ll(1) parser that doesn't handle escaping and treats numbers as -// [0-9.]+. Could be adapted to have a streaming interface. Uses O(1) memory. +// Table-based ll(1) parser that doesn't handle escaping and all numbers, with a +// streaming interface. Uses O(1) memory. struct Parser2 { Parser2(const Callbacks *callbacks, void *data) : callbacks(callbacks), data(data) { @@ -488,7 +495,7 @@ private: Status parse_number() { char *const bufBefore = buf; while (len() > 0) { - if ('0' <= *buf && *buf <= '9' || (*buf == '.')) { + if (tables.number[*buf]) { ++buf; } else { break;