-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.lua
47 lines (39 loc) · 1.51 KB
/
string.lua
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
-- Provide the ability to encapsulate the extensions into a different table.
local _,_,module = ...
module = module or string
assert(type(module) == 'table', 'must provide a table to extend')
--- Recipes for common string manipulations in Lua ---
-- Returns true if the given string starts with another, otherwise returns false.
function module.startsWith(str, query)
return string.sub(str, 1, string.len(query)) == query
end
-- Returns true if the given string ends with another, otherwise returns false.
function module.endsWith(str, query)
return query == '' or string.sub(str, -string.len(query)) == query
end
-- Removes initial and trailing whitespace
-- @see http://lua-users.org/wiki/StringTrim, 'trim6' implementation
function module.trim(str)
return str:match'^()%s*$' and '' or str:match'^%s*(.*%S)'
end
-- Given a string, one presumably composed of a sequence of tokens joined by underscores,
-- returns the given string with the underscores replaced by a single space, along with the number of
-- underscores replaced.
function module.unchain(str)
return str:gsub('_', ' ')
end
-- Given a string, return a version with the given suffix removed.
function module.chop(str, suffix_query)
local s = str:gsub(suffix_query..'$', '', 1)
return s
end
-- Simple splitter function: divides a string into chunks based on the given delimeter.
function module.split(str, delim)
local chunks = {}
for chunk in str:gmatch('[^'..delim..']*') do
table.insert(chunks, chunk)
end
return chunks
end
-----------
return module