mirror of https://github.com/rxi/json.lua.git
Optimize code
Took advice from @Dnsls and @Faserr to optimize code from #34. https://github.com/rxi/json.lua/issues/34 https://github.com/rxi/json.lua/issues/34#issue-900464723 https://github.com/rxi/json.lua/issues/34#issuecomment-1060329470pull/44/head
parent
cd2b736e8c
commit
917e8a08c2
12
json.lua
12
json.lua
|
@ -22,7 +22,7 @@
|
||||||
-- SOFTWARE.
|
-- SOFTWARE.
|
||||||
--
|
--
|
||||||
|
|
||||||
local json = { _version = "0.1.2" }
|
local json = { _version = "0.1.2fix" }
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Encode
|
-- Encode
|
||||||
|
@ -233,7 +233,7 @@ end
|
||||||
|
|
||||||
|
|
||||||
local function parse_string(str, i)
|
local function parse_string(str, i)
|
||||||
local res = ""
|
local res = {}
|
||||||
local j = i + 1
|
local j = i + 1
|
||||||
local k = j
|
local k = j
|
||||||
|
|
||||||
|
@ -244,25 +244,25 @@ local function parse_string(str, i)
|
||||||
decode_error(str, j, "control character in string")
|
decode_error(str, j, "control character in string")
|
||||||
|
|
||||||
elseif x == 92 then -- `\`: Escape
|
elseif x == 92 then -- `\`: Escape
|
||||||
res = res .. str:sub(k, j - 1)
|
res[#res + 1] = str:sub(k, j - 1)
|
||||||
j = j + 1
|
j = j + 1
|
||||||
local c = str:sub(j, j)
|
local c = str:sub(j, j)
|
||||||
if c == "u" then
|
if c == "u" then
|
||||||
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
|
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 str:match("^%x%x%x%x", j + 1)
|
||||||
or decode_error(str, j - 1, "invalid unicode escape in string")
|
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
|
j = j + #hex
|
||||||
else
|
else
|
||||||
if not escape_chars[c] then
|
if not escape_chars[c] then
|
||||||
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
|
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
|
||||||
end
|
end
|
||||||
res = res .. escape_char_map_inv[c]
|
res[#res + 1] = escape_char_map_inv[c]
|
||||||
end
|
end
|
||||||
k = j + 1
|
k = j + 1
|
||||||
|
|
||||||
elseif x == 34 then -- `"`: End of string
|
elseif x == 34 then -- `"`: End of string
|
||||||
res = res .. str:sub(k, j - 1)
|
res[#res + 1] = str:sub(k, j - 1)
|
||||||
return res, j + 1
|
return res, j + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue