utils.lua 847 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. -- {{{ License
  2. -- Awesome utils functions
  3. -- 2012, Emmanuel Bouthenot <kolter@openics.org>
  4. --
  5. -- This file is distributed in the hope that it will be useful,
  6. -- but WITHOUT ANY WARRANTY
  7. --
  8. -- }}}
  9. -- {{{ Functions
  10. function table.pos(t, k)
  11. local ret = nil
  12. for i,n in ipairs(t) do
  13. if n == k then
  14. ret = i
  15. break
  16. end
  17. end
  18. return ret
  19. end
  20. function table.keypos(t, p)
  21. local ret
  22. for i,n in ipairs(t) do
  23. if i == p then
  24. ret = n
  25. break
  26. end
  27. end
  28. return ret
  29. end
  30. function table.keyfirst(t)
  31. return table.keypos(t, 1)
  32. end
  33. function table.keylast(t)
  34. return table.keypos(t, table.getn(t))
  35. end
  36. function string.trim(s)
  37. -- from http://lua-users.org/wiki/StringTrim
  38. return (s:gsub("^%s*(.-)%s*$", "%1"))
  39. end
  40. -- }}}
  41. -- vim: foldmethod=marker