Debian Codegraph @

CorsixTH/Lua/string_extensions.lua

raw
1 --[[ Copyright (c) 2011 Manuel "Roujin" Wolf
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 local TH = require("TH")
22 local lpeg = require("lpeg")
23 local _unwrap = TH.stringProxy._unwrap
24
25 function TH.stringProxy.gsub(str, patt, repl)
26 local str_was_proxy, patt_was_proxy, repl_was_proxy
27 str, str_was_proxy = _unwrap(str)
28 patt, patt_was_proxy = _unwrap(patt)
29 local result
30 local t = type(repl)
31 if t == "table" then
32 result = str:gsub(patt, function(cap)
33 local res, was_proxy = _unwrap(repl[cap])
34 if was_proxy then
35 repl_was_proxy = true
36 end
37 return res
38 end)
39 elseif t == "function" then
40 result = str:gsub(patt, function(...)
41 local res, was_proxy = _unwrap(repl(...))
42 if was_proxy then
43 repl_was_proxy = true
44 end
45 return res
46 end)
47 else
48 repl, repl_was_proxy = _unwrap(repl)
49 result = str:gsub(patt, repl)
50 end
51 if str_was_proxy or patt_was_proxy or repl_was_proxy then
52 result = TH.stringProxy(result, TH.stringProxy.gsub, str, patt, repl)
53 end
54 return result
55 end
56
57 local format_pattern = lpeg.Cs(
58 (
59 (
60 -- %% to %
61 lpeg.P("%%") / "%%" +
62 -- Old-style (%s) and (%d)
63 lpeg.C("%" * lpeg.S("sd") * lpeg.Carg(1)) / function(key, args)
64 local idx = args.idx + 1
65 args.idx = idx
66 local arg, proxy_found = _unwrap(args[idx])
67 local t = type(arg)
68 if (key == "%d" and t == "number") or
69 -- NB: Numbers are allowed for %s as well to allow concatenation with number.
70 -- TODO the third check should really be class.is(arg, TH.stringProxy), but
71 -- it doesn't currently work due to TH.stringProxy overriding __index
72 (key == "%s" and (t == "string" or t == "number" or t == "userdata")) then
73 if proxy_found then
74 args.proxy_found = true
75 end
76 return arg
77 end
78 return key
79 end +
80 -- New-style %([1-9])% and %([1-9]):([a-zA-Z0-9_.]-)%
81 lpeg.C("%" * lpeg.C(lpeg.R("19")) *
82 (":" * lpeg.C(lpeg.R("az","AZ","09","__","..")^1) + lpeg.Cc(nil))
83 * "%" * lpeg.Carg(1)) / function(str, key, tab, args)
84 local val, proxy_found = _unwrap(args[tonumber(key)])
85 if val == nil then return str end -- abort
86 if tab then
87 local string_table = _S
88 for part in tab:gmatch("[^.]+") do
89 string_table = string_table[part]
90 if not string_table then return str end -- abort
91 end
92 if not string_table[val] then return str end -- abort
93 val = _unwrap(string_table[val])
94 proxy_found = true
95 end
96 if proxy_found then
97 args.proxy_found = true
98 end
99 return val
100 end
101 ) + 1
102 ) ^ 0
103 )
104
105 --! Custom format function for our proxy strings.
106 --! Keywords for replacing are: %s, %d, %%, %[num]% and %[num]:[tab]%
107 --! NB: %s and %d are only replaced if none of the new keywords (%[num]% and %[num]:[tab]%) are present.
108 --! NB: Always escape percent sign (i.e. use %% instead of %) in strings with formatting keywords! Failure
109 --! to do so can cause unwanted behavior.
110 --!
111 --! Keywords:
112 --! %s, %d : replaced with the n-th parameter, if the type matches (%s = string, %d = number).
113 --! %% : replaced by single percent sign "%". Needed for escaping.
114 --! %[num]% : ([num] between 1-9) replaced by [num]-th parameter.
115 --! %[num]:[tab]% : replaced by string obtained by indexing string table _S.[tab] with [num]-th parameter.
116 --!param str (string, stringProxy) the string that contains keywords to be replaced
117 --!param ... (string, stringProxy, number) parameters to be inserted in the string (or used for lookup)
118 --!return formatted stringProxy, if any of the result's components was a stringProxy, else formatted string
119 function TH.stringProxy.format(str, ...)
120 local args = {idx = 0, ...}
121 local str_was_proxy
122 str, str_was_proxy = _unwrap(str)
123 local result = format_pattern:match(str, 1, args)
124 if str_was_proxy or args.proxy_found then
125 result = TH.stringProxy(result, TH.stringProxy.format, str, ...)
126 end
127 return result
128 end
129
130 --! Wrapper for the lua string.find function. Make sure to call mystring:find(...) instead of string.find(mystring, ...).
131 function TH.stringProxy.find(str, ...)
132 str = _unwrap(str)
133 return str:find(...)
134 end
135
136 --! Wrapper for the lua string.sub function. Make sure to call mystring:sub(...) instead of string.sub(mystring, ...).
137 function TH.stringProxy.sub(str, ...)
138 local str_was_proxy
139 str, str_was_proxy = _unwrap(str)
140 local result = str:sub(...)
141 if str_was_proxy then
142 result = TH.stringProxy(result, TH.stringProxy.sub, str, ...)
143 end
144 return result
145 end
146
147 for _, method_name in ipairs({"gsub", "format", "find", "sub"}) do
148 permanent("TH.stringProxy." .. method_name, TH.stringProxy[method_name])
149 end