-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(router): use
resty.core.utils.str_replace_char()
for dashes (#…
…11721) resty.core.utils.str_replace_char() is a better way to replace - to _. In the future string.lua will gather more functions to simplify tools.utils.lua. See: #10443
- Loading branch information
Showing
5 changed files
with
52 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
local find = string.find | ||
local gsub = string.gsub | ||
|
||
|
||
local _M = {} | ||
|
||
|
||
local replace_dashes | ||
local replace_dashes_lower | ||
do | ||
local str_replace_char | ||
|
||
if ngx and ngx.config.subsystem == "http" then | ||
|
||
-- 1,000,000 iterations with input of "my-header": | ||
-- string.gsub: 81ms | ||
-- ngx.re.gsub: 74ms | ||
-- loop/string.buffer: 28ms | ||
-- str_replace_char: 14ms | ||
str_replace_char = require("resty.core.utils").str_replace_char | ||
|
||
else -- stream subsystem | ||
str_replace_char = function(str, ch, replace) | ||
if not find(str, ch, nil, true) then | ||
return str | ||
end | ||
|
||
return gsub(str, ch, replace) | ||
end | ||
end | ||
|
||
replace_dashes = function(str) | ||
return str_replace_char(str, "-", "_") | ||
end | ||
|
||
replace_dashes_lower = function(str) | ||
return str_replace_char(str:lower(), "-", "_") | ||
end | ||
end | ||
_M.replace_dashes = replace_dashes | ||
_M.replace_dashes_lower = replace_dashes_lower | ||
|
||
|
||
return _M | ||
|