diff options
author | IronClawTrem <louie.nutman@gmail.com> | 2020-02-16 03:40:06 +0000 |
---|---|---|
committer | IronClawTrem <louie.nutman@gmail.com> | 2020-02-16 03:40:06 +0000 |
commit | 425decdf7e9284d15aa726e3ae96b9942fb0e3ea (patch) | |
tree | 6c0dd7edfefff1be7b9e75fe0b3a0a85fe1595f3 /scripts/granger/lib/string.lua | |
parent | ccb0b2e4d6674a7a00c9bf491f08fc73b6898c54 (diff) |
create tremded branch
Diffstat (limited to 'scripts/granger/lib/string.lua')
-rw-r--r-- | scripts/granger/lib/string.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/granger/lib/string.lua b/scripts/granger/lib/string.lua new file mode 100644 index 0000000..56fae4d --- /dev/null +++ b/scripts/granger/lib/string.lua @@ -0,0 +1,50 @@ +-- +-- string.lua +-- Additions to Lua's built-in string functions. +-- Copyright (c) 2002-2008 Jason Perkins and the Premake project +-- + + +-- +-- Returns an array of strings, each of which is a substring of s +-- formed by splitting on boundaries formed by `pattern`. +-- + + function string.explode(s, pattern, plain) + if (pattern == '') then return false end + local pos = 0 + local arr = { } + for st,sp in function() return s:find(pattern, pos, plain) end do + table.insert(arr, s:sub(pos, st-1)) + pos = sp + 1 + end + table.insert(arr, s:sub(pos)) + return arr + end + + + +-- +-- Find the last instance of a pattern in a string. +-- + + function string.findlast(s, pattern, plain) + local curr = 0 + repeat + local next = s:find(pattern, curr + 1, plain) + if (next) then curr = next end + until (not next) + if (curr > 0) then + return curr + end + end + + + +-- +-- Returns true if `haystack` starts with the sequence `needle`. +-- + + function string.startswith(haystack, needle) + return (haystack:find(needle, 1, true) == 1) + end |