1 -- The following junk is for building functions to parse strings for various locales.
3 local varindex
, varswap
= 0, {}
5 local function replacePattern(p
)
6 varindex
= varindex
+ 1
7 local b
, e
, i
= string.find(p
, "(%d-)%$")
10 -- String contains an index.
11 p
= string.sub(p
, 1, b
-1)..string.sub(p
, e
+1)
14 -- String doesn't contains an index, assume its index is the position we found it in.
20 if p
== "d" then return "(%d+)" end
21 if p
== "s" then return "(.-)" end
23 -- Only know about strings and integers. Other types are broken.
24 assert(false, "Unknown pattern: %"..p
)
27 -- Used to cache results, so we don't produce multiple functions for the same string.
28 local known_patterns
= {}
30 function QuestHelper
:convertPattern(pattern
)
31 local result
= known_patterns
[pattern
]
37 while(table.remove(varswap
)) do end
41 local pat
= string.format("^%s$", string.gsub(pattern
, "%%(.-[%%%a])", replacePattern
))
44 -- The string doesn't contain any variables.
47 local func
= "local function parse(input)\n"
51 if varswap
[i
] ~= i
then
58 -- All the arguments are in order, we can just return them.
59 func
= func
.. string.format("return select(3, string.find(input, %q))", pat
)
61 -- The order has been changed, use temporary variables.
63 func
= func
..(i
==1 and "local " or ", ")..("n"..i
)
66 func
= func
.. string.format(" = select(3, string.find(input, %q))\n", pat
)
69 func
= func
..(i
==1 and "return " or ", ")..(varswap
[i
] and ("n"..varswap
[i
]) or "nil")
73 func
= func
.. "\nend\nreturn parse\n"
75 result
= loadstring(func
)()
78 known_patterns
[pattern
] = result