local log = "[2026-05-05 12:34:56] WARN request_id=req-7Fa2 method=GET status=200 path=/api/v1/users size=1532b"
local url = "https://user:pass@example.com:8443/path/to/resource?q=hello+world&page=42#frag-1"
local csv = '"name","age","city"\n"alice",30,stockholm\n"bob, jr.",25,"oslo"'
local function replace(str, what, with)
what = string.gsub(what, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
with = string.gsub(with, "[%%]", "%%%%")
return string.gsub(str, what, with)
end
function string.trim(self)
return self:match("^()%s*$") and "" or self:match("^%s*(.*%S)")
end
local padded = " leading and trailing whitespace "
local function format_valuta(i)
return (tostring(i):reverse():gsub("%d%d%d", "%1."):reverse():gsub("^,", ""))
end
function _bench()
local total = 0
local _, _, ts, level = string.find(log, "%[([%d%-%s:]+)%]%s+(%u+)")
total = total + (ts and #ts or 0) + (level and #level or 0)
for m in string.gmatch(log, "[%s%#%p]-(%a?%d*)%S*") do
total = total + #m
end
local scheme, userinfo, host, port, path, query, frag = string.match(
url,
"^(%w+)://([^@/]*)@?([^:/]+):?(%d*)([^?#]*)%??([^#]*)#?(.*)$"
)
total = total
+ #(scheme or "") + #(userinfo or "") + #(host or "") + #(port or "")
+ #(path or "") + #(query or "") + #(frag or "")
for field in string.gmatch(csv, '"([^"]*)"') do
total = total + #field
end
local out, n = string.gsub(log, "%d+", "<NUM>")
total = total + #out + n
local flipped, k = string.gsub(log, "(%w+)=([^%s]+)", "%2=%1")
total = total + #flipped + k
local r = replace(log, "request_id=req-7Fa2", "request_id=ANON")
total = total + #r
total = total + #padded:trim()
total = total + #format_valuta(1234567890)
return total
end
for i = 1, 1500 do _bench() end
print("strings/patterns: true")