1 QuestHelper_File
["pattern.lua"] = "Development Version"
3 -- The following junk is for building functions to parse strings for various locales.
5 local varindex
, varswap
= 0, {}
7 local function replacePattern(p
)
8 varindex
= varindex
+ 1
9 local b
, e
, i
= string.find(p
, "(%d-)%$")
12 -- String contains an index.
13 p
= string.sub(p
, 1, b
-1)..string.sub(p
, e
+1)
16 -- String doesn't contains an index, assume its index is the position we found it in.
22 if p
== "d" then return "(%d+)" end
23 if p
== "s" then return "(.-)" end
25 -- Only know about strings and integers. Other types are broken.
26 assert(false, "Unknown pattern: %"..p
)
29 -- Used to cache results, so we don't produce multiple functions for the same string.
30 local known_patterns
= {}
32 function QuestHelper
:convertPattern(pattern
)
33 local result
= known_patterns
[pattern
]
39 while(table.remove(varswap
)) do end
43 local pat
= string.format("^%s$", string.gsub(pattern
, "%%(.-[%%%a])", replacePattern
))
46 -- The string doesn't contain any variables.
49 local func
= "local function parse(input)\n"
53 if varswap
[i
] ~= i
then
60 -- All the arguments are in order, we can just return them.
61 func
= func
.. string.format("return select(3, string.find(input, %q))", pat
)
63 -- The order has been changed, use temporary variables.
65 func
= func
..(i
==1 and "local " or ", ")..("n"..i
)
68 func
= func
.. string.format(" = select(3, string.find(input, %q))\n", pat
)
71 func
= func
..(i
==1 and "return " or ", ")..(varswap
[i
] and ("n"..varswap
[i
]) or "nil")
75 func
= func
.. "\nend\nreturn parse\n"
77 result
= loadstring(func
)()
80 known_patterns
[pattern
] = result