sdl_string.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. -- Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  2. --
  3. -- This software is provided 'as-is', without any express or implied
  4. -- warranty. In no event will the authors be held liable for any damages
  5. -- arising from the use of this software.
  6. --
  7. -- Permission is granted to anyone to use this software for any purpose,
  8. -- including commercial applications, and to alter it and redistribute it
  9. -- freely.
  10. --
  11. -- Meta-build system using premake created and maintained by
  12. -- Benjamin Henning <b.henning@digipen.edu>
  13. --[[
  14. sdl_string.lua
  15. Contains a few convenient string utility functions which are not supported in
  16. Lua or not supported as intended.
  17. ]]
  18. -- Performs a non-pattern based substring search of one string in another
  19. -- string. It's of O(n^2) complexity. It returns nil if the result cannot be
  20. -- found, otherwise it returns the starting index of the first found occurrence.
  21. string.indexOf = function(str, substr)
  22. local pos = 1
  23. local i = 1
  24. for i = 1, str:len(), 1 do
  25. if str:sub(i, i) == substr:sub(pos, pos) then
  26. -- have we matched the complete string?
  27. if pos == substr:len() then
  28. return i - pos + 1-- starting pos
  29. end
  30. -- matched character...keep going
  31. pos = pos + 1
  32. else
  33. -- restart, no match
  34. pos = 0
  35. end
  36. end
  37. if pos == substr:len() then
  38. return i - pos + 1
  39. end
  40. return nil -- no match
  41. end
  42. -- This is a public-access version of the explode function defined below.
  43. function explode(str, delim)
  44. return str:explode(delim)
  45. end
  46. -- Explodes a string into an array of elements, separated by a non-pattern
  47. -- delimiter. This function is part of the string table, allowing for a
  48. -- member-based invocation for strings.
  49. string.explode = function(str, delim)
  50. local exploded = { }
  51. local needle = string.find(str, delim)
  52. while needle ~= nil do
  53. table.insert(exploded, string.sub(str, 0, needle - 1))
  54. str = string.sub(str, needle + 1)
  55. needle = string.find(str, delim)
  56. end
  57. table.insert(exploded, str)
  58. return exploded
  59. end
  60. -- Similar to table.concat, except it supports more advanced token pasting. This
  61. -- function is vastly used by the main meta-build script (premake4.lua) to
  62. -- generate all the main lines of code for various tables that need to be in the
  63. -- generated lua file.
  64. -- - tbl: table of values to implode into a string
  65. -- - prefix: string to paste before entire result
  66. -- - pre: string to always paste before each entry in table
  67. -- - post: string to always paste after each entry in table
  68. -- - join: string to paste between entries (inclusive)
  69. -- - suffix: string to paste after entire result
  70. -- Returns the imploded string.
  71. function implode(tbl, prefix, pre, post, join, suffix)
  72. local result = ""
  73. -- not the most efficient way to do this, but...
  74. local itbl = { }
  75. for k,v in pairs(tbl) do
  76. itbl[#itbl + 1] = v
  77. end
  78. for i = 1, #itbl, 1 do
  79. if pre ~= nil then
  80. result = result .. pre
  81. end
  82. result = result .. itbl[i]
  83. if post ~= nil then
  84. result = result .. post
  85. end
  86. if i ~= #itbl then
  87. result = result .. join
  88. end
  89. end
  90. if prefix ~= nil then
  91. result = prefix .. result
  92. end
  93. if suffix ~= nil then
  94. result = result .. suffix
  95. end
  96. return result
  97. end