Skip to content

Instantly share code, notes, and snippets.

@dndx
Last active August 20, 2019 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dndx/623a29aea034a90b005d6c153155e661 to your computer and use it in GitHub Desktop.
Save dndx/623a29aea034a90b005d6c153155e661 to your computer and use it in GitHub Desktop.
Split by comma and space
local connection = "keep-alive"
local i = 0
local ngx_re = require "ngx.re"
local re_split = ngx_re.split
local lower = string.lower
local gsub = string.gsub
local find = string.find
local sub = string.sub
local function csv_iterator(s, b)
if b == -1 then
return
end
local e = find(s, ",", b, true)
local v
local l
if e then
if e == b then
return csv_iterator(s, b + 1) -- empty string
end
v = sub(s, b, e - 1)
l = e - b
b = e + 1
else
if b > 1 then
v = sub(s, b)
else
v = s
end
l = #v
b = -1 -- end iteration
end
if l == 1 and (v == " " or v == ",") then
return csv_iterator(s, b)
end
if sub(v, 1, 1) == " " then
v = gsub(v, "^%s+", "")
end
if sub(v, -1) == " " then
v = gsub(v, "%s+$", "")
end
if v == "" then
return csv_iterator(s, b)
end
return b, lower(v)
end
local function csv(s)
if type(s) ~= "string" or s == "" then
return csv_iterator, s, -1
end
return csv_iterator, s, 1
end
for i = 1, 50000000 do
for _, header_name in csv(connection) do
-- print(header_name)
-- some of these are already handled by the proxy module,
-- proxy-authorization being an exception that is handled
-- below with special semantics.
if header_name ~= "close" and
header_name ~= "upgrade" and
header_name ~= "keep-alive" and
header_name ~= "proxy-authorization" then
i = i + 1
end
end
end
print("total cleared: ", i)
local connection = "keep-alive"
local i = 0
local ngx_re = require "ngx.re"
local re_split = ngx_re.split
local lower = string.lower
local gsub = string.gsub
local find = string.find
local sub = string.sub
local csv_iterator do
local COMMA, SPACE = (", "):byte(1, -1)
function csv_iterator(s, b)
b = b or 1
local first, last
for i = b, #s do
local c = s:byte(i)
if c == COMMA then
if first and last then
return i+1, s:sub(first, last)
else
return i+1, ''
end
end
if c ~= SPACE then
if not first then first = i end
last = i
end
end
if first then
return #s+1, s:sub(first, last)
end
end
end
local function csv(s)
if type(s) ~= "string" or s == "" then
return csv_iterator, s, -1
end
return csv_iterator, s, 1
end
for i = 1, 50000000 do
for _, header_name in csv(connection) do
-- print(header_name)
-- some of these are already handled by the proxy module,
-- proxy-authorization being an exception that is handled
-- below with special semantics.
if header_name ~= "close" and
header_name ~= "upgrade" and
header_name ~= "keep-alive" and
header_name ~= "proxy-authorization" then
i = i + 1
end
end
end
print("total cleared: ", i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment