summaryrefslogtreecommitdiff
path: root/xmake/core/base/string.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2018-08-14 22:38:41 +0800
committerruki <[email protected]>2018-08-14 09:52:06 +0800
commite5011cbbe2717c0fd07c4da2394f6ced07a3f6cc (patch)
treec0259e6740ef07adb0d94a21656e5fca4a766cf8 /xmake/core/base/string.lua
parent96de3aa701d92ceb2162d519d3ea9679c32f655a (diff)
case-insensitive for windows path
Diffstat (limited to 'xmake/core/base/string.lua')
-rw-r--r--xmake/core/base/string.lua60
1 files changed, 60 insertions, 0 deletions
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index 51dfb86ef..abf92e945 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -151,5 +151,65 @@ function string.tryformat(format, ...)
end
end
+-- case-insensitive pattern-matching
+--
+-- print(("src/dadasd.C"):match(string.ipattern("sR[cd]/.*%.c", true)))
+-- print(("src/dadasd.C"):match(string.ipattern("src/.*%.c", true)))
+--
+-- print(string.ipattern("sR[cd]/.*%.c"))
+-- [sS][rR][cd]/.*%.[cC]
+--
+-- print(string.ipattern("sR[cd]/.*%.c", true))
+-- [sS][rR][cCdD]/.*%.[cC]
+--
+function string.ipattern(pattern, brackets)
+ local tmp = {}
+ local i = 1
+ while i <= #pattern do
+
+ -- get current charactor
+ local char = pattern:sub(i, i)
+
+ -- escape?
+ if char == '%' then
+ tmp[#tmp + 1] = char
+ i = i + 1
+ char = pattern:sub(i,i)
+ tmp[#tmp + 1] = char
+
+ -- '%bxy'? add next 2 chars
+ if char == 'b' then
+ tmp[#tmp + 1] = pattern:sub(i + 1, i + 2)
+ i = i + 2
+ end
+ -- brackets?
+ elseif char == '[' then
+ tmp[#tmp + 1] = char
+ i = i + 1
+ while i <= #pattern do
+ char = pattern:sub(i, i)
+ if char == '%' then
+ tmp[#tmp + 1] = char
+ tmp[#tmp + 1] = pattern:sub(i + 1, i + 1)
+ i = i + 1
+ elseif char:match("%a") then
+ tmp[#tmp + 1] = not brackets and char or char:lower() .. char:upper()
+ else
+ tmp[#tmp + 1] = char
+ end
+ if char == ']' then break end
+ i = i + 1
+ end
+ -- letter, [aA]
+ elseif char:match("%a") then
+ tmp[#tmp + 1] = '[' .. char:lower() .. char:upper() .. ']'
+ else
+ tmp[#tmp + 1] = char
+ end
+ i = i + 1
+ end
+ return table.concat(tmp)
+end
+
-- return module: string
return string