-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.lua
More file actions
109 lines (88 loc) · 2.24 KB
/
str.lua
File metadata and controls
109 lines (88 loc) · 2.24 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
local mq = require('mq')
local sub = string.sub
local gsub = string.gsub
local find = string.find
local len = string.len
local gmatch = string.gmatch
local format = string.format
local str = {}
function str.AsNumber(s, default)
local num = default
if s then num = tonumber(s) end
return num
end
function str.Trim(s)
return gsub(s, "^%s*(.-)%s*$", "%1")
end
-- function str.Split(s, sep)
-- local res = {}
-- local start2 = mq.gettime()
-- for i = 1, 1000000 do
-- res = str.Split2(s, sep)
-- end
-- local elapsed2 = mq.gettime() - start2
-- local start1 = mq.gettime()
-- for i = 1, 1000000 do
-- res = str.Split1(s, sep)
-- end
-- local elapsed1 = mq.gettime() - start1
-- print('s: ' .. s .. ' ; sep: ' .. sep)
-- print('1: ' .. elapsed1 .. ' ; 2: ' .. elapsed2)
-- return res
-- end
function str.Split(s, sep)
--print('s: ' .. s .. ' ; sep: ' .. sep)
local result = { }
if #s == 0 then return result end
local idx = 1
local from = 1
local delim_from, delim_to = find(s, sep, from)
while delim_from do
result[idx] = sub(s, from, delim_from - 1)
idx = idx + 1
from = delim_to + 1
delim_from, delim_to = find(s, sep, from)
end
local part = sub(s, from)
if #part > 0 then
result[idx] = part
end
return result
end
-- function str.Split2(s, sep)
-- local result = {}
-- if #s == 0 then return result end
-- local idx = 1
-- local regex = format("([^%s]+)", sep)
-- for each in gmatch(s, regex) do
-- result[idx] = each
-- idx = idx + 1
-- end
-- return result
-- end
function str.IsEmpty(s)
return s == nil or len(s) == 0
end
function str.Insert(s1, s2, pos)
return sub(s1, 1, pos) .. s2 .. sub(s1, pos + 1)
end
function str.StartsWith(s, start)
return sub(s, 1, #start) == start
end
function str.EndsWith(s, ending)
return ending == "" or sub(s, -#ending) == ending
end
function str.Join(arr, sep, start)
sep = sep or ''
start = start or 1
local joined = arr[start] or ''
local size = #arr
for i = start + 1, size do
joined = joined .. sep .. arr[i]
end
return joined
end
function str.FirstToUpper(s)
return gsub(s, "^%l", string.upper)
end
return str