| 482 | // parses only when every element and separator in it is valid. Returns `i` |
| 483 | // to leave anything else, including errors, to the precise scan. |
| 484 | scanElements(text: string, i: number, out: Array<unknown>): number { |
| 485 | if (this.base + i < this.slow) return i |
| 486 | const { maxDepth, maxElementSize } = this.config |
| 487 | let cut = -1 |
| 488 | let start = i |
| 489 | let close = false |
| 490 | let depth = 0 |
| 491 | let j = i |
| 492 | while (j < text.length) { |
| 493 | const c = text.charCodeAt(j++) |
| 494 | if (c === QUOTE) { |
| 495 | j = this.scanString(text, j, false) |
| 496 | if (j === -1) break |
| 497 | } else if (c === LBRACKET || c === LBRACE) { |
| 498 | if (++depth > maxDepth) break |
| 499 | } else if (c === RBRACKET || c === RBRACE) { |
| 500 | if (depth-- > 0) continue |
| 501 | // Including surrounding whitespace makes this a conservative size check. |
| 502 | if (c === RBRACKET && j - 1 - start <= maxElementSize) { |
| 503 | cut = j - 1 |
| 504 | close = true |
| 505 | } |
| 506 | break |
| 507 | } else if (c === COMMA && depth === 0) { |
| 508 | if (j - 1 - start > maxElementSize) break |
| 509 | cut = j - 1 |
| 510 | start = j |
| 511 | } |
| 512 | } |
| 513 | let values: Array<unknown> | undefined |
| 514 | if (cut !== -1) { |
| 515 | try { |
| 516 | values = JSON.parse("[" + text.slice(i, cut) + "]") |
| 517 | } catch { |
| 518 | this.slow = this.base + cut |
| 519 | } |
| 520 | } |
| 521 | // Rescanning may start before the cached matches, so discard them. |
| 522 | this.quote = -1 |
| 523 | this.backslash = -1 |
| 524 | if (values === undefined) return i |
| 525 | for (let k = 0; k < values.length; k++) out.push(values[k]) |
| 526 | if (close) return this.close(cut) |
| 527 | this.state = ARRAY_VALUE |
| 528 | return cut + 1 |
| 529 | } |
| 530 | |
| 531 | checkSize(size: number): void { |
| 532 | if (size > this.config.maxElementSize) { |