Skip to content

Commit

Permalink
Merge pull request rstudio#20 from rstudio/string-replace
Browse files Browse the repository at this point in the history
Add safe string replace function
  • Loading branch information
kippandrew committed Dec 11, 2015
2 parents 506ec16 + 48310e6 commit 75dd6e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lua/src/lib/library.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local M = { }
local escape_pattern
do
local _obj_0 = require("lapis.util")
escape_pattern = _obj_0.escape_pattern
end
escape_pattern = require("lapis.util").escape_pattern
M.log = function(msg)
return ngx.log(ngx.NOTICE, inspect(msg))
end
Expand All @@ -20,6 +17,11 @@ M.split = function(str, delim)
end
return _accum_0
end
M.replace = function(str, what, sub)
what = string.gsub(what, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
sub = string.gsub(sub, "[%%]", "%%%%")
return string.gsub(str, what, sub)
end
M.set = function(list)
local set = { }
for _, l in ipairs(list) do
Expand Down
5 changes: 5 additions & 0 deletions src/lib/library.moon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ M.split = (str, delim using nil) ->
str ..= delim
[part for part in str\gmatch "(.-)" .. escape_pattern delim]

M.replace = (str, what, sub) ->
what = string.gsub(what, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
sub = string.gsub(sub, "[%%]", "%%%%") -- escape replacement
return string.gsub(str, what, sub)

M.set = (list) ->
set = {}
for _, l in ipairs(list) do
Expand Down

0 comments on commit 75dd6e9

Please sign in to comment.