/metalualib/metalua/string2.lua
Lua | 44 lines | 28 code | 5 blank | 11 comment | 4 complexity | b6965200c6f4142ac543e9dd237111ec MD5 | raw file
Possible License(s): ISC
1 2---------------------------------------------------------------------- 3---------------------------------------------------------------------- 4-- 5-- String module extension 6-- 7---------------------------------------------------------------------- 8---------------------------------------------------------------------- 9 10-- Courtesy of lua-users.org 11function 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 28end 29 30-- "match" is regularly used as a keyword for pattern matching, 31-- so here is an always available substitute. 32string.strmatch = string["match"] 33 34-- change a compiled string into a function 35function 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 42end 43 44return string