Skip to content

Commit fe1c433

Browse files
committed
add improve string end searching
1 parent a9530f2 commit fe1c433

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

dictdatabase/utils.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,33 @@ def seek_index_through_value_bytes(json_bytes: bytes, index: int) -> int:
5757

5858
# See https://www.json.org/json-en.html for the JSON syntax
5959

60-
in_str, list_depth, dict_depth, i, len_json_bytes = False, 0, 0, index, len(json_bytes)
60+
list_depth, dict_depth, i, len_json_bytes = 0, 0, index, len(json_bytes)
6161

6262
while i < len_json_bytes:
6363
current = json_bytes[i]
6464
# If backslash, skip the next character
6565
if current == byte_codes.BACKSLASH:
6666
i += 1
67-
# If quote, toggle in_str
67+
68+
# Assert: the current character is not escaped with a backslash
69+
6870
elif current == byte_codes.QUOTE:
69-
in_str = not in_str
71+
while True:
72+
i = json_bytes.find(byte_codes.QUOTE, i + 1)
73+
if i == -1:
74+
raise TypeError("Invalid JSON")
75+
76+
j = i - 1
77+
backslash_count = 0
78+
while j >= 0 and json_bytes[j] == byte_codes.BACKSLASH:
79+
backslash_count += 1
80+
j -= 1
81+
if backslash_count % 2 == 0:
82+
break
83+
7084
# Possible exit point where string ends and nesting is zero
71-
if not in_str and list_depth == 0 and dict_depth == 0:
85+
if list_depth == 0 and dict_depth == 0:
7286
return i + 1
73-
# If in string, skip
74-
elif in_str:
75-
pass
7687

7788
# Invariant: Not in_str, not escaped
7889

0 commit comments

Comments
 (0)