Skip to content

Commit b33485d

Browse files
committed
Optimize string parsing using a rope
1 parent 452449d commit b33485d

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

json.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ end
231231

232232

233233
local function parse_string(str, i)
234-
local res = ""
234+
local res = {}
235235
local j = i + 1
236236
local k = j
237237

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

244244
elseif x == 92 then -- `\`: Escape
245-
res = res .. str:sub(k, j - 1)
245+
res[#res + 1] = str:sub(k, j - 1)
246246
j = j + 1
247247
local c = str:sub(j, j)
248248
if c == "u" then
249249
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
250250
or str:match("^%x%x%x%x", j + 1)
251251
or decode_error(str, j - 1, "invalid unicode escape in string")
252-
res = res .. parse_unicode_escape(hex)
252+
res[#res + 1] = parse_unicode_escape(hex)
253253
j = j + #hex
254254
else
255255
if not escape_chars[c] then
256256
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
257257
end
258-
res = res .. escape_char_map_inv[c]
258+
res[#res + 1] = escape_char_map_inv[c]
259259
end
260260
k = j + 1
261261

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

267267
j = j + 1
268268
end
269269

270270
decode_error(str, i, "expected closing quote for string")
271+
return table.concat(res)
271272
end
272273

273274

0 commit comments

Comments
 (0)