summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/genie/src/base/string.lua
blob: f12fdbe444f488460bcb404e316149a89488c015 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--
-- 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

	--
	-- remove leading and trailing whitespace from string.
	--

	function string.trim(s)
		return (s:gsub("^%s*(.-)%s*$", "%1"))
	end