Skip to content

Commit 8615760

Browse files
committed
Should now pass.
1 parent 176d2cc commit 8615760

File tree

6 files changed

+43
-39
lines changed

6 files changed

+43
-39
lines changed

benchmark/parse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ int main(int argc, char *argv[]) {
224224
<< " Gigabytes/second: " << (p.size()) / (min_result * 1000000000.0)
225225
<< "\n";
226226
if(jsonoutput) {
227-
isok = isok && pj.printjson();
227+
isok = isok && pj.printjson(std::cout);
228228
}
229229
if(dump) {
230-
isok = isok && pj.dump_raw_tape();
230+
isok = isok && pj.dump_raw_tape(std::cout);
231231
}
232232
if (!isok) {
233233
printf(" Parsing failed. \n ");

include/simdjson/jsoncharutils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
#include "simdjson/parsedjson.h"
55

66
// structural chars here are
7-
// they are { 0x7b } 0x7d : 0x3a [ 0x5b ] 0x5d , 0x2c
7+
// they are { 0x7b } 0x7d : 0x3a [ 0x5b ] 0x5d , 0x2c (and NULL)
88
// we are also interested in the four whitespace characters
99
// space 0x20, linefeed 0x0a, horizontal tab 0x09 and carriage return 0x0d
1010

1111
// these are the chars that can follow a true/false/null or number atom
1212
// and nothing else
1313
const u32 structural_or_whitespace_negated[256] = {
14-
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
14+
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1515
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1616
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1717

include/simdjson/numberparsing.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ parse_float(const u8 *const buf,
242242
}
243243
i *= power_of_ten[308 + exponent];
244244
}
245+
if(is_not_structural_or_whitespace(*p)) {
246+
return false;
247+
}
245248
double d = negative ? -i : i;
246249
pj.write_tape_double(d);
247250
#ifdef JSON_TEST_NUMBERS // for unit testing
@@ -447,6 +450,9 @@ static really_inline bool parse_number(const u8 *const buf,
447450
}
448451
exponent += (negexp ? -expnumber : expnumber);
449452
}
453+
if(is_not_structural_or_whitespace(*p)) {
454+
return false;
455+
}
450456
i = negative ? -i : i;
451457
if ((exponent != 0) || (expnumber != 0)) {
452458
if (unlikely(digitcount >= 19)) { // this is uncommon!!!

include/simdjson/parsedjson.h

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct ParsedJson {
110110
// return false if the tape is likely wrong (e.g., you did not parse a valid
111111
// JSON).
112112
WARN_UNUSED
113-
bool printjson() {
113+
bool printjson(std::ostream &os) {
114114
if(!isvalid) return false;
115115
size_t tapeidx = 0;
116116
u64 tape_val = tape[tapeidx];
@@ -138,64 +138,64 @@ struct ParsedJson {
138138
type = (tape_val >> 56);
139139
if (!inobject[depth]) {
140140
if ((inobjectidx[depth] > 0) && (type != ']'))
141-
printf(", ");
141+
os << ", ";
142142
inobjectidx[depth]++;
143143
} else { // if (inobject) {
144144
if ((inobjectidx[depth] > 0) && ((inobjectidx[depth] & 1) == 0) &&
145145
(type != '}'))
146-
printf(", ");
146+
os << ", ";
147147
if (((inobjectidx[depth] & 1) == 1))
148-
printf(" : ");
148+
os << " : ";
149149
inobjectidx[depth]++;
150150
}
151151
switch (type) {
152152
case '"': // we have a string
153-
putchar('"');
153+
os << '"';
154154
print_with_escapes((const unsigned char *)(string_buf + payload));
155-
putchar('"');
155+
os << '"';
156156
break;
157157
case 'l': // we have a long int
158158
if (tapeidx + 1 >= howmany)
159159
return false;
160-
printf("%" PRId64, (int64_t)tape[++tapeidx]);
160+
os << (int64_t)tape[++tapeidx];
161161
break;
162162
case 'd': // we have a double
163163
if (tapeidx + 1 >= howmany)
164164
return false;
165165
double answer;
166166
memcpy(&answer, &tape[++tapeidx], sizeof(answer));
167-
printf("%f", answer);
167+
os << answer;
168168
break;
169169
case 'n': // we have a null
170-
printf("null");
170+
os << "null";
171171
break;
172172
case 't': // we have a true
173-
printf("true");
173+
os << "true";
174174
break;
175175
case 'f': // we have a false
176-
printf("false");
176+
os << "false";
177177
break;
178178
case '{': // we have an object
179-
printf("\n");
180-
printf("%*s\n%*s", depth, "{", depth + 1, "");
179+
os << '\n';
180+
os << '{';
181181
depth++;
182182
inobject[depth] = true;
183183
inobjectidx[depth] = 0;
184184
break;
185185
case '}': // we end an object
186186
depth--;
187-
printf("\n%*s}\n%*s", depth - 1, "", depth, "");
187+
os << '}';
188188
break;
189189
case '[': // we start an array
190-
printf("\n");
191-
printf("%*s\n%*s", depth, "[", depth + 1, "");
190+
os << '\n';
191+
os << '[';
192192
depth++;
193193
inobject[depth] = false;
194194
inobjectidx[depth] = 0;
195195
break;
196196
case ']': // we end an array
197197
depth--;
198-
printf("\n%*s]\n%*s", depth - 1, "", depth, "");
198+
os << ']';
199199
break;
200200
case 'r': // we start and end with the root node
201201
printf("should we be hitting the root node?\n");
@@ -215,7 +215,7 @@ struct ParsedJson {
215215
}
216216

217217
WARN_UNUSED
218-
bool dump_raw_tape() {
218+
bool dump_raw_tape(std::ostream &os) {
219219
if(!isvalid) return false;
220220
size_t tapeidx = 0;
221221
u64 tape_val = tape[tapeidx++];
@@ -228,52 +228,49 @@ struct ParsedJson {
228228
return false;
229229
}
230230
for (; tapeidx < howmany; tapeidx++) {
231-
printf("%zu : ", tapeidx);
231+
os << tapeidx << " : ";
232232
tape_val = tape[tapeidx];
233233
u64 payload = tape_val & JSONVALUEMASK;
234234
type = (tape_val >> 56);
235235
switch (type) {
236236
case '"': // we have a string
237-
printf("string: ");
238-
putchar('"');
237+
os << "string \"";
239238
print_with_escapes((const unsigned char *)(string_buf + payload));
240-
putchar('"');
241-
printf("\n");
239+
os << '"';
242240
break;
243241
case 'l': // we have a long int
244242
if (tapeidx + 1 >= howmany)
245243
return false;
246-
printf("integer: ");
247-
printf("%" PRId64"\n", (int64_t)tape[++tapeidx]);
244+
os << "integer " << (int64_t)tape[++tapeidx] << "\n";
248245
break;
249246
case 'd': // we have a double
250-
printf("float: ");
247+
os << "float ";
251248
if (tapeidx + 1 >= howmany)
252249
return false;
253250
double answer;
254251
memcpy(&answer, &tape[++tapeidx], sizeof(answer));
255-
printf("%f\n", answer);
252+
os << answer << '\n';
256253
break;
257254
case 'n': // we have a null
258-
printf("null\n");
255+
os << "null\n";
259256
break;
260257
case 't': // we have a true
261-
printf("true\n");
258+
os << "true\n";
262259
break;
263260
case 'f': // we have a false
264-
printf("false\n");
261+
os << "false\n";
265262
break;
266263
case '{': // we have an object
267-
printf("{\t// pointing to next tape location %llu \n", payload);
264+
os << "{\t// pointing to next tape location " << payload << "\n";
268265
break;
269266
case '}': // we end an object
270-
printf("}\t// pointing to previous tape location %llu \n", payload);
267+
os << "}\t// pointing to previous tape location " << payload << "\n";
271268
break;
272269
case '[': // we start an array
273-
printf("[\t// pointing to next tape location %llu \n", payload);
270+
os << "[\t// pointing to next tape location " << payload << "\n";
274271
break;
275272
case ']': // we end an array
276-
printf("]\t// pointing to previous tape location %llu \n", payload);
273+
os << "]\t// pointing to previous tape location " << payload << "\n";
277274
break;
278275
case 'r': // we start and end with the root node
279276
printf("end of root\n");

jsonchecker/fail38.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12 a]

tools/json2json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int main(int argc, char *argv[]) {
101101
}
102102
compute_dump(pjh);
103103
} else {
104-
is_ok = rawdump ? pj.dump_raw_tape() : pj.printjson();
104+
is_ok = rawdump ? pj.dump_raw_tape(std::cout) : pj.printjson(std::cout);
105105
if (!is_ok) {
106106
std::cerr << " Could not print out parsed result. " << std::endl;
107107
return EXIT_FAILURE;

0 commit comments

Comments
 (0)