Repository navigation
Expand file tree
/
Copy pathString.lua
More file actions
430 lines (379 loc) · 10.6 KB
/
Copy pathString.lua
File metadata and controls
430 lines (379 loc) · 10.6 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
--[[
Copyright 2017 YANG Huan ([email protected]).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local System = System
local throw = System.throw
local ArgumentException = System.ArgumentException
local ArgumentNullException = System.ArgumentNullException
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
local FormatException = System.FormatException
local IndexOutOfRangeException = System.IndexOutOfRangeException
local unpack = table.unpack
local string = string
local schar = string.char
local table = table
local tinsert = table.insert
local tconcat = table.concat
local setmetatable = setmetatable
local select = select
local type = type
local String = string
String.traceback = System.emptyFn -- make throw(str) not fail
local function check(s, startIndex, count)
local len = #s
startIndex = startIndex or 0
if startIndex < 0 or startIndex > len then
throw(ArgumentOutOfRangeException("startIndex"))
end
count = count or len - startIndex
if count < 0 or count > len - startIndex then
throw(ArgumentOutOfRangeException("count"))
end
return startIndex, count, len
end
local function ctor(_, ...)
local len = select("#", ...)
if len == 2 then
local c, count = ...
if count <= 0 then
throw(ArgumentOutOfRangeException("count"))
end
return schar(c):rep(count)
end
local value, startIndex, length = ...
startIndex, length = check(value, startIndex, length)
return schar(unpack(value, startIndex + 1, startIndex + length))
end
local function compare(strA, strB, ignoreCaseOrType, cultureInfo)
if strA == nil then
return(strB == nil) and 0 or -1
end
if strB == nil then
return 1
end
if ignoreCaseOrType ~= nil then
if type(ignoreCaseOrType) == "number" then
-- StringComparison
if ignoreCaseOrType % 2 ~= 0 then
strA = strA:lower()
strB = strB:lower()
end
else
-- ignoreCase
if ignoreCaseOrType then
strA = strA:lower()
strB = strB:lower()
end
if cultureInfo then
-- CultureInfo
throw(System.NotSupportedException("cultureInfo is not support"))
end
end
end
if strA > strB then return 1 end
if strA < strB then return -1 end
return 0
end
String.Compare = compare
function String.CompareTo(this, v)
return compare(this, v)
end
function String.CompareToObj(this, v)
if v == nil then return 1 end
if type(v) ~= "string" then
throw(ArgumentException("Arg_MustBeString"))
end
return compare(this, v)
end
function String.Equals(this, v, comparisonType)
return compare(this, v, comparisonType) == 0
end
function String.EqualsObj(this, v)
if type(v) == "string" then
return this == v
end
return false
end
function String.GetType(this)
return System.typeof(String)
end
String.ToString = tostring
function String.get(this, index)
if index < 0 or index >= #this then
throw(IndexOutOfRangeException())
end
return this:byte(index + 1)
end
function String.Concat(...)
local t = {}
local len = select("#", ...)
if len == 1 then
local v = ...
if System.isEnumerableLike(v) then
for _, v in System.each(array) do
tinsert(t, v:ToString())
end
else
return v:ToString()
end
else
for i = 1, len do
local v = select(i, ...)
tinsert(t, v:ToString())
end
end
return tconcat(t)
end
function String.Join(separator, value, startIndex, count)
local t = {}
if startIndex then
check(value, startIndex, count)
for i = startIndex + 1, startIndex + count do
local v = value:get(i)
if v ~= nil then
tinsert(t, v)
end
end
else
for _, v in System.each(value) do
if v ~= nil then
tinsert(t, v)
end
end
end
return tconcat(t, separator)
end
local function checkIndexOf(str, value, startIndex, count, comparisonType)
if value == nil then
throw(ArgumentNullException("value"))
end
startIndex, count = check(str, startIndex, count)
str = str:sub(startIndex + 1, startIndex + count)
if comparisonType and comparisonType % 2 ~= 0 then
str = str:lower()
value = value:lower()
end
return str, value, startIndex
end
function String.LastIndexOf(str, value, startIndex, count, comparisonType)
if type(value) == "number" then
value = schar(value)
end
str, value, startIndex = checkIndexOf(str, value, startIndex, count, comparisonType)
local index = str:match(".*()" .. value)
if index then
return index - 1 + startIndex
end
return -1
end
local function indexOfAny(str, chars, startIndex, count)
if chars == nil then
throw(ArgumentNullException("chars"))
end
startIndex, count = check(str, startIndex, count)
str = str:sub(startIndex + 1, startIndex + count)
return str, "[" .. schar(unpack(chars)) .. "]", startIndex
end
function String.LastIndexOfAny(str, chars, startIndex, count)
str, chars, startIndex = indexOfAny(str, chars, startIndex, count)
local index = str:match("^.*()" .. chars)
if index then
return index - 1 + startIndex
end
return -1
end
function String.IsNullOrWhiteSpace(value)
return value == nil or value:find("^%s*$") ~= nil
end
function String.IsNullOrEmpty(value)
return value == nil or #value == 0
end
function String.Format(format, ...)
local len = select("#", ...)
if len == 1 then
local args = ...
if System.isArrayLike(args) then
return format:gsub("{(%d)}", function(n)
local v = args:get(n + 0) -- make n to number
if v == nil then
throw(FormatException())
end
return v:ToString()
end)
end
end
local args = { ... }
return format:gsub("{(%d)}", function(n)
local v = args[n + 1]
if v == nil then
throw(FormatException())
end
return v:ToString()
end)
end
function String.StartsWith(this, prefix)
return this:sub(1, #prefix) == prefix
end
function String.EndsWith(this, suffix)
return suffix == "" or this:sub(-#suffix) == suffix
end
function String.Contains(this, value)
if value == nil then
throw(ArgumentNullException("value"))
end
return this:find(value) ~= nil
end
function String.IndexOfAny(str, chars, startIndex, count)
str, chars, startIndex = indexOfAny(str, chars, startIndex, count)
local index = str:find(chars)
if index then
return index - 1 + startIndex
end
return -1
end
function String.IndexOf(str, value, startIndex, count, comparisonType)
if type(value) == "number" then
value = schar(value)
end
str, value, startIndex = checkIndexOf(str, value, startIndex, count, comparisonType)
local index = str:find(value)
if index then
return index - 1 + startIndex
end
return -1
end
function String.ToCharArray(str, startIndex, count)
startIndex, count = check(str, startIndex, count)
local t = { }
for i = startIndex + 1, startIndex + count do
tinsert(t, str:byte(i))
end
return System.arrayFromTable(t, System.Char)
end
local function escape(s)
return s:gsub("([%%%^%.])", "%%%1")
end
function String.Replace(this, a, b)
if type(a) == "number" then
a = schar(a)
b = schar(b)
end
a = escape(a)
return this:gsub(a, b)
end
function String.Insert(this, startIndex, value)
if value == nil then
throw(ArgumentNullException("value"))
end
startIndex = check(this, startIndex)
return this:sub(1, startIndex) .. value .. this:sub(startIndex + 1)
end
function String.Remove(this, startIndex, count)
startIndex, count = stringCheck(this, startIndex, count)
return this:sub(1, startIndex) .. this:sub(startIndex + 1 + count)
end
function String.Substring(this, startIndex, count)
startIndex, count = check(str, startIndex, count)
return this:sub(startIndex + 1, startIndex + count)
end
local function findAny(s, strings, startIndex)
local findBegin, findEnd, findStr
for _, str in ipairs(strings) do
local pattern = escape(str)
local posBegin, posEnd = string.find(s, pattern, startIndex)
if posBegin then
if not findBegin or posBegin < findBegin then
findBegin, findEnd, findStr = posBegin, posEnd, str
end
end
end
return findBegin, findEnd, findStr
end
String.FindAny = findAny
function String.Split(this, strings, count, options)
local t = {}
local find = string.find
if type(strings) == "table" then
if #strings == 0 then
return t
end
if type(strings[1]) == "string" then
find = findAny
else
strings = schar(unpack(strings))
strings = escape(strings)
strings = "[" .. strings .. "]"
end
elseif type(strings) == "string" then
strings = escape(strings)
else
strings = schar(strings)
strings = escape(strings)
end
local startIndex = 1
while true do
local posBegin, posEnd = find(this, strings, startIndex)
posBegin = posBegin or 0
local subStr = this:sub(startIndex, posBegin -1)
if options ~= 1 or #subStr > 0 then
tinsert(t, subStr)
if count then
count = count -1
if count == 0 then
break
end
end
end
if posBegin == 0 then
break
end
startIndex = posEnd + 1
end
return System.arrayFromTable(t, String)
end
String.ToLower = string.lower
String.ToUpper = string.upper
function String.TrimEnd(this, chars)
if chars then
chars = schar(unpack(chars))
chars = escape(chars)
chars = "(.-)[" .. chars .. "]*$"
else
chars = "(.-)%s*$"
end
return (this:gsub(chars, "%1"))
end
function String.TrimStart(this, chars)
if chars then
chars = schar(unpack(chars))
chars = escape(chars)
chars = "^[" .. chars .. "]*(.-)"
else
chars = "^%s*(.-)"
end
return (this:gsub(chars, "%1"))
end
function String.Trim(this, chars)
if chars then
chars = schar(unpack(chars))
chars = escape(chars)
chars = "^[" .. chars .. "]*(.-)[" .. chars .. "]*$"
else
chars = "^%s*(.-)%s*$"
end
return (this:gsub(chars, "%1"))
end
function String.__inherits__()
return { System.IComparable, System.IEnumerable, System.IComparable_1(String), System.IEnumerable_1(String), System.IEquatable_1(String) }
end
System.define("System.String", String)
setmetatable(String, { __index = System.Object, __call = ctor })