-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.lua
More file actions
72 lines (56 loc) · 1.21 KB
/
string_utils.lua
File metadata and controls
72 lines (56 loc) · 1.21 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local M = {}
local start_line_pattern = "^(.-)"
local end_line_pattern = "(.*)$"
---@param str string
---@return boolean
function M.isEmpty(str)
return #str == 0
end
---@param str string
---@return string
function M.trimr(str)
local result = string.gsub(str, "%s+$", "")
return result
end
---@param str string
---@return string
function M.triml(str)
local result = string.gsub(str, "^%s+", "")
return result
end
---@param str string
---@return string
function M.trim(str)
return M.triml(M.trimr(str))
end
---@param str string
---@param sep string
---@return table
function M.split(str, sep)
if #str == 0 or #sep == 0 then
return { str }
end
local matched = str:match(start_line_pattern .. sep)
if matched == nil then
return { str }
end
local result = {}
if matched ~= "" then
table.insert(result, matched)
end
for _, splittedStr in ipairs(M.split(str:match(sep .. end_line_pattern), sep)) do
if not M.isEmpty(splittedStr) then
table.insert(result, splittedStr)
end
end
return result
end
---@param str string
---@return boolean
function M.isNilOrEmpty(str)
if not str then
return true
end
return M.isEmpty(str)
end
return M