This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
88 lines (72 loc) · 2.39 KB
/
init.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
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
local StringUtils = {}
function StringUtils.trim(str)
local from = string.match(str, "^%s*()")
return from > #str and "" or string.match(str, ".*%S", from) -- there is no escaping 2n complexity, but we can prevent n^2
end
function StringUtils.trimStart(str)
local from = string.match(str, "^%s*()")
return from > #str and "" or string.sub(str, from)
end
function StringUtils.trimEnd(str)
local _, from = string.find(str, "^%s*") -- checking this prevents quadratic backtracking
return from == #str and "" or string.match(str, ".*%S") -- there is no escaping 2n/3n complexity, but we can prevent n^2
end
-- https://github.com/tc39/proposal-string-pad-start-end/blob/master/polyfill.js
local function getPadding(str, maxLength, fillString)
local stringLength = #str
if maxLength <= stringLength then return str end
local filler = fillString == nil and " " or fillString
if filler == "" then return str end
local fillLen = maxLength - stringLength
while #filler < fillLen do
local fLen = #filler
local remainingCodeUnits = fillLen - fLen
if fLen > remainingCodeUnits then
filler = filler .. string.sub(filler, 1, remainingCodeUnits)
else
filler = filler .. filler
end
end
return string.sub(filler, 1, fillLen)
end
function StringUtils.padStart(str, maxLength, fillString)
return getPadding(str, maxLength, fillString) .. str
end
function StringUtils.padEnd(str, maxLength, fillString)
return str .. getPadding(str, maxLength, fillString)
end
function StringUtils.slice(str, i, j)
if j ~= nil and j < 0 then j = j - 1 end
return string.sub(str, i + 1, j)
end
function StringUtils.indexOf(str, searchElement, fromIndex)
return (string.find(str, searchElement, (fromIndex or 0) + 1, true) or 0) - 1
end
function StringUtils.includes(str, searchElement, fromIndex)
return string.find(str, searchElement, (fromIndex or 0) + 1, true) ~= nil
end
function StringUtils.startsWith(str1, str2, pos)
local n1 = #str1
local n2 = #str2
if pos == nil or pos ~= pos then
pos = 0
else
pos = math.clamp(pos, 0, n1)
end
local last = pos + n2
return last <= n1 and string.sub(str1, pos + 1, last) == str2
end
function StringUtils.endsWith(str1, str2, pos)
local n1 = #str1
local n2 = #str2
if pos == nil then
pos = n1
elseif pos ~= pos then
pos = 0
else
pos = math.clamp(pos, 0, n1)
end
local start = pos - n2 + 1
return start > 0 and string.sub(str1, start, pos) == str2
end
return StringUtils