Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.156.3 (2026-11-27)

- **Faster: the depth check before a parse reads a dense file in about half the time of 0.156.1.** 0.156.1 made the check linear by stepping through every bracket, quote and backslash in Python, which on many small objects (50,000 of them, 2.2 MB) took 129 ms where 0.156.0 took 54. The strings are now cut out by one pattern that never backtracks, and the brackets left are counted as a running sum: 61 ms on the same file, and still linear on a string left open and full of escaped quotes (17 ms at 1 MB). Its answers were held to 0.156.1's on 500,000 random texts; none differed.

## 0.156.2 (2026-11-27)

- **Fixed: `calendar.format_duration` no longer raises on a `step_name` that is not a string.** Such a value reads as absent and the duration is worded in the grain, the calendar profile's rule for an optional key of the wrong kind (a `step_name` of `5` raised `TypeError`).
Expand Down
37 changes: 12 additions & 25 deletions src/onlyworlds/_outside_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import math
import re
import sys
from itertools import accumulate
from typing import Any

#: The deepest nesting read: a value at the top is depth 0, an object or array there depth 1, what it holds
Expand Down Expand Up @@ -51,41 +52,27 @@ def _int(text: str) -> int:
return int(text)


#: The characters the depth scan reads: the four brackets, the quote and the backslash.
_SPECIAL = re.compile(r'[\[\]{}"\\]')
#: A string as the depth scan reads it: from a quote to the next quote no backslash escapes, or to the end of
#: the text when none closes it (a lone backslash at the very end included). Every quantifier is possessive, so a
#: match never backtracks and every quote outside a string starts one that succeeds: one pass over the text.
_STRING = re.compile(r'"(?:[^"\\]++|\\.?)*+(?:"|\Z)', re.DOTALL)
#: Whatever is not a bracket, once the strings are cut out.
_NOT_BRACKET = re.compile(r"[^\[\]{}]++")
_STEP = {"[": 1, "{": 1, "]": -1, "}": -1}.__getitem__


def _text_too_deep(text: str) -> bool:
"""Whether the text opens an object or array deeper than ``MAX_DEPTH``, counted in the text itself as each
one opens, before any parse (the folder format's rule since serial 148): so each value of a repeated key
counts, and a text past the line is too deep whatever else is wrong with it. Brackets inside a string are
not counted, and a backslash in a string escapes the character after it; a string left open runs to the
end of the text, as the npm SDK's scan reads it. One pass, in time linear in the text's length."""
end of the text, as the npm SDK's scan reads it. In time linear in the text's length: the strings cut out by
one pattern that never backtracks, then the brackets left counted as a running sum."""
# No text can open more levels than it has opening brackets.
if text.count("[") + text.count("{") <= MAX_DEPTH:
return False
depth = 0
in_string = False
escaped_at = -1
for m in _SPECIAL.finditer(text):
i = m.start()
if i == escaped_at:
continue
c = m.group()
if in_string:
if c == "\\":
escaped_at = i + 1
elif c == '"':
in_string = False
elif c == '"':
in_string = True
elif c == "[" or c == "{":
depth += 1
if depth > MAX_DEPTH:
return True
elif c == "]" or c == "}":
depth -= 1
return False
brackets = _NOT_BRACKET.sub("", _STRING.sub("", text))
return max(accumulate(map(_STEP, brackets)), default=0) > MAX_DEPTH


def loads(text: str, **kwargs: Any) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.156.2"
__version__ = "0.156.3"
8 changes: 8 additions & 0 deletions tests/test_outside_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ def test_the_depth_scan_answers_as_the_strings_removed_reading(monkeypatch: pyte
assert _outside_json._text_too_deep(text) == _too_deep_by_strings_removed(text, 3), repr(text)


def test_a_string_left_open_on_a_lone_backslash_runs_to_the_end(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(_outside_json, "MAX_DEPTH", 3)
assert _outside_json._text_too_deep("[[[[" + '"ab\\') is True
assert _outside_json._text_too_deep("[[[" + '"ab\\' + "[[[[") is False
assert _outside_json._text_too_deep("[[[" + '"a\\"' + "[[[[") is False
assert _outside_json._text_too_deep("[[[" + '"a\\\\"' + "[") is True


def test_the_depth_scan_is_linear_on_a_string_left_open_full_of_escaped_quotes() -> None:
import time

Expand Down
Loading