/metalualib/metalua/string2.lua

http://github.com/davidm/lua-inspect · Lua · 44 lines · 28 code · 5 blank · 11 comment · 9 complexity · b6965200c6f4142ac543e9dd237111ec MD5 · raw file

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