vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | local MAJOR_VERSION = "Deformat 1.2" |
2 | local MINOR_VERSION = tonumber(string.sub("$Revision: 3860 $", 12, -3)) |
||
3 | |||
4 | if BabbleLib and BabbleLib.versions[MAJOR_VERSION] and BabbleLib.versions[MAJOR_VERSION].minor >= MINOR_VERSION then |
||
5 | return |
||
6 | end |
||
7 | |||
8 | local locale = GetLocale and GetLocale() or "enUS" |
||
9 | if locale ~= "frFR" and locale ~= "deDE" and locale ~= "zhCN" and locale ~= "koKR" then |
||
10 | locale = "enUS" |
||
11 | end |
||
12 | |||
13 | -------------IRIEL'S-STUB-CODE-------------- |
||
14 | local stub = {}; |
||
15 | |||
16 | -- Instance replacement method, replace contents of old with that of new |
||
17 | function stub:ReplaceInstance(old, new) |
||
18 | for k,v in pairs(old) do old[k]=nil; end |
||
19 | for k,v in pairs(new) do old[k]=v; end |
||
20 | end |
||
21 | |||
22 | -- Get a new copy of the stub |
||
23 | function stub:NewStub() |
||
24 | local newStub = {}; |
||
25 | self:ReplaceInstance(newStub, self); |
||
26 | newStub.lastVersion = ''; |
||
27 | newStub.versions = {}; |
||
28 | return newStub; |
||
29 | end |
||
30 | |||
31 | -- Get instance version |
||
32 | function stub:GetInstance(version) |
||
33 | if (not version) then version = self.lastVersion; end |
||
34 | local versionData = self.versions[version]; |
||
35 | if (not versionData) then |
||
36 | message("Cannot find library instance with version '" |
||
37 | .. version .. "'"); |
||
38 | return; |
||
39 | end |
||
40 | return versionData.instance; |
||
41 | end |
||
42 | |||
43 | -- Register new instance |
||
44 | function stub:Register(newInstance) |
||
45 | local version,minor = newInstance:GetLibraryVersion(); |
||
46 | self.lastVersion = version; |
||
47 | local versionData = self.versions[version]; |
||
48 | if (not versionData) then |
||
49 | -- This one is new! |
||
50 | versionData = { instance = newInstance, |
||
51 | minor = minor, |
||
52 | old = {} |
||
53 | }; |
||
54 | self.versions[version] = versionData; |
||
55 | newInstance:LibActivate(self); |
||
56 | return newInstance; |
||
57 | end |
||
58 | if (minor <= versionData.minor) then |
||
59 | -- This one is already obsolete |
||
60 | if (newInstance.LibDiscard) then |
||
61 | newInstance:LibDiscard(); |
||
62 | end |
||
63 | return versionData.instance; |
||
64 | end |
||
65 | -- This is an update |
||
66 | local oldInstance = versionData.instance; |
||
67 | local oldList = versionData.old; |
||
68 | versionData.instance = newInstance; |
||
69 | versionData.minor = minor; |
||
70 | local skipCopy = newInstance:LibActivate(self, oldInstance, oldList); |
||
71 | table.insert(oldList, oldInstance); |
||
72 | if (not skipCopy) then |
||
73 | for i, old in ipairs(oldList) do |
||
74 | self:ReplaceInstance(old, newInstance); |
||
75 | end |
||
76 | end |
||
77 | return newInstance; |
||
78 | end |
||
79 | |||
80 | -- Bind stub to global scope if it's not already there |
||
81 | if (not BabbleLib) then |
||
82 | BabbleLib = stub:NewStub(); |
||
83 | end |
||
84 | |||
85 | -- Nil stub for garbage collection |
||
86 | stub = nil; |
||
87 | -----------END-IRIEL'S-STUB-CODE------------ |
||
88 | |||
89 | local function assert(condition, message) |
||
90 | if not condition then |
||
91 | local stack = debugstack() |
||
92 | local first = string.gsub(stack, "\n.*", "") |
||
93 | local file = string.gsub(first, "^(.*\\.*).lua:%d+: .*", "%1") |
||
94 | file = string.gsub(file, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1") |
||
95 | if not message then |
||
96 | local _,_,second = string.find(stack, "\n(.-)\n") |
||
97 | message = "assertion failed! " .. second |
||
98 | end |
||
99 | message = "BabbleLib-Deformat: " .. message |
||
100 | local i = 1 |
||
101 | for s in string.gfind(stack, "\n([^\n]*)") do |
||
102 | i = i + 1 |
||
103 | if not string.find(s, file .. "%.lua:%d+:") then |
||
104 | error(message, i) |
||
105 | return |
||
106 | end |
||
107 | end |
||
108 | error(message, 2) |
||
109 | return |
||
110 | end |
||
111 | return condition |
||
112 | end |
||
113 | |||
114 | local function argCheck(arg, num, kind, kind2, kind3, kind4) |
||
115 | if tostring(type(arg)) ~= kind then |
||
116 | if kind2 then |
||
117 | if tostring(type(arg)) ~= kind2 then |
||
118 | if kind3 then |
||
119 | if tostring(type(arg)) ~= kind3 then |
||
120 | if kind4 then |
||
121 | if tostring(type(arg)) ~= kind4 then |
||
122 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
123 | assert(false, format("Bad argument #%d to `%s' (%s, %s, %s, or %s expected, got %s)", num, func, kind, kind2, kind3, kind4, type(arg))) |
||
124 | end |
||
125 | else |
||
126 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
127 | assert(false, format("Bad argument #%d to `%s' (%s, %s, or %s expected, got %s)", num, func, kind, kind2, kind3, type(arg))) |
||
128 | end |
||
129 | end |
||
130 | else |
||
131 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
132 | assert(false, format("Bad argument #%d to `%s' (%s or %s expected, got %s)", num, func, kind, kind2, type(arg))) |
||
133 | end |
||
134 | end |
||
135 | else |
||
136 | local _,_,func = string.find(debugstack(), "\n.-`(.-)'\n") |
||
137 | assert(false, format("Bad argument #%d to `%s' (%s expected, got %s)", num, func, kind, type(arg))) |
||
138 | end |
||
139 | end |
||
140 | end |
||
141 | |||
142 | local lib = {} |
||
143 | |||
144 | do |
||
145 | local sequences = { |
||
146 | ["%d*d"] = "%%d+", |
||
147 | ["s"] = ".+", |
||
148 | ["[fg]"] = "%%d+%%.%%d+", |
||
149 | ["%.%d[fg]"] = "%%d+%%.%%d+", |
||
150 | ["c"] = ".", |
||
151 | } |
||
152 | local curries = {} |
||
153 | |||
154 | local function doNothing(item) |
||
155 | return item |
||
156 | end |
||
157 | local v = {} |
||
158 | |||
159 | local function concat(a1, a2, a3, a4) |
||
160 | local left, right |
||
161 | if not a2 then |
||
162 | return a1 |
||
163 | elseif not a3 then |
||
164 | left, right = a1, a2 |
||
165 | elseif not a4 then |
||
166 | return concat(concat(a1, a2), a3) |
||
167 | else |
||
168 | return concat(concat(concat(a1, a2), a3), a4) |
||
169 | end |
||
170 | if not string.find(left, "%%1%$") and not string.find(right, "%%1%$") then |
||
171 | return left .. right |
||
172 | elseif not string.find(right, "%%1%$") then |
||
173 | local i |
||
174 | for j = 9, 1, -1 do |
||
175 | if string.find(left, "%%" .. j .. "%$") then |
||
176 | i = j |
||
177 | break |
||
178 | end |
||
179 | end |
||
180 | while true do |
||
181 | local first |
||
182 | local firstPat |
||
183 | for x, y in pairs(sequences) do |
||
184 | local i = string.find(right, "%%" .. x) |
||
185 | if not first or (i and i < first) then |
||
186 | first = i |
||
187 | firstPat = x |
||
188 | end |
||
189 | end |
||
190 | if not first then |
||
191 | break |
||
192 | end |
||
193 | i = i + 1 |
||
194 | right = string.gsub(right, "%%(" .. firstPat .. ")", "%%" .. i .. "$%1") |
||
195 | end |
||
196 | return left .. right |
||
197 | elseif not string.find(left, "%%1%$") then |
||
198 | local i = 1 |
||
199 | while true do |
||
200 | local first |
||
201 | local firstPat |
||
202 | for x, y in pairs(sequences) do |
||
203 | local i = string.find(left, "%%" .. x) |
||
204 | if not first or (i and i < first) then |
||
205 | first = i |
||
206 | firstPat = x |
||
207 | end |
||
208 | end |
||
209 | if not first then |
||
210 | break |
||
211 | end |
||
212 | i = i + 1 |
||
213 | left = string.gsub(left, "%%(" .. firstPat .. ")", "%%" .. i .. "$%1") |
||
214 | end |
||
215 | return concat(left, right) |
||
216 | else |
||
217 | local i |
||
218 | for j = 9, 1, -1 do |
||
219 | if string.find(left, "%%" .. j .. "%$") then |
||
220 | i = j |
||
221 | break |
||
222 | end |
||
223 | end |
||
224 | local j |
||
225 | for k = 9, 1, -1 do |
||
226 | if string.find(right, "%%" .. k .. "%$") then |
||
227 | j = k |
||
228 | break |
||
229 | end |
||
230 | end |
||
231 | for k = j, 1, -1 do |
||
232 | right = string.gsub(right, "%%" .. k .. "%$", "%%" .. k + i .. "%$") |
||
233 | end |
||
234 | return left .. right |
||
235 | end |
||
236 | end |
||
237 | |||
238 | local function Curry(a1, a2, a3, a4) |
||
239 | local pattern = concat(a1, a2, a3, a4) |
||
240 | if not string.find(pattern, "%%1%$") then |
||
241 | local unpattern = string.gsub(pattern, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1") |
||
242 | local f = {} |
||
243 | local i = 0 |
||
244 | while true do |
||
245 | local first |
||
246 | local firstPat |
||
247 | for x, y in pairs(sequences) do |
||
248 | local i = string.find(unpattern, "%%%%" .. x) |
||
249 | if not first or (i and i < first) then |
||
250 | first = i |
||
251 | firstPat = x |
||
252 | end |
||
253 | end |
||
254 | if not first then |
||
255 | break |
||
256 | end |
||
257 | unpattern = string.gsub(unpattern, "%%%%" .. firstPat, "(" .. sequences[firstPat] .. ")", 1) |
||
258 | i = i + 1 |
||
259 | if firstPat == "c" or firstPat == "s" then |
||
260 | table.insert(f, doNothing) |
||
261 | else |
||
262 | table.insert(f, tonumber) |
||
263 | end |
||
264 | end |
||
265 | unpattern = "^" .. unpattern .. "$" |
||
266 | local _,alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india |
||
267 | if i == 0 then |
||
268 | return |
||
269 | elseif i == 1 then |
||
270 | return function(text) |
||
271 | _,_,alpha = string.find(text, unpattern) |
||
272 | if alpha then |
||
273 | return f[1](alpha) |
||
274 | end |
||
275 | end |
||
276 | elseif i == 2 then |
||
277 | return function(text) |
||
278 | _,_,alpha, bravo = string.find(text, unpattern) |
||
279 | if alpha then |
||
280 | return f[1](alpha), f[2](bravo) |
||
281 | end |
||
282 | end |
||
283 | elseif i == 3 then |
||
284 | return function(text) |
||
285 | _,_,alpha, bravo, charlie = string.find(text, unpattern) |
||
286 | if alpha then |
||
287 | return f[1](alpha), f[2](bravo), f[3](charlie) |
||
288 | end |
||
289 | end |
||
290 | elseif i == 4 then |
||
291 | return function(text) |
||
292 | _,_,alpha, bravo, charlie, delta = string.find(text, unpattern) |
||
293 | if alpha then |
||
294 | return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta) |
||
295 | end |
||
296 | end |
||
297 | elseif i == 5 then |
||
298 | return function(text) |
||
299 | _,_,alpha, bravo, charlie, delta, echo = string.find(text, unpattern) |
||
300 | if alpha then |
||
301 | return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo) |
||
302 | end |
||
303 | end |
||
304 | elseif i == 6 then |
||
305 | return function(text) |
||
306 | _,_,alpha, bravo, charlie, delta, echo, foxtrot = string.find(text, unpattern) |
||
307 | if alpha then |
||
308 | return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo), f[6](foxtrot) |
||
309 | end |
||
310 | end |
||
311 | elseif i == 7 then |
||
312 | return function(text) |
||
313 | _,_,alpha, bravo, charlie, delta, echo, foxtrot, golf = string.find(text, unpattern) |
||
314 | if alpha then |
||
315 | return f[1](alpha), f[2](bravo), f[3](charlie), f[4](delta), f[5](echo), f[6](foxtrot), f[7](golf) |
||
316 | end |
||
317 | end |
||
318 | elseif i == 8 then |
||
319 | return function(text) |
||
320 | _,_,alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel = string.find(text, unpattern) |
||
321 | if alpha then |
||
322 | 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) |
||
323 | end |
||
324 | end |
||
325 | else |
||
326 | return function(text) |
||
327 | _,_,alpha, bravo, charlie, delta, echo, foxtrot, golf, hotel, india = string.find(text, unpattern) |
||
328 | if alpha then |
||
329 | 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) |
||
330 | end |
||
331 | end |
||
332 | end |
||
333 | else |
||
334 | local o = {} |
||
335 | local f = {} |
||
336 | local unpattern = string.gsub(pattern, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1") |
||
337 | local i = 1 |
||
338 | while true do |
||
339 | local pat |
||
340 | for x, y in pairs(sequences) do |
||
341 | if not pat and string.find(unpattern, "%%%%" .. i .. "%%%$" .. x) then |
||
342 | pat = x |
||
343 | break |
||
344 | end |
||
345 | end |
||
346 | if not pat then |
||
347 | break |
||
348 | end |
||
349 | unpattern = string.gsub(unpattern, "%%%%" .. i .. "%%%$" .. pat, "(" .. sequences[pat] .. ")", 1) |
||
350 | if pat == "c" or pat == "s" then |
||
351 | table.insert(f, doNothing) |
||
352 | else |
||
353 | table.insert(f, tonumber) |
||
354 | end |
||
355 | i = i + 1 |
||
356 | end |
||
357 | i = 1 |
||
358 | string.gsub(pattern, "%%(%d)%$", function(w) o[i] = tonumber(w); i = i + 1; end) |
||
359 | v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9] = nil |
||
360 | for x, y in pairs(f) do |
||
361 | v[x] = f[y] |
||
362 | end |
||
363 | for x, y in pairs(v) do |
||
364 | f[x] = v[x] |
||
365 | end |
||
366 | unpattern = "^" .. unpattern .. "$" |
||
367 | i = i - 1 |
||
368 | if i == 0 then |
||
369 | return function(text) |
||
370 | return |
||
371 | end |
||
372 | elseif i == 1 then |
||
373 | return function(text) |
||
374 | _,_,v[1] = string.find(text, unpattern) |
||
375 | if v[1] then |
||
376 | return f[1](v[1]) |
||
377 | end |
||
378 | end |
||
379 | elseif i == 2 then |
||
380 | return function(text) |
||
381 | _,_,v[1],v[2] = string.find(text, unpattern) |
||
382 | if v[1] then |
||
383 | return f[1](v[o[1]]), f[2](v[o[2]]) |
||
384 | end |
||
385 | end |
||
386 | elseif i == 3 then |
||
387 | return function(text) |
||
388 | _,_,v[1],v[2],v[3] = string.find(text, unpattern) |
||
389 | if v[1] then |
||
390 | return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]) |
||
391 | end |
||
392 | end |
||
393 | elseif i == 4 then |
||
394 | return function(text) |
||
395 | _,_,v[1],v[2],v[3],v[4] = string.find(text, unpattern) |
||
396 | if v[1] then |
||
397 | return f[1](v[o[1]]), f[2](v[o[2]]), f[3](v[o[3]]), f[4](v[o[4]]) |
||
398 | end |
||
399 | end |
||
400 | elseif i == 5 then |
||
401 | return function(text) |
||
402 | _,_,v[1],v[2],v[3],v[4],v[5] = string.find(text, unpattern) |
||
403 | if v[1] then |
||
404 | 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]]) |
||
405 | end |
||
406 | end |
||
407 | elseif i == 6 then |
||
408 | return function(text) |
||
409 | _,_,v[1],v[2],v[3],v[4],v[5],v[6] = string.find(text, unpattern) |
||
410 | if v[1] then |
||
411 | 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]]) |
||
412 | end |
||
413 | end |
||
414 | elseif i == 7 then |
||
415 | return function(text) |
||
416 | _,_,v[1],v[2],v[3],v[4],v[5],v[6],v[7] = string.find(text, unpattern) |
||
417 | if v[1] then |
||
418 | 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]]) |
||
419 | end |
||
420 | end |
||
421 | elseif i == 8 then |
||
422 | return function(text) |
||
423 | _,_,v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8] = string.find(text, unpattern) |
||
424 | if v[1] then |
||
425 | 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]]) |
||
426 | end |
||
427 | end |
||
428 | else |
||
429 | return function(text) |
||
430 | _,_,v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9] = string.find(text, unpattern) |
||
431 | if v[1] then |
||
432 | 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]]) |
||
433 | end |
||
434 | end |
||
435 | end |
||
436 | end |
||
437 | end |
||
438 | |||
439 | function lib:Deformat(text, a1, a2, a3, a4) |
||
440 | argCheck(text, 2, "string") |
||
441 | argCheck(a1, 3, "string") |
||
442 | local pattern = a1 |
||
443 | if a4 then |
||
444 | pattern = string.format("%s%s%s%s", a1, a2, a3, a4) |
||
445 | elseif a3 then |
||
446 | pattern = string.format("%s%s%s", a1, a2, a3) |
||
447 | elseif a2 then |
||
448 | pattern = a1 .. a2 |
||
449 | end |
||
450 | if curries[pattern] == nil then |
||
451 | curries[pattern] = Curry(a1, a2, a3, a4) |
||
452 | end |
||
453 | return curries[pattern](text) |
||
454 | end |
||
455 | end |
||
456 | |||
457 | function lib:GetLibraryVersion() |
||
458 | return MAJOR_VERSION, MINOR_VERSION |
||
459 | end |
||
460 | |||
461 | function lib:LibActivate(stub, oldLib, oldList) |
||
462 | local mt = getmetatable(self) or {} |
||
463 | mt.__call = self.Deformat |
||
464 | setmetatable(self, mt) |
||
465 | end |
||
466 | |||
467 | function lib:LibDeactivate() |
||
468 | end |
||
469 | |||
470 | BabbleLib:Register(lib) |
||
471 | lib = nil |