variable capture bug in match extension
[metalua.git] / src / lib / string2.lua
blob60c186d31a04eab2987b231558d9bce87616acfa
2 ----------------------------------------------------------------------
3 ----------------------------------------------------------------------
4 --
5 -- String module extension
6 --
7 ----------------------------------------------------------------------
8 ----------------------------------------------------------------------
10 -- Courtesy of lua-users.org
11 function string.split(str, pat)
12 local t = {}
13 local fpat = "(.-)" .. pat
14 local last_end = 1
15 local s, e, cap = string.find(str, fpat, 1)
16 while s do
17 if s ~= 1 or cap ~= "" then
18 table.insert(t,cap)
19 end
20 last_end = e+1
21 s, e, cap = string.find(str, fpat, last_end)
22 end
23 if last_end <= string.len(str) then
24 cap = string.sub(str, last_end)
25 table.insert(t, cap)
26 end
27 return t
28 end
30 -- "match" is regularly used as a keyword for pattern matching,
31 -- so here is an always available substitute.
32 string.strmatch = string["match"]
34 -- change a compiled string into a function
35 function string.undump(str)
36 if str:strmatch '^\027LuaQ' or str:strmatch '^#![^\n]+\n\027LuaQ' then
37 local f = (lua_loadstring or loadstring)(str)
38 return f
39 else
40 error "Not a chunk dump"
41 end
42 end
44 return string