⚡️ Speed up function fix_unescaped_quotes_in_json by 106% (#1891)
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com> Co-authored-by: Shuchang Zheng <shu@skyvern.com>
This commit is contained in:
@@ -122,12 +122,10 @@ def fix_unescaped_quotes_in_json(json_string: str) -> str:
|
|||||||
str: The JSON-like string with unescaped quotation marks within strings.
|
str: The JSON-like string with unescaped quotation marks within strings.
|
||||||
"""
|
"""
|
||||||
escape_char = "\\"
|
escape_char = "\\"
|
||||||
# Indices to add the escape character to. Since we're processing the string from left to right, we need to sort
|
|
||||||
# the indices in descending order to avoid index shifting.
|
|
||||||
indices_to_add_escape_char = []
|
|
||||||
in_string = False
|
in_string = False
|
||||||
escape = False
|
escape = False
|
||||||
json_structure_chars = {",", ":", "}", "]", "{", "["}
|
json_structure_chars = {",", ":", "}", "]", "{", "["}
|
||||||
|
result = []
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(json_string):
|
while i < len(json_string):
|
||||||
@@ -146,23 +144,22 @@ def fix_unescaped_quotes_in_json(json_string: str) -> str:
|
|||||||
in_string = False
|
in_string = False
|
||||||
else:
|
else:
|
||||||
# If the next character is not a JSON structure character, the quote is part of the string
|
# If the next character is not a JSON structure character, the quote is part of the string
|
||||||
# Update the indices to add the escape character with the current index
|
# Add the escape character before the quote
|
||||||
indices_to_add_escape_char.append(i)
|
result.append(escape_char)
|
||||||
else:
|
else:
|
||||||
# Start of the JSON string
|
# Start of the JSON string
|
||||||
in_string = True
|
in_string = True
|
||||||
else:
|
else:
|
||||||
escape = False
|
escape = False
|
||||||
|
|
||||||
|
# Append the current character to the result
|
||||||
|
result.append(char)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# Sort the indices in descending order to avoid index shifting then add the escape character to the string
|
if len(result) != len(json_string):
|
||||||
if indices_to_add_escape_char:
|
|
||||||
LOG.warning("Unescaped quotes found in JSON string. Adding escape character to fix the issue.")
|
LOG.warning("Unescaped quotes found in JSON string. Adding escape character to fix the issue.")
|
||||||
indices_to_add_escape_char.sort(reverse=True)
|
|
||||||
for index in indices_to_add_escape_char:
|
|
||||||
json_string = json_string[:index] + escape_char + json_string[index:]
|
|
||||||
|
|
||||||
return json_string
|
return "".join(result)
|
||||||
|
|
||||||
|
|
||||||
def fix_and_parse_json_string(json_string: str) -> dict[str, Any]:
|
def fix_and_parse_json_string(json_string: str) -> dict[str, Any]:
|
||||||
|
|||||||
Reference in New Issue
Block a user