-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringLib.lua
337 lines (252 loc) · 5.71 KB
/
stringLib.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
function tobool(s)
if (s == "true") then
return true
end
if (s == "false") then
return false
end
return nil
end
function boolToString(b)
if b then
return "true"
end
return "false"
end
function stringToType(val, type)
assert(val, 'no value')
assert(type, 'no type')
if (type == 'boolean') then
return tobool(val)
end
if (type == 'number') then
return tonumber(val)
end
assert((type ~= 'table'), 'cannot convert to table')
return val
end
string.endsWith = function(s, target)
return (s:sub(s:len() - target:len() + 1, s:len()) == target)
end
string.trimStartWhitespace = function(s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
string.trimSurroundingWhitespace = function(s)
local pos = s:find("[^ \t]")
local posEnd = s:len() - s:reverse():find("[^ \t]") + 1
return s:sub(pos, posEnd)
end
string.trim = function(s, rem)
while s:find(rem, 1, true) do
s = s:sub(1, s:find(rem, 1, true) - 1)..s:sub(s:find(rem, 1, true) + 1)
end
return s
end
string.quote = function(s)
return '\"'..s..'\"'
end
string.dequote = function(s)
s = s:gsub('"', '')
return s
end
string.split = function(s, delimiter)
if (s == nil) then
return nil
end
if (s == '') then
return {}
end
if (type(s) ~= 'string') then
s = tostring(s)
end
local results = {}
local resultsCount = 0
while s:find(delimiter) do
resultsCount = resultsCount + 1
results[resultsCount] = s:sub(1, s:find(delimiter) - 1)
s = s:sub(s:find(delimiter) + 1)
end
resultsCount = resultsCount + 1
results[resultsCount] = s
return results
end
string.doubleBackslashes = function(s)
return s:gsub('\\', '\\\\')
end
--[[string.lastFind = function(s, target)
local result = 0
while s:find(target, result + 1, true) do
result = s:find(target, result + 1, true)
end
if (result == 0) then
return nil
end
return result
end]]
string.lastFind = function(s, target)
local lastPos, lastPosEnd
local pos, posEnd = s:find(target)
while pos do
lastPos, lastPosEnd = pos, posEnd
pos, posEnd = s:find(target, posEnd + 1)
end
return lastPos, lastPosEnd
end
function concat(a, b)
return (tostring(a)..tostring(b))
end
function isPlainText(s)
assert(s, 'no value')
assert(type(s) == 'string', 'value is not a string '..'('..table.concat({tostring(s)}, ', ')..')')
if s:find('\\') then
return true
end
while s:find('.', 1, true) do
s = s:sub(s:find('.', 1, true) + 1)
end
if (s == '') then
return true
end
if (tonumber(s) or ((s:sub(1, 1) == "'") and (s:sub(s:len(), s:len()) == "'"))) then
return false
end
if (s:len() == 4) then
return true
end
local c = 1
local ch = s:sub(c, c)
while ((((ch >= 'A') and (ch <= 'Z')) or tonumber(ch) or (ch == '_')) and (c <= s:len())) do
c = c + 1
ch = s:sub(c, c)
end
if (c > s:len()) then
return false
end
return true
end
string.isWhitespace = function(s, ex)
if ex then
return (s:match'^%s*(.*)' == "")
end
return (s == "")
end
string.debracket = function(s, startToken, endToken)
if (s:sub(1, 1) == startToken) then
s = s:sub(2, s:len())
end
if (s:sub(s:len(), s:len()) == endToken) then
s = s:sub(1, s:len() - 1)
end
return s
end
function debracket(s, startToken, endToken)
return s:debracket(startToken, endToken)
end
local chars = {}
for i = 0, 9, 1 do
chars[i] = i
end
chars[10] = "A"
chars[11] = "B"
chars[12] = "C"
chars[13] = "D"
chars[14] = "E"
chars[15] = "F"
chars[16] = "G"
chars[17] = "H"
chars[18] = "I"
chars[19] = "J"
chars[20] = "K"
chars[21] = "L"
chars[22] = "M"
chars[23] = "N"
chars[24] = "O"
chars[25] = "P"
chars[26] = "Q"
chars[27] = "R"
chars[28] = "S"
chars[29] = "T"
chars[30] = "U"
chars[31] = "V"
chars[32] = "W"
chars[33] = "X"
chars[34] = "Y"
chars[35] = "Z"
charsC = 36
function getAscii(val, digits)
local c = val
local s = ""
for i = 3, 0, -1 do
local val = math.floor(c / math.pow(charsC, i))
s = s..chars[val]
c = c % math.pow(charsC, i)
end
return s:sub(s:len() - digits + 1, s:len())
end
string.findInner = function(s, startDel, endDel)
local searchPat = "%b"..startDel..endDel
local startPos, endPos = string.find(s, searchPat)
if not startPos then
return nil, nil
end
local newStartPos, newEndPos = string.find(s, searchPat, startPos + 1)
while (newEndPos and (newEndPos < endPos)) do
startPos = newStartPos
endPos = newEndPos
newStartPos, newEndPos = string.find(s, searchPat, startPos + 1)
end
return startPos, endPos
end
string.splitOuter = function(s, delimiter, encaps)
if (s == nil) then
return nil
end
if (s == "") then
return {}
end
if (type(s) ~= "string") then
s = tostring(s)
end
local s2 = s
for i = 1, #encaps, 1 do
local pos, posEnd = s2:findInner(encaps[i][1], encaps[i][2])
while pos do
s2 = s2:sub(1, pos - 1)..string.rep("#", encaps[i][1]:len())..s2:sub(pos + 1, posEnd - 1):gsub(delimiter, string.rep("#", delimiter:len()))..string.rep("#", encaps[i][2]:len())..s2:sub(posEnd + 1, s2:len())
pos, posEnd = s2:findInner(encaps[i][1], encaps[i][2])
end
end
local results = {}
local pos, posEnd = s2:find(delimiter, 1, true)
local lastPosEnd = 0
while pos do
results[#results + 1] = s:sub(lastPosEnd + 1, pos - 1)
lastPosEnd = posEnd
pos, posEnd = s2:find(delimiter, posEnd + 1, true)
end
results[#results + 1] = s:sub(lastPosEnd + 1, s:len())
return results
end
function nilToString(s, inval)
if (s == nil) then
return tostring(inval)
end
return tostring(s)
end
function valToLua(val)
if (val == nil) then
return 'nil'
end
if (type(val) == 'table') then
return tableToLua(val)
end
if (type(val) == 'boolean') then
return boolToString(val)
end
if (type(val) == 'number') then
return tostring(val)
end
if (type(val) == 'string') then
return string.format('%q', val)
end
return nil
end