| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/bits" |
| 5 |
"unsafe" |
| 6 |
) |
| 7 |
|
| 8 |
// The values which are not decoded, as the ones of the keys which match no field, are skipped by the grammar of |
| 9 |
// JSON, as encoding/json checks the whole input: a value which is not valid is a syntax error wherever it is. |
| 10 |
|
| 11 |
// skipState is where a skip of an object or an array is: what it looks for next. |
| 12 |
type skipState uint8 |
| 13 |
|
| 14 |
const ( |
| 15 |
// skipValueStart looks for a value. |
| 16 |
skipValueStart skipState = iota |
| 17 |
// skipAfterValue looks for what follows a value: a comma or the end of the object or the array it is in. |
| 18 |
skipAfterValue |
| 19 |
) |
| 20 |
|
| 21 |
// skipRestOfObject returns the position after the end of the object whose value ends at cursor, which it checks by |
| 22 |
// the grammar of JSON: nesting is how deep the object is nested in the input. |
| 23 |
func skipRestOfObject(buf []byte, cursor, nesting int64) (int64, error) { |
| 24 |
return skipGrammarFast(buf, cursor, nesting-1, 1, 1, resumeAfterValue) |
| 25 |
} |
| 26 |
|
| 27 |
// skipResume is where skipFast resumes: at a value, after a value, or after the key of an object. |
| 28 |
type skipResume uint8 |
| 29 |
|
| 30 |
const ( |
| 31 |
resumeValue skipResume = iota |
| 32 |
resumeAfterValue |
| 33 |
resumeAfterKey |
| 34 |
// resumeKey is where a key is waited for, which skipFast doesn't resume at: it tells the error of the byte. |
| 35 |
resumeKey |
| 36 |
) |
| 37 |
|
| 38 |
// skipEvent is why skipFast returns: the end of the value, or what it doesn't do itself, which its caller does |
| 39 |
// before it resumes it. |
| 40 |
type skipEvent uint8 |
| 41 |
|
| 42 |
const ( |
| 43 |
skipDone skipEvent = iota |
| 44 |
// skipAString is a string which has an escape or a control character, or is at the end of the buffer. |
| 45 |
skipAString |
| 46 |
// skipANumber is a number which starts with a minus sign or is not an integer. |
| 47 |
skipANumber |
| 48 |
// skipDeep is an object or an array deeper than the levels which the bits of a word hold. |
| 49 |
skipDeep |
| 50 |
// skipInvalid is a byte which the grammar doesn't have where it is, for which the caller makes the error. |
| 51 |
skipInvalid |
| 52 |
) |
| 53 |
|
| 54 |
// skipGrammarFast skips the objects and the arrays which are open, level of them whose kinds are the bits of |
| 55 |
// objects, from cursor, where it resumes, by skipFast, which returns for what it doesn't do: a string with an |
| 56 |
// escape, a number which is not an integer, a byte which is not valid, which is done here, or an object or an array |
| 57 |
// deeper than 64 levels, which the rest of the value is skipped by skipGrammar for. |
| 58 |
func skipGrammarFast(buf []byte, cursor, nesting, level int64, objects uint64, resume skipResume) (int64, error) { |
| 59 |
var ev skipEvent |
| 60 |
cursor, level, objects, resume, ev = skipFast(buf, cursor, level, objects, resume, skipMaxLevel(nesting)) |
| 61 |
if ev == skipDone { |
| 62 |
return cursor, nil |
| 63 |
} |
| 64 |
return skipGrammarEvent(buf, cursor, nesting, level, objects, resume, ev) |
| 65 |
} |
| 66 |
|
| 67 |
// skipMaxLevel returns the number of the levels of objects and arrays which skipFast opens at most, for a value |
| 68 |
// nested nesting levels deep. |
| 69 |
func skipMaxLevel(nesting int64) int64 { |
| 70 |
return min(maxDecodeNestingDepth-nesting, 64) |
| 71 |
} |
| 72 |
|
| 73 |
// skipGrammarEvent does what skipFast stopped for at cursor, ev, and resumes it, up to the end of the value. |
| 74 |
func skipGrammarEvent(buf []byte, cursor, nesting, level int64, objects uint64, resume skipResume, ev skipEvent) (int64, error) { |
| 75 |
maxLevel := skipMaxLevel(nesting) |
| 76 |
for { |
| 77 |
switch ev { |
| 78 |
case skipDone: |
| 79 |
return cursor, nil |
| 80 |
case skipAString: |
| 81 |
end, err := skipString(buf, cursor) |
| 82 |
if err != nil { |
| 83 |
return 0, stringError(buf, cursor, err) |
| 84 |
} |
| 85 |
cursor = end |
| 86 |
case skipANumber: |
| 87 |
end, err := skipNumber(buf, cursor) |
| 88 |
if err != nil { |
| 89 |
return 0, err |
| 90 |
} |
| 91 |
cursor = end |
| 92 |
case skipDeep: |
| 93 |
// the value at cursor opens the level 64, or the limit of the nesting |
| 94 |
return skipGrammar(buf, cursor, nesting, level, objects, skipValueStart) |
| 95 |
default: |
| 96 |
return 0, skipFastError(buf, cursor, level, objects, resume) |
| 97 |
} |
| 98 |
cursor, level, objects, resume, ev = skipFast(buf, cursor, level, objects, resume, maxLevel) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// skipFastError returns the syntax error of the byte at cursor, which skipFast stopped at, where it resumes. |
| 103 |
func skipFastError(buf []byte, cursor, level int64, objects uint64, resume skipResume) error { |
| 104 |
switch resume { |
| 105 |
case resumeKey: |
| 106 |
return syntaxErrorAt(buf, cursor, whereKey) |
| 107 |
case resumeAfterKey: |
| 108 |
return syntaxErrorAt(buf, cursor, whereAfterKey) |
| 109 |
case resumeAfterValue: |
| 110 |
if objects&(1<<uint(level-1)) != 0 { |
| 111 |
return syntaxErrorAt(buf, cursor, whereAfterPair) |
| 112 |
} |
| 113 |
return syntaxErrorAt(buf, cursor, whereAfterElement) |
| 114 |
} |
| 115 |
switch buf[cursor] { |
| 116 |
case 't': |
| 117 |
return literalSyntaxError(buf, cursor, "true") |
| 118 |
case 'f': |
| 119 |
return literalSyntaxError(buf, cursor, "false") |
| 120 |
case 'n': |
| 121 |
return literalSyntaxError(buf, cursor, "null") |
| 122 |
} |
| 123 |
return syntaxErrorAt(buf, cursor, whereValue) |
| 124 |
} |
| 125 |
|
| 126 |
// skipFast skips from cursor, where it resumes, the objects and the arrays which are open, level of them whose |
| 127 |
// kinds are the bits of objects, set for an object, up to the end of the outermost of them, and returns where it |
| 128 |
// is and why it stops: at the end, or at what it doesn't do itself ( see skipEvent ), after which its caller |
| 129 |
// resumes it. It calls nothing, so that its loop keeps its state in the registers. |
| 130 |
// |
| 131 |
// For skipInvalid, resume is the state at the byte: resumeValue for a value, resumeAfterValue after a value, |
| 132 |
// resumeKey where a key is waited for and resumeAfterKey where its colon is. |
| 133 |
// |
| 134 |
//nolint:maintidx // one loop of gotos without calls, whose state stays in the registers |
| 135 |
func skipFast(buf []byte, cursor, level int64, objects uint64, resume skipResume, maxLevel int64) (int64, int64, uint64, skipResume, skipEvent) { |
| 136 |
length := int64(len(buf)) |
| 137 |
// the bytes are read without the checks of the bounds: every loop stops at the nul byte at the end of the |
| 138 |
// buffer, and a literal is compared byte by byte up to the first byte which differs |
| 139 |
b := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 140 |
// c is the byte at cursor after the white spaces are skipped: a byte above ' ' isn't one, so that the table |
| 141 |
// of the white spaces is read only for the bytes which may be |
| 142 |
var c byte |
| 143 |
switch resume { |
| 144 |
case resumeAfterValue: |
| 145 |
goto afterValue |
| 146 |
case resumeAfterKey: |
| 147 |
goto afterKey |
| 148 |
} |
| 149 |
value: |
| 150 |
if c = char(b, cursor); c <= ' ' { |
| 151 |
for isWhiteSpace[c] { |
| 152 |
cursor++ |
| 153 |
c = char(b, cursor) |
| 154 |
} |
| 155 |
} |
| 156 |
// a string, which most values are, before the switch of the others |
| 157 |
if c == '"' { |
| 158 |
// a string of plain bytes up to its quote, eight bytes at a time |
| 159 |
for c := cursor + 1; c+8 <= length; c += 8 { |
| 160 |
if special := keyEndBytes(load64(buf, c)); special != 0 { |
| 161 |
if c += int64(bits.TrailingZeros64(special) / 8); char(b, c) == '"' { |
| 162 |
cursor = c + 1 |
| 163 |
goto afterValue |
| 164 |
} |
| 165 |
break |
| 166 |
} |
| 167 |
} |
| 168 |
return cursor, level, objects, resumeAfterValue, skipAString |
| 169 |
} |
| 170 |
switch c { |
| 171 |
case '{', '[': |
| 172 |
if level+1 > maxLevel { |
| 173 |
return cursor, level, objects, resumeValue, skipDeep |
| 174 |
} |
| 175 |
if c == '{' { |
| 176 |
objects |= 1 << uint(level) |
| 177 |
} else { |
| 178 |
objects &^= 1 << uint(level) |
| 179 |
} |
| 180 |
level++ |
| 181 |
cursor++ |
| 182 |
open := c |
| 183 |
if c = char(b, cursor); c <= ' ' { |
| 184 |
for isWhiteSpace[c] { |
| 185 |
cursor++ |
| 186 |
c = char(b, cursor) |
| 187 |
} |
| 188 |
} |
| 189 |
if open == '{' { |
| 190 |
if c == '}' { |
| 191 |
cursor++ |
| 192 |
goto closed |
| 193 |
} |
| 194 |
goto key |
| 195 |
} |
| 196 |
if c == ']' { |
| 197 |
cursor++ |
| 198 |
goto closed |
| 199 |
} |
| 200 |
goto value |
| 201 |
case '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 202 |
start := cursor |
| 203 |
cursor++ |
| 204 |
for char(b, cursor)-'0' <= 9 { |
| 205 |
cursor++ |
| 206 |
} |
| 207 |
if c := char(b, cursor); c == '.' || c == 'e' || c == 'E' { |
| 208 |
// a number which is not an integer, which skipNumber checks from its start |
| 209 |
return start, level, objects, resumeAfterValue, skipANumber |
| 210 |
} |
| 211 |
case '0': |
| 212 |
if c := char(b, cursor+1); c == '.' || c == 'e' || c == 'E' { |
| 213 |
return cursor, level, objects, resumeAfterValue, skipANumber |
| 214 |
} |
| 215 |
cursor++ |
| 216 |
case '-': |
| 217 |
return cursor, level, objects, resumeAfterValue, skipANumber |
| 218 |
case 't': |
| 219 |
if char(b, cursor+1) != 'r' || char(b, cursor+2) != 'u' || char(b, cursor+3) != 'e' { |
| 220 |
return cursor, level, objects, resumeValue, skipInvalid |
| 221 |
} |
| 222 |
cursor += 4 |
| 223 |
case 'f': |
| 224 |
if char(b, cursor+1) != 'a' || char(b, cursor+2) != 'l' || char(b, cursor+3) != 's' || char(b, cursor+4) != 'e' { |
| 225 |
return cursor, level, objects, resumeValue, skipInvalid |
| 226 |
} |
| 227 |
cursor += 5 |
| 228 |
case 'n': |
| 229 |
if char(b, cursor+1) != 'u' || char(b, cursor+2) != 'l' || char(b, cursor+3) != 'l' { |
| 230 |
return cursor, level, objects, resumeValue, skipInvalid |
| 231 |
} |
| 232 |
cursor += 4 |
| 233 |
default: |
| 234 |
return cursor, level, objects, resumeValue, skipInvalid |
| 235 |
} |
| 236 |
afterValue: |
| 237 |
// a value here is in an object or an array: the value which skipFast starts at is one of them, and the end of |
| 238 |
// the outermost is returned at closed |
| 239 |
if c = char(b, cursor); c <= ' ' { |
| 240 |
for isWhiteSpace[c] { |
| 241 |
cursor++ |
| 242 |
c = char(b, cursor) |
| 243 |
} |
| 244 |
} |
| 245 |
if objects&(1<<uint(level-1)) != 0 { |
| 246 |
switch c { |
| 247 |
case ',': |
| 248 |
cursor++ |
| 249 |
if c = char(b, cursor); c <= ' ' { |
| 250 |
for isWhiteSpace[c] { |
| 251 |
cursor++ |
| 252 |
c = char(b, cursor) |
| 253 |
} |
| 254 |
} |
| 255 |
goto key |
| 256 |
case '}': |
| 257 |
cursor++ |
| 258 |
goto closed |
| 259 |
} |
| 260 |
return cursor, level, objects, resumeAfterValue, skipInvalid |
| 261 |
} |
| 262 |
switch c { |
| 263 |
case ',': |
| 264 |
cursor++ |
| 265 |
goto value |
| 266 |
case ']': |
| 267 |
cursor++ |
| 268 |
goto closed |
| 269 |
} |
| 270 |
return cursor, level, objects, resumeAfterValue, skipInvalid |
| 271 |
key: |
| 272 |
if c != '"' { |
| 273 |
return cursor, level, objects, resumeKey, skipInvalid |
| 274 |
} |
| 275 |
// a key of plain bytes, as the strings of the values |
| 276 |
for c := cursor + 1; c+8 <= length; c += 8 { |
| 277 |
if special := keyEndBytes(load64(buf, c)); special != 0 { |
| 278 |
if c += int64(bits.TrailingZeros64(special) / 8); char(b, c) == '"' { |
| 279 |
cursor = c + 1 |
| 280 |
goto afterKey |
| 281 |
} |
| 282 |
break |
| 283 |
} |
| 284 |
} |
| 285 |
return cursor, level, objects, resumeAfterKey, skipAString |
| 286 |
afterKey: |
| 287 |
if c = char(b, cursor); c <= ' ' { |
| 288 |
for isWhiteSpace[c] { |
| 289 |
cursor++ |
| 290 |
c = char(b, cursor) |
| 291 |
} |
| 292 |
} |
| 293 |
if c != ':' { |
| 294 |
return cursor, level, objects, resumeAfterKey, skipInvalid |
| 295 |
} |
| 296 |
cursor++ |
| 297 |
// the single space after the colon, which most objects have, is skipped before the loop over the others |
| 298 |
if char(b, cursor) == ' ' { |
| 299 |
cursor++ |
| 300 |
} |
| 301 |
goto value |
| 302 |
closed: |
| 303 |
// the end of an object or an array, which cursor is after |
| 304 |
if level--; level == 0 { |
| 305 |
return cursor, level, objects, resumeAfterValue, skipDone |
| 306 |
} |
| 307 |
goto afterValue |
| 308 |
} |
| 309 |
|
| 310 |
// skipGrammar skips from cursor, in state, the values of the objects and the arrays which are open, level of them |
| 311 |
// the innermost of which is an object, up to the end of the outermost of them, or the value at cursor if none is |
| 312 |
// open, and returns the position after it. |
| 313 |
// |
| 314 |
// The kinds of the open values are the bits of a word, set for an object, for the first 64 levels, and of the |
| 315 |
// words of deeper, which are allocated as the nesting gets deeper: the loop keeps them in its registers. |
| 316 |
// |
| 317 |
//nolint:maintidx // one loop of gotos without calls, whose state stays in the registers |
| 318 |
func skipGrammar(buf []byte, cursor, nesting, level int64, objects uint64, state skipState) (int64, error) { |
| 319 |
maxLevel := maxDecodeNestingDepth - nesting |
| 320 |
length := int64(len(buf)) |
| 321 |
// the bytes are read without the checks of the bounds: every loop stops at the nul byte at the end of the |
| 322 |
// buffer, and a literal is compared byte by byte up to the first byte which differs |
| 323 |
b := (*sliceHeader)(unsafe.Pointer(&buf)).data |
| 324 |
var deeper []uint64 |
| 325 |
if state == skipAfterValue { |
| 326 |
goto afterValue |
| 327 |
} |
| 328 |
value: |
| 329 |
for isWhiteSpace[char(b, cursor)] { |
| 330 |
cursor++ |
| 331 |
} |
| 332 |
switch c := char(b, cursor); c { |
| 333 |
case '{', '[': |
| 334 |
if level+1 > maxLevel { |
| 335 |
return 0, depthSyntaxError(buf, cursor) |
| 336 |
} |
| 337 |
if level < 64 { |
| 338 |
if c == '{' { |
| 339 |
objects |= 1 << uint(level) |
| 340 |
} else { |
| 341 |
objects &^= 1 << uint(level) |
| 342 |
} |
| 343 |
} else { |
| 344 |
deeper = pushDeeper(deeper, level, c == '{') |
| 345 |
} |
| 346 |
level++ |
| 347 |
cursor++ |
| 348 |
for isWhiteSpace[char(b, cursor)] { |
| 349 |
cursor++ |
| 350 |
} |
| 351 |
if c == '{' { |
| 352 |
if char(b, cursor) == '}' { |
| 353 |
cursor++ |
| 354 |
level-- |
| 355 |
goto afterValue |
| 356 |
} |
| 357 |
goto key |
| 358 |
} |
| 359 |
if char(b, cursor) == ']' { |
| 360 |
cursor++ |
| 361 |
level-- |
| 362 |
goto afterValue |
| 363 |
} |
| 364 |
goto value |
| 365 |
case '"': |
| 366 |
// a string of plain bytes up to its quote without a call, eight bytes at a time, and any other by |
| 367 |
// skipString, which checks its escapes |
| 368 |
for c := cursor + 1; c+8 <= length; c += 8 { |
| 369 |
if special := keyEndBytes(load64(buf, c)); special != 0 { |
| 370 |
if c += int64(bits.TrailingZeros64(special) / 8); char(b, c) == '"' { |
| 371 |
cursor = c + 1 |
| 372 |
goto afterValue |
| 373 |
} |
| 374 |
break |
| 375 |
} |
| 376 |
} |
| 377 |
end, err := skipString(buf, cursor) |
| 378 |
if err != nil { |
| 379 |
return 0, stringError(buf, cursor, err) |
| 380 |
} |
| 381 |
cursor = end |
| 382 |
case '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 383 |
// an integer, which most numbers are, without a call |
| 384 |
start := cursor |
| 385 |
cursor++ |
| 386 |
for char(b, cursor)-'0' <= 9 { |
| 387 |
cursor++ |
| 388 |
} |
| 389 |
if c := char(b, cursor); c == '.' || c == 'e' || c == 'E' { |
| 390 |
end, err := skipNumberRest(buf, start, cursor) |
| 391 |
if err != nil { |
| 392 |
return 0, err |
| 393 |
} |
| 394 |
cursor = end |
| 395 |
} |
| 396 |
case '0': |
| 397 |
start := cursor |
| 398 |
cursor++ |
| 399 |
if c := char(b, cursor); c == '.' || c == 'e' || c == 'E' { |
| 400 |
end, err := skipNumberRest(buf, start, cursor) |
| 401 |
if err != nil { |
| 402 |
return 0, err |
| 403 |
} |
| 404 |
cursor = end |
| 405 |
} |
| 406 |
case '-': |
| 407 |
end, err := skipNumber(buf, cursor) |
| 408 |
if err != nil { |
| 409 |
return 0, err |
| 410 |
} |
| 411 |
cursor = end |
| 412 |
case 't': |
| 413 |
if char(b, cursor+1) != 'r' || char(b, cursor+2) != 'u' || char(b, cursor+3) != 'e' { |
| 414 |
return 0, literalSyntaxError(buf, cursor, "true") |
| 415 |
} |
| 416 |
cursor += 4 |
| 417 |
case 'f': |
| 418 |
if char(b, cursor+1) != 'a' || char(b, cursor+2) != 'l' || char(b, cursor+3) != 's' || char(b, cursor+4) != 'e' { |
| 419 |
return 0, literalSyntaxError(buf, cursor, "false") |
| 420 |
} |
| 421 |
cursor += 5 |
| 422 |
case 'n': |
| 423 |
if char(b, cursor+1) != 'u' || char(b, cursor+2) != 'l' || char(b, cursor+3) != 'l' { |
| 424 |
return 0, literalSyntaxError(buf, cursor, "null") |
| 425 |
} |
| 426 |
cursor += 4 |
| 427 |
default: |
| 428 |
return 0, syntaxErrorAt(buf, cursor, whereValue) |
| 429 |
} |
| 430 |
afterValue: |
| 431 |
if level == 0 { |
| 432 |
return cursor, nil |
| 433 |
} |
| 434 |
for isWhiteSpace[char(b, cursor)] { |
| 435 |
cursor++ |
| 436 |
} |
| 437 |
if inObject(objects, deeper, level-1) { |
| 438 |
switch char(b, cursor) { |
| 439 |
case ',': |
| 440 |
cursor++ |
| 441 |
for isWhiteSpace[char(b, cursor)] { |
| 442 |
cursor++ |
| 443 |
} |
| 444 |
goto key |
| 445 |
case '}': |
| 446 |
cursor++ |
| 447 |
level-- |
| 448 |
goto afterValue |
| 449 |
} |
| 450 |
return 0, syntaxErrorAt(buf, cursor, whereAfterPair) |
| 451 |
} |
| 452 |
switch char(b, cursor) { |
| 453 |
case ',': |
| 454 |
cursor++ |
| 455 |
goto value |
| 456 |
case ']': |
| 457 |
cursor++ |
| 458 |
level-- |
| 459 |
goto afterValue |
| 460 |
} |
| 461 |
return 0, syntaxErrorAt(buf, cursor, whereAfterElement) |
| 462 |
key: |
| 463 |
if char(b, cursor) != '"' { |
| 464 |
return 0, syntaxErrorAt(buf, cursor, whereKey) |
| 465 |
} |
| 466 |
// a key of plain bytes without a call, as the strings of the values |
| 467 |
for c := cursor + 1; c+8 <= length; c += 8 { |
| 468 |
if special := keyEndBytes(load64(buf, c)); special != 0 { |
| 469 |
if c += int64(bits.TrailingZeros64(special) / 8); char(b, c) == '"' { |
| 470 |
cursor = c + 1 |
| 471 |
goto afterKey |
| 472 |
} |
| 473 |
break |
| 474 |
} |
| 475 |
} |
| 476 |
{ |
| 477 |
end, err := skipString(buf, cursor) |
| 478 |
if err != nil { |
| 479 |
return 0, stringError(buf, cursor, err) |
| 480 |
} |
| 481 |
cursor = end |
| 482 |
} |
| 483 |
afterKey: |
| 484 |
for isWhiteSpace[char(b, cursor)] { |
| 485 |
cursor++ |
| 486 |
} |
| 487 |
if char(b, cursor) != ':' { |
| 488 |
return 0, syntaxErrorAt(buf, cursor, whereAfterKey) |
| 489 |
} |
| 490 |
cursor++ |
| 491 |
goto value |
| 492 |
} |
| 493 |
|
| 494 |
// inObject reports whether the value open at level is an object. |
| 495 |
func inObject(objects uint64, deeper []uint64, level int64) bool { |
| 496 |
if level < 64 { |
| 497 |
return objects&(1<<uint(level)) != 0 |
| 498 |
} |
| 499 |
return deeper[(level-64)/64]&(1<<uint((level-64)%64)) != 0 |
| 500 |
} |
| 501 |
|
| 502 |
// pushDeeper sets the kind of the value open at level, which is 64 or deeper, in deeper, which it returns. |
| 503 |
func pushDeeper(deeper []uint64, level int64, object bool) []uint64 { |
| 504 |
i, bit := (level-64)/64, uint((level-64)%64) |
| 505 |
for int64(len(deeper)) <= i { |
| 506 |
deeper = append(deeper, 0) |
| 507 |
} |
| 508 |
if object { |
| 509 |
deeper[i] |= 1 << bit |
| 510 |
} else { |
| 511 |
deeper[i] &^= 1 << bit |
| 512 |
} |
| 513 |
return deeper |
| 514 |
} |
| 515 |
|
| 516 |
// skipNumber returns the position after the number at cursor, which it checks by the grammar of JSON. |
| 517 |
func skipNumber(buf []byte, cursor int64) (int64, error) { |
| 518 |
c := cursor |
| 519 |
if buf[c] == '-' { |
| 520 |
c++ |
| 521 |
} |
| 522 |
switch { |
| 523 |
case buf[c] == '0': |
| 524 |
c++ |
| 525 |
case buf[c]-'1' <= 8: |
| 526 |
c = skipDigits(buf, c+1) |
| 527 |
default: |
| 528 |
return 0, numberSyntaxError(buf, c, cursor, whereNumber) |
| 529 |
} |
| 530 |
return skipNumberRest(buf, cursor, c) |
| 531 |
} |
| 532 |
|
| 533 |
// skipNumberRest returns the position after the fraction and the exponent of the number which starts at start and |
| 534 |
// whose integer ends at c, which it checks by the grammar of JSON. |
| 535 |
func skipNumberRest(buf []byte, start, c int64) (int64, error) { |
| 536 |
if buf[c] == '.' { |
| 537 |
c++ |
| 538 |
if buf[c]-'0' > 9 { |
| 539 |
return 0, numberSyntaxError(buf, c, start, whereFraction) |
| 540 |
} |
| 541 |
c = skipDigits(buf, c) |
| 542 |
} |
| 543 |
if buf[c] == 'e' || buf[c] == 'E' { |
| 544 |
c++ |
| 545 |
if buf[c] == '+' || buf[c] == '-' { |
| 546 |
c++ |
| 547 |
} |
| 548 |
if buf[c]-'0' > 9 { |
| 549 |
return 0, numberSyntaxError(buf, c, start, whereExponent) |
| 550 |
} |
| 551 |
c = skipDigits(buf, c) |
| 552 |
} |
| 553 |
return c, nil |
| 554 |
} |
| 555 |
|
| 556 |
// stringError returns the syntax error of the string at cursor, which skipString failed with err, as encoding/json |
| 557 |
// has it. |
| 558 |
func stringError(buf []byte, cursor int64, err error) error { |
| 559 |
if serr := stringSyntaxError(buf, cursor); serr != nil { |
| 560 |
return serr |
| 561 |
} |
| 562 |
return err |
| 563 |
} |
| 564 |
|
| 565 |
// InputSyntaxError returns the first syntax error of the input of the decoding, as encoding/json reports it, or |
| 566 |
// nil if the input is valid: the decoders report a syntax error as they meet it, which this reports again as |
| 567 |
// encoding/json does, which checks the whole input first. The input is copied again, since the decoding writes |
| 568 |
// its buffer, as the escapes of the keys are decoded in it. |
| 569 |
func (ctx *RuntimeContext) InputSyntaxError() error { |
| 570 |
return inputSyntaxError(NewInput(ctx.origin)) |
| 571 |
} |
| 572 |
|
| 573 |
// inputSyntaxError returns the first syntax error of buf, the input followed by the nul byte, or nil. |
| 574 |
func inputSyntaxError(buf []byte) error { |
| 575 |
end, err := skipValue(buf, skipWhiteSpace(buf, 0), 0) |
| 576 |
if err != nil { |
| 577 |
return err |
| 578 |
} |
| 579 |
return ValidateEnd(buf, end) |
| 580 |
} |
| 581 |
|
| 582 |
// Valid reports whether buf, the input followed by the nul byte, is a JSON value, by the grammar which the values |
| 583 |
// which are not decoded are skipped by. |
| 584 |
func Valid(buf []byte) bool { |
| 585 |
end, err := skipValue(buf, skipWhiteSpace(buf, 0), 0) |
| 586 |
return err == nil && skipWhiteSpace(buf, end) == int64(len(buf))-1 |
| 587 |
} |
| 588 |
|
| 589 |
// ValidateEnd returns the syntax error of what follows the value which ends at cursor in buf, the input followed by |
| 590 |
// the nul byte: white spaces only may follow it, up to the end of the input. A nul byte in the input is a byte which |
| 591 |
// the grammar doesn't have there, as encoding/json has it. |
| 592 |
func ValidateEnd(buf []byte, cursor int64) error { |
| 593 |
cursor = skipWhiteSpace(buf, cursor) |
| 594 |
if cursor == int64(len(buf))-1 { |
| 595 |
return nil |
| 596 |
} |
| 597 |
return syntaxErrorAt(buf, cursor, whereAfterTop) |
| 598 |
} |