Replaced use of string.sub() + .byte() in parse_string with just .byte()

Provides a noticeable speed improvement on both Lua5.2 and LuaJIT 2.0.2
pull/6/head
rxi 2015-08-19 21:00:40 +01:00
parent 6fdbd28ed9
commit 425c8b3e88
1 changed files with 9 additions and 7 deletions

View File

@ -196,13 +196,14 @@ local function parse_string(str, i, chr)
local has_escape = false local has_escape = false
local last local last
for j = i + 1, #str do for j = i + 1, #str do
local x = str:sub(j, j) local x = str:byte(j)
if x:byte() < 32 then if x < 32 then
decode_error(str, j, "control character in string") decode_error(str, j, "control character in string")
end
elseif last == "\\" then if last == 92 then -- "\\" (escape char)
if x == "u" then if x == 117 then -- "u" (unicode escape sequence)
local hex = str:sub(j + 1, j + 5) local hex = str:sub(j + 1, j + 5)
if not hex:find("%x%x%x%x") then if not hex:find("%x%x%x%x") then
decode_error(str, j, "invalid unicode escape in string") decode_error(str, j, "invalid unicode escape in string")
@ -213,14 +214,15 @@ local function parse_string(str, i, chr)
has_unicode_escape = true has_unicode_escape = true
end end
else else
if not escape_chars[x] then local c = string.char(x)
decode_error(str, j, "invalid escape char '" .. x .. "' in string") if not escape_chars[c] then
decode_error(str, j, "invalid escape char '" .. c .. "' in string")
end end
has_escape = true has_escape = true
end end
last = nil last = nil
elseif x == '"' then elseif x == 34 then -- '"' (end of string)
local s = str:sub(i + 1, j - 1) local s = str:sub(i + 1, j - 1)
if has_surrogate_escape then if has_surrogate_escape then
s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape)