Commit 784095e3 authored by François Perrad's avatar François Perrad
Browse files

avoid string methods

parent 1b5c6a24
Pipeline #177185 passed with stages
in 18 seconds
......@@ -8,6 +8,7 @@ local matcher = require(modname).matcher
local pcall = pcall
local require = require
local sub = string.sub
local _ENV = nil
local match
......@@ -40,8 +41,8 @@ if not match then
i = i or 1
j = j or 1
while j <= #patt do
local c = s:sub(i, i)
local p = patt:sub(j, j)
local c = sub(s, i, i)
local p = sub(patt, j, j)
if p == '?' then
if c == '' then
return
......@@ -49,15 +50,15 @@ if not match then
if c == '/' then
return
end
if c == '.' and ((i == 1) or (s:sub(i-1, i-1) == '/')) then
if c == '.' and ((i == 1) or (sub(s, i-1, i-1) == '/')) then
return
end
elseif p == '*' then
if c == '.' and ((i == 1) or (s:sub(i-1, i-1) == '/')) then
if c == '.' and ((i == 1) or (sub(s, i-1, i-1) == '/')) then
return
end
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
while p == '?' or p == '*' do
if c == '/' then
return
......@@ -66,16 +67,16 @@ if not match then
return
end
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
i = i + 1
c = s:sub(i, i)
c = sub(s, i, i)
end
if c == '' then
return s
end
while i <= #s do
i = i + 1
c = s:sub(i, i)
c = sub(s, i, i)
if (p == '[' or c == p) and match(s, patt, i, j) then
return s
end
......@@ -85,15 +86,15 @@ if not match then
if c == '' then
return
end
if c == '.' and ((i == 1) or (s:sub(i-1, i-1) == '/')) then
if c == '.' and ((i == 1) or (sub(s, i-1, i-1) == '/')) then
return
end
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
local compl = (p == '!') or (p == '^')
if compl then
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
end
local matched
while true do
......@@ -102,18 +103,18 @@ if not match then
return -- [ (unterminated)
end
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
if p == '/' and not compl then
return -- [/] can never match
end
if p == '-' and patt:sub(j+1, j+1) ~= ']' then
if p == '-' and sub(patt, j+1, j+1) ~= ']' then
j = j + 1
cend = patt:sub(j, j)
cend = sub(patt, j, j)
if cend == '' then
return
end
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
end
if c >= cstart and c <= cend then
matched = true
......@@ -122,7 +123,7 @@ if not match then
return -- [ (unterminated)
end
j = j + 1
p = patt:sub(j, j)
p = sub(patt, j, j)
end
if compl then
return
......@@ -143,7 +144,7 @@ if not match then
i = i + 1
j = j + 1
end
if s:sub(i, i) == '' then
if sub(s, i, i) == '' then
return s
end
end
......
......@@ -10,6 +10,9 @@ local char = string.char
local error = error
local tonumber = tonumber
local tostring = tostring
local sub = string.sub
local gmatch = string.gmatch
local gsub = string.gsub
local _ENV = nil
local function convert (n)
......@@ -18,9 +21,9 @@ end
local function unescape (s, query)
if query then
s = s:gsub('+', ' ') -- x-www-form-urlencoded
s = gsub(s, '+', ' ') -- x-www-form-urlencoded
end
return (s:gsub('%%(%x%x)', convert))
return (gsub(s, '%%(%x%x)', convert))
end
local legal_op = {
......@@ -78,13 +81,13 @@ local function compile (patt)
local ops = {}
local ip = 1
for start, _end in patt:gmatch("()%b{}()") do
for start, _end in gmatch(patt, "()%b{}()") do
if start > ip then
ops[#ops+1] = { '0', unescape(patt:sub(ip, start - 1)) }
ops[#ops+1] = { '0', unescape(sub(patt, ip, start - 1)) }
end
ip = _end
local s = patt:sub(start + 1, _end - 2)
local c = s:sub(1, 1)
local s = sub(patt, start + 1, _end - 2)
local c = sub(s, 1, 1)
if future_op[c] then
error("operator for future extension found at position " .. tostring(start + 1), 3)
end
......@@ -99,13 +102,13 @@ local function compile (patt)
end
local j = i
while i <= #s do
c = s:sub(i, i)
c = sub(s, i, i)
if not is_word[c] then
if c == ',' then
op[#op+1] = uniq(s:sub(j, i - 1))
op[#op+1] = uniq(sub(s, j, i - 1))
j = i + 1
elseif c == '%' then
if not is_hexa[s:sub(i+1, i+1)] or not is_hexa[s:sub(i+2, i+2)] then
if not is_hexa[sub(s, i+1, i+1)] or not is_hexa[sub(s, i+2, i+2)] then
error("invalid triplet found at position " .. tostring(start + i), 3)
end
i = i + 2
......@@ -118,12 +121,12 @@ local function compile (patt)
i = i + 1
end
if i ~= j then
op[#op+1] = uniq(s:sub(j, i))
op[#op+1] = uniq(sub(s, j, i))
end
ops[#ops+1] = op
end
if #patt > ip then
ops[#ops+1] = { '0', unescape(patt:sub(ip)) }
ops[#ops+1] = { '0', unescape(sub(patt, ip)) }
end
return ops
end
......@@ -161,11 +164,11 @@ local function match (s, ops)
if oper == '0' then
local literal = op[2]
for j = 1, #literal do
local p = literal:sub(j, j)
local c = s:sub(i, i)
local p = sub(literal, j, j)
local c = sub(s, i, i)
i = i + 1
if c == '%' and is_hexa[s:sub(i, i)] and is_hexa[s:sub(i+1, i+1)] then
c = convert(s:sub(i, i+1))
if c == '%' and is_hexa[sub(s, i, i)] and is_hexa[sub(s, i+1, i+1)] then
c = convert(sub(s, i, i+1))
i = i + 2
else
if c == '+' and query then
......@@ -183,7 +186,7 @@ local function match (s, ops)
end
end
elseif oper == ';' or oper == '?' or oper == '&' then
local c = s:sub(i, i)
local c = sub(s, i, i)
if c == oper then
if c == '?' then
query = true
......@@ -197,7 +200,7 @@ local function match (s, ops)
local key
while i <= #s do
while i <= #s do
c = s:sub(i, i)
c = sub(s, i, i)
if c == '=' or c == ';' or end_oper[oper][c] then
break
end
......@@ -206,7 +209,7 @@ local function match (s, ops)
if start == i and end_oper[oper][c] then
break
end
key = s:sub(start, i-1)
key = sub(s, start, i-1)
if not keys[key] then
return
end
......@@ -219,9 +222,9 @@ local function match (s, ops)
i = i + 1
else
while i <= #s do
c = s:sub(i, i)
c = sub(s, i, i)
if c == sep_var[oper] then
capture[key] = unescape(s:sub(start, i-1), query)
capture[key] = unescape(sub(s, start, i-1), query)
i = i + 1
start = i
break
......@@ -238,12 +241,12 @@ local function match (s, ops)
end
if key then
if k == #ops then
capture[key] = unescape(s:sub(start, i-1), query)
capture[key] = unescape(sub(s, start, i-1), query)
else
local sav = query
for j = i, start, -1 do
if inner(j, k+1) then
capture[key] = unescape(s:sub(start, j-1), sav)
capture[key] = unescape(sub(s, start, j-1), sav)
return true
end
end
......@@ -251,7 +254,7 @@ local function match (s, ops)
end
end
else
local c = s:sub(i, i)
local c = sub(s, i, i)
if oper == '1' or oper == '+' or oper == c then
if c == '#' then
query = false
......@@ -263,9 +266,9 @@ local function match (s, ops)
local nvar = 2
local varname = op[nvar]
while i <= #s do
c = s:sub(i, i)
c = sub(s, i, i)
if c == sep_var[oper] then
capture[varname] = unescape(s:sub(start, i-1), query)
capture[varname] = unescape(sub(s, start, i-1), query)
if (oper == '/' or oper == '.') and not op[nvar+1] then
break
else
......@@ -282,12 +285,12 @@ local function match (s, ops)
i = i + 1
end
if k == #ops then
capture[varname] = unescape(s:sub(start, i-1), query)
capture[varname] = unescape(sub(s, start, i-1), query)
else
local sav = query
for j = i, start, -1 do
if inner(j, k+1) then
capture[varname] = unescape(s:sub(start, j-1), sav)
capture[varname] = unescape(sub(s, start, j-1), sav)
return true
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment