Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tools/string): speed up strip (whitespace) #13168

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions kong/tools/string.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
local pl_stringx = require "pl.stringx"


local type = type
local ipairs = ipairs
local tostring = tostring
local lower = string.lower
local fmt = string.format
local find = string.find
local gsub = string.gsub
local type = type
local ipairs = ipairs
local tostring = tostring
local lower = string.lower
local sub = string.sub
local fmt = string.format
local find = string.find
local gsub = string.gsub
local byte = string.byte


local SPACE_BYTE = byte(" ")
local TAB_BYTE = byte("\t")
local CR_BYTE = byte("\r")


local _M = {}
Expand All @@ -24,16 +31,52 @@ _M.split = pl_stringx.split

--- strips whitespace from a string.
-- @function strip
_M.strip = function(str)
if str == nil then
_M.strip = function(value)
if value == nil then
return ""
end
str = tostring(str)
if #str > 200 then
return str:gsub("^%s+", ""):reverse():gsub("^%s+", ""):reverse()
else
return str:match("^%s*(.-)%s*$")

-- TODO: do we want to operate on non-string values (kept for backward compatibility)?
if type(value) ~= "string" then
value = tostring(value) or ""
end

if value == "" then
return ""
end

local len = #value
local s = 1 -- position of the leftmost non-whitespace char
for i = 1, len do
local b = byte(value, i)
if b == SPACE_BYTE or (b >= TAB_BYTE and b <= CR_BYTE) then
s = s + 1
else
break
end
end

if s > len then
return ""
end

local e = len -- position of the rightmost non-whitespace char
if s < e then
for i = e, 1, -1 do
local b = byte(value, i)
if b == SPACE_BYTE or (b >= TAB_BYTE and b <= CR_BYTE) then
e = e - 1
else
break
end
end
end

if s ~= 1 or e ~= len then
value = sub(value, s, e)
end

return value
end


Expand Down Expand Up @@ -180,4 +223,3 @@ _M.replace_dashes_lower = replace_dashes_lower


return _M

Loading