Merge pull request #737 from aleksijuvani/string-escape
Add string.escapepattern function
This commit is contained in:
commit
00f6f0547b
@ -91,3 +91,13 @@
|
||||
return self .. "s"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
---
|
||||
-- Returns the string escaped for Lua patterns.
|
||||
---
|
||||
|
||||
function string.escapepattern(s)
|
||||
return s:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%0")
|
||||
end
|
||||
|
@ -70,3 +70,31 @@
|
||||
function suite.startswith_OnEmptyNeedle()
|
||||
test.istrue(string.startswith("Abcdef", ""))
|
||||
end
|
||||
|
||||
|
||||
|
||||
--
|
||||
-- string.escapepattern() tests
|
||||
--
|
||||
|
||||
function suite.escapepattern_escapes()
|
||||
test.isequal("boost_filesystem%-vc140%.1%.61%.0%.0", string.escapepattern("boost_filesystem-vc140.1.61.0.0"))
|
||||
test.isequal("footage/down/temp/cars_%[100%]_upper/cars_%[100%]_upper%.exr", string.escapepattern("footage/down/temp/cars_[100]_upper/cars_[100]_upper.exr"))
|
||||
end
|
||||
|
||||
function suite.escapepattern_doesntEscape()
|
||||
local s = '<something foo="bar" />'
|
||||
test.isequal(s, s:escapepattern())
|
||||
|
||||
s = 'lorem ipsum dolor sit amet'
|
||||
test.isequal(s, s:escapepattern())
|
||||
|
||||
s = 'forward/slashes/foo/bar'
|
||||
test.isequal(s, s:escapepattern())
|
||||
|
||||
s = '\\back\\slashes'
|
||||
test.isequal(s, s:escapepattern())
|
||||
|
||||
s = 'new\nlines'
|
||||
test.isequal(s, s:escapepattern())
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user