Skip to content

Commit

Permalink
Optimize string parsing using a rope
Browse files Browse the repository at this point in the history
  • Loading branch information
appgurueu committed Dec 17, 2022
1 parent d26f748 commit cb4bc74
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ end


local function parse_string(str, i)
local res = ""
local res = {}
local j = i + 1
local k = j

Expand All @@ -242,32 +242,33 @@ local function parse_string(str, i)
decode_error(str, j, "control character in string")

elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
res[#res + 1] = str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
res[#res + 1] = parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
res[#res + 1] = escape_char_map_inv[c]
end
k = j + 1

elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
res[#res + 1] = str:sub(k, j - 1)
return table.concat(res), j + 1
end

j = j + 1
end

decode_error(str, i, "expected closing quote for string")
return table.concat(res)
end


Expand Down

0 comments on commit cb4bc74

Please sign in to comment.