vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Name: Deformat-2.0
3 Revision: $Rev: 6804 $
4 Author(s): ckknight (ckknight@gmail.com)
5 Website: http://ckknight.wowinterface.com/
6 Documentation: http://wiki.wowace.com/index.php/Deformat-2.0
7 SVN: http://svn.wowace.com/root/trunk/Deformat/Deformat-2.0
8 Description: A library to deformat format strings.
9 Dependencies: AceLibrary
10 ]]
11  
12 local MAJOR_VERSION = "Deformat-2.0"
13 local MINOR_VERSION = "$Revision: 6804 $"
14  
15  
16 if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary") end
17 if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
18  
19 local Deformat = {}
20  
21 do
22 local sequences = {
23 ["%d*d"] = "%%-?%%d+",
24 ["s"] = ".+",
25 ["[fg]"] = "%%-?%%d+%%.%%d+",
26 ["%%%.%d[fg]"] = "%%-?%%d+%%.?%%d*",
27 ["c"] = ".",
28 }
29 local curries = {}
30  
31 local function doNothing(item)
32 return item
33 end
34 local v = {}
35  
36 local function concat(a1, a2, a3, a4, a5)
37 local left, right
38 if not a2 then
39 return a1
40 elseif not a3 then
41 left, right = a1, a2
42 elseif not a4 then
43 return concat(concat(a1, a2), a3)
44 elseif not a5 then
45 return concat(concat(concat(a1, a2), a3), a4)
46 else
47 return concat(concat(concat(concat(a1, a2), a3), a4), a5)
48 end
49 if not string.find(left, "%%1%$") and not string.find(right, "%%1%$") then
50 return left .. right
51 elseif not string.find(right, "%%1%$") then
52 local i
53 for j = 9, 1, -1 do
54 if string.find(left, "%%" .. j .. "%$") then
55 i = j
56 break
57 end
58 end
59 while true do
60 local first
61 local firstPat
62 for x, y in pairs(sequences) do
63 local i = string.find(right, "%%" .. x)
64 if not first or (i and i < first) then
65 first = i
66 firstPat = x
67 end
68 end
69 if not first then
70 break
71 end
72 i = i + 1
73 right = string.gsub(right, "%%(" .. firstPat .. ")", "%%" .. i .. "$%1")
74 end
75 return left .. right
76 elseif not string.find(left, "%%1%$") then
77 local i = 1
78 while true do
79 local first
80 local firstPat
81 for x, y in pairs(sequences) do
82 local i = string.find(left, "%%" .. x)
83 if not first or (i and i < first) then
84 first = i
85 firstPat = x
86 end
87 end
88 if not first then
89 break
90 end
91 i = i + 1
92 left = string.gsub(left, "%%(" .. firstPat .. ")", "%%" .. i .. "$%1")
93 end
94 return concat(left, right)
95 else
96 local i
97 for j = 9, 1, -1 do
98 if string.find(left, "%%" .. j .. "%$") then
99 i = j
100 break
101 end
102 end
103 local j
104 for k = 9, 1, -1 do
105 if string.find(right, "%%" .. k .. "%$") then
106 j = k
107 break
108 end
109 end
110 for k = j, 1, -1 do
111 right = string.gsub(right, "%%" .. k .. "%$", "%%" .. k + i .. "%$")
112 end
113 return left .. right
114 end
115 end
116  
117 local function Curry(a1, a2, a3, a4, a5)
118 local pattern = concat(a1, a2, a3, a4, a5)
119 if not string.find(pattern, "%%1%$") then
120 local unpattern = string.gsub(pattern, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1")
121 local f = {}
122 local i = 0
123 while true do
124 local first
125 local firstPat
126 for x, y in pairs(sequences) do
127 local i = string.find(unpattern, "%%%%" .. x)
128 if not first or (i and i < first) then
129 first = i
130 firstPat = x
131 end
132 end
133 if not first then
134 break
135 end
136 unpattern = string.gsub(unpattern, "%%%%" .. firstPat, "(" .. sequences[firstPat] .. ")", 1)
137 i = i + 1
138 if firstPat == "c" or firstPat == "s" then
139 table.insert(f, doNothing)
140 else
141 table.insert(f, tonumber)
142 end
143 end
144 unpattern = "^" .. unpattern .. "$"
145 local _,alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india
146 if i == 0 then
147 return
148 elseif i == 1 then
149 return function(text)
150 _,_,alpha = string.find(text, unpattern)
151 if alpha then
152 return f[1](alpha)
153 end
154 end
155 elseif i == 2 then
156 return function(text)
157 _,_,alpha, bravo = string.find(text, unpattern)
158 if alpha then
159 return f[1](alpha), f[2](bravo)
160 end
161 end
162 elseif i == 3 then
163 return function(text)
164 _,_,alpha, bravo, charlie = string.find(text, unpattern)
165 if alpha then
166 return f[1](alpha), f[2](bravo), f[3](charlie)
167 end
168 end
169 elseif i == 4 then
170 return function(text)
171 _,_,alpha, bravo, charlie, delta = string.find(text, unpattern)
172 if alpha then
173 return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta)
174 end
175 end
176 elseif i == 5 then
177 return function(text)
178 _,_,alpha, bravo, charlie, delta, echo = string.find(text, unpattern)
179 if alpha then
180 return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo)
181 end
182 end
183 elseif i == 6 then
184 return function(text)
185 _,_,alpha, bravo, charlie, delta, echo, foxtrot = string.find(text, unpattern)
186 if alpha then
187 return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo), f[6](foxtrot)
188 end
189 end
190 elseif i == 7 then
191 return function(text)
192 _,_,alpha, bravo, charlie, delta, echo, foxtrot, golf = string.find(text, unpattern)
193 if alpha then
194 return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo), f[6](foxtrot), f[7](golf)
195 end
196 end
197 elseif i == 8 then
198 return function(text)
199 _,_,alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel = string.find(text, unpattern)
200 if alpha then
201 return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo), f[6](foxtrot), f[7](golf), f[8](hotel)
202 end
203 end
204 else
205 return function(text)
206 _,_,alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india = string.find(text, unpattern)
207 if alpha then
208 return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo), f[6](foxtrot), f[7](golf), f[8](hotel), f[9](india)
209 end
210 end
211 end
212 else
213 local o = {}
214 local f = {}
215 local unpattern = string.gsub(pattern, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1")
216 local i = 1
217 while true do
218 local pat
219 for x, y in pairs(sequences) do
220 if not pat and string.find(unpattern, "%%%%" .. i .. "%%%$" .. x) then
221 pat = x
222 break
223 end
224 end
225 if not pat then
226 break
227 end
228 unpattern = string.gsub(unpattern, "%%%%" .. i .. "%%%$" .. pat, "(" .. sequences[pat] .. ")", 1)
229 if pat == "c" or pat == "s" then
230 table.insert(f, doNothing)
231 else
232 table.insert(f, tonumber)
233 end
234 i = i + 1
235 end
236 i = 1
237 string.gsub(pattern, "%%(%d)%$", function(w) o[i] = tonumber(w); i = i + 1; end)
238 v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9] = nil
239 for x, y in pairs(f) do
240 v[x] = f[y]
241 end
242 for x, y in pairs(v) do
243 f[x] = v[x]
244 end
245 unpattern = "^" .. unpattern .. "$"
246 i = i - 1
247 if i == 0 then
248 return function(text)
249 return
250 end
251 elseif i == 1 then
252 return function(text)
253 _,_,v[1] = string.find(text, unpattern)
254 if v[1] then
255 return f[1](v[1])
256 end
257 end
258 elseif i == 2 then
259 return function(text)
260 _,_,v[1],v[2] = string.find(text, unpattern)
261 if v[1] then
262 return f[1](v[o[1]]), f[2](v[o[2]])
263 end
264 end
265 elseif i == 3 then
266 return function(text)
267 _,_,v[1],v[2],v[3] = string.find(text, unpattern)
268 if v[1] then
269 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]])
270 end
271 end
272 elseif i == 4 then
273 return function(text)
274 _,_,v[1],v[2],v[3],v[4] = string.find(text, unpattern)
275 if v[1] then
276 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]])
277 end
278 end
279 elseif i == 5 then
280 return function(text)
281 _,_,v[1],v[2],v[3],v[4],v[5] = string.find(text, unpattern)
282 if v[1] then
283 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]]), f[5](v[o[5]])
284 end
285 end
286 elseif i == 6 then
287 return function(text)
288 _,_,v[1],v[2],v[3],v[4],v[5],v[6] = string.find(text, unpattern)
289 if v[1] then
290 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]]), f[5](v[o[5]]), f[6](v[o[6]])
291 end
292 end
293 elseif i == 7 then
294 return function(text)
295 _,_,v[1],v[2],v[3],v[4],v[5],v[6],v[7] = string.find(text, unpattern)
296 if v[1] then
297 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]]), f[5](v[o[5]]), f[6](v[o[6]]), f[7](v[o[7]])
298 end
299 end
300 elseif i == 8 then
301 return function(text)
302 _,_,v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8] = string.find(text, unpattern)
303 if v[1] then
304 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]]), f[5](v[o[5]]), f[6](v[o[6]]), f[7](v[o[7]]), f[8](v[o[8]])
305 end
306 end
307 else
308 return function(text)
309 _,_,v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9] = string.find(text, unpattern)
310 if v[1] then
311 return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]]), f[5](v[o[5]]), f[6](v[o[6]]), f[7](v[o[7]]), f[8](v[o[8]]), f[9](v[o[9]])
312 end
313 end
314 end
315 end
316 end
317  
318 function Deformat:Deformat(text, a1, a2, a3, a4, a5)
319 self:argCheck(text, 2, "string")
320 self:argCheck(a1, 3, "string")
321 local pattern = a1
322 if a5 then
323 pattern = a1 .. a2 .. a3 .. a4 .. a5
324 elseif a4 then
325 pattern = a1 .. a2 .. a3 .. a4
326 elseif a3 then
327 pattern = a1 .. a2 .. a3
328 elseif a2 then
329 pattern = a1 .. a2
330 end
331 if curries[pattern] == nil then
332 curries[pattern] = Curry(a1, a2, a3, a4, a5)
333 end
334 return curries[pattern](text)
335 end
336 end
337  
338 local mt = getmetatable(Deformat) or {}
339 mt.__call = Deformat.Deformat
340 setmetatable(Deformat, mt)
341  
342 AceLibrary:Register(Deformat, MAJOR_VERSION, MINOR_VERSION)
343 Deformat = nil