-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtools.lua
More file actions
5944 lines (5869 loc) · 192 KB
/
tools.lua
File metadata and controls
5944 lines (5869 loc) · 192 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- tools:[2025-04-29_22:21:33]
-- file:[./files/lua.lua]
function null()
return null
end
function is_userdata(v)
return type(v) == 'userdata'
end
function is_table(v)
return type(v) == 'table'
end
function is_array(v)
return type(v) == 'table' and #v == table.count(v)
end
function is_string(v)
return type(v) == 'string'
end
function is_number(v)
return type(v) == 'number'
end
function is_boolean(v)
return type(v) == 'boolean'
end
function is_nil(v)
return type(v) == 'nil' or v == null
end
function is_empty(v)
if is_nil(v) then
return true
end
if is_table(v) then
return next(v) == nil
end
return false
end
function is_function(v)
return type(v) == 'function'
end
function is_class(v)
return is_table(v) and v.__type__ == 'class'
end
function is_object(v)
return is_table(v) and v.__type__ == 'object'
end
if not rawget(_G, "lua_print") then
rawset(_G, "lua_print", print)
end
function print(...)
local args = {...}
for i=1,select("#", ...) do
local v = args[i]
if v == null then
v = "null"
elseif is_class(v) or is_object(v) then
v = tostring(v)
elseif is_table(v) then
v = table.printable(v, " ")
else
v = tostring(v)
end
io.write(v, " ")
end
io.write('\n')
end
function to_type(v, tp)
if type(v) == tp then
return v
elseif tp == 'string' then
return tostring(v)
elseif tp == 'number' then
return tonumber(v)
elseif tp == 'boolean' then
if is_string(v) then
v = v:lower()
if v == 'true' then
return true
elseif v == 'false' then
return false
else
return nil
end
elseif is_number(v) then
if v == 1 then
return true
elseif v == 0 then
return false
else
return nil
end
else
return nil
end
else
return nil
end
end
function lua_to_string(t)
local m = getmetatable(t)
if m and m.__tostring then
local tmp = m.__tostring
m.__tostring = nil
local ret = tostring(t)
m.__tostring = tmp
return ret
else
return tostring(t)
end
end
function lua_get_pointer(v)
local t = type(v)
if t == "function" or t == "table" then
local s = lua_to_string(v):explode(": ")
return s[2]
else
return nil
end
end
function lua_set_debug(enable)
rawset(_G, 'lua-is-debug', enable == true)
end
function lua_is_debug()
return rawget(_G, 'lua-is-debug') == true
end
function lua_set_user(user)
assert(#user >= 3, 'invalid user format')
assert(string.match(user, '%w+'), 'invalid user format')
rawset(_G, 'lua-user-name', user)
end
function lua_get_user()
return rawget(_G, 'lua-user-name') or 'unknown'
end
function lua_script_path(level)
level = level or 0
local info = debug.getinfo(2 + level, "S")
if info and info.source and info.source:sub(1, 1) == "@" then
return info.source:sub(2)
end
return nil
end
function lua_new_decorator(func)
assert(func == nil or is_function(func))
local function _call(self, ...)
local args = {...}
if self._bFunc then
local _results = {self._bFunc(unpack(args))}
if #_results > 0 then
args = _results
end
end
assert(self._fFunc, 'decorator func not found')
local results = nil
if not self._eFunc then
results = {self._fFunc(unpack(args))}
else
xpcall(function()
results = {self._fFunc(unpack(args))}
end, function(e)
results = {self._eFunc(e)}
end)
end
assert(results ~= nil, 'decorator logic eror found')
if self._aFunc then
local _results = {self._aFunc(unpack(results))}
if #_results > 0 then
results = _results
end
end
return unpack(results)
end
local decorator = {
_bFunc = nil,
_fFunc = func,
_eFunc = nil,
_aFunc = nil,
}
function decorator:before(func)
assert(func == nil or is_function(func))
self._bFunc = func
return self
end
function decorator:after(func)
assert(func == nil or is_function(func))
self._aFunc = func
return self
end
function decorator:error(func)
assert(func == nil or is_function(func))
self._eFunc = func
return self
end
function decorator:func(func)
assert(func == nil or is_function(func))
self._fFunc = func
return self
end
function decorator:call(...)
return _call(self, ...)
end
setmetatable(decorator, {__call = _call})
return decorator
end
local function __lua_delegate_func(t, k)
local v = rawget(t, k)
if v ~= nil then
return
end
local _meta = rawget(t, "__delegated")
local _dlgt = rawget(t, "__delegation")
if _meta ~= nil and _meta.__index ~= nil and _meta.__index ~= __lua_delegate_func then
v = _meta.__index[k]
end
if v ~= nil then
return v
end
if is_function(_dlgt) then
v = function(...) return _dlgt(k, ...) end
elseif is_userdata(_dlgt) then
if is_function(_dlgt[k]) then
v = function(_t, ...) return _dlgt[k](_dlgt, ...) end
else
v = _dlgt[k]
end
end
return v
end
function lua_set_delegate(obj, delegation)
rawset(obj, '__delegation', delegation)
if rawget(obj, '__delegated') ~= nil or delegation == nil then return end
local _meta = getmetatable(obj)
rawset(obj, '__delegated', _meta)
setmetatable(obj, {__index = __lua_delegate_func})
end
-- file:[./files/number.lua]
number = number or {}
function number.is_odd(v)
return v % 2 ~= 0
end
function number.is_even()
return not number.is_odd(v)
end
function number.is_int(v)
return math.floor(v) == v
end
function number.is_float(v)
return not number.is_int(v)
end
-- file:[./files/math.lua]
function math.radian(angle)
return angle * math.pi / 180
end
function math.angle(radian)
return radian * 180 / math.pi
end
function math.round(value)
return math.floor(value + 0.5)
end
-- file:[./files/string.lua]
function string.new(v)
assert(v ~= string)
assert(v == nil or type(v) == 'string')
return v or ""
end
function string.append(this, other)
return this .. other
end
function string.prepend(this, other)
return other .. this
end
function string.ltrim(this, pattern)
pattern = pattern or " \t\n\r"
return string.gsub(this, "^[" .. pattern .. "]+", "")
end
function string.rtrim(this, pattern)
pattern = pattern or " \t\n\r"
return string.gsub(this, "[" .. pattern .. "]+$", "")
end
function string.trim(this, pattern)
return this:ltrim(pattern):rtrim(pattern)
end
function string.slash(this)
return this:gsub('\\', '/')
end
function string.implode(t, separator)
return table.concat(t, separator)
end
function string.explode(this, separator, maxCount)
assert(is_string(this))
assert(is_string(separator))
local splitArray = table.new()
if #this == 0 then
return splitArray
end
if #separator == 0 then
for i=1,#this do
if #splitArray >= maxCount then
splitArray[i] = string.sub(this, i, string.len(this))
break
end
splitArray[i] = string.sub(this, i, i)
end
return splitArray
end
local startIndex = 1
local splitIndex = 1
while true do
local foundIndex, endIndex = string.find(this, separator, startIndex)
if not foundIndex or (maxCount and #splitArray >= maxCount) then
splitArray[splitIndex] = string.sub(this, startIndex, string.len(this))
break
end
splitArray[splitIndex] = string.sub(this, startIndex, foundIndex - 1)
startIndex = foundIndex + (endIndex - foundIndex + 1)
splitIndex = splitIndex + 1
end
return splitArray
end
function string.valid(v)
return type(v) == 'string' and #v > 0
end
function string.center(this, len, char)
len = math.max(len, 0)
local need = len - #this
if need <= 0 then
return this:sub(1, len)
else
local l = math.floor(need / 2)
local r = need - l
return char:rep(l) .. this .. char:rep(r)
end
end
function string.left(this, len, char)
len = math.max(len, 0)
local need = len - #this
return need <= 0 and this:sub(1, len) or this .. char:rep(need)
end
function string.right(this, len, char)
len = math.max(len, 0)
local need = len - #this
return need <= 0 and this:sub(#this - len + 1, #this) or char:rep(need) .. this
end
function string.table(this)
local f = loadstring("return " .. this)
if not f then return end
local t = f()
if not is_table(t) then return end
return table.new(t)
end
function string.encode(tb)
assert(is_table(tb))
return tb:string()
end
function string.decode(this)
assert(is_string(this))
return this:table()
end
function string.print(this)
print(this)
end
function string.execute(this)
local f = loadstring(this)
assert(is_function(f), "invalid script string")
return f()
end
function string.escape(s)
s = s:gsub('%%', '%%%%')
:gsub('^%^', '%%^')
:gsub('%$$', '%%$')
:gsub('%(', '%%(')
:gsub('%)', '%%)')
:gsub('%.', '%%.')
:gsub('%[', '%%[')
:gsub('%]', '%%]')
:gsub('%*', '%%*')
:gsub('%+', '%%+')
:gsub('%-', '%%-')
:gsub('%?', '%%?')
return s
end
function string.starts(this, s)
return string.sub(this, 1, #s) == s
end
function string.ends(this, s)
return string.sub(this, -#s, -1) == s
end
function string.limit(this, length, suffix)
assert(length > 0, 'invalid limit length')
suffix = suffix or "..."
assert(length > #suffix, 'invalid limit length')
if #this <= length then
return this
else
return stirng.sub(this, 1, length - #suffix) .. suffix
end
end
function string.render(this, ...)
local args = {...}
if is_table(args[1]) then
args = args[1]
end
local rgxp = is_array(args) and "{(%d+)}" or "{(%w+)}"
local result = this:gsub(rgxp, function(val)
local key = is_array(args) and tonumber(val) or val
return args[key] and tostring(args[key]) or "{" .. key .. "}"
end)
return result
end
-- file:[./files/table.lua]
function table.new(t, mode)
assert(t ~= table)
assert(t == nil or type(t) == 'table')
return setmetatable(t or {}, {__index = table, __mode = mode})
end
function table.new_with_weak_key(t)
return table.new(t, 'k')
end
function table.new_with_weak_value(t)
return table.new(t, 'v')
end
function table.clear(this)
for k,v in pairs(this) do
this[k] = nil
end
return this
end
function table.keys(this)
local keys = table.new()
for key, _ in pairs(this) do
table.insert(keys, key)
end
return keys
end
function table.values(this)
local values = table.new()
for _, value in pairs(this) do
table.insert(values, value)
end
return values
end
function table.merge(this, that)
for k,v in pairs(that) do
this[k] = v
end
return this
end
function table.sub(this, from, to)
assert(is_array(this))
local ret = {}
local len = #this
from = from or 1
to = to or #this
if from < 0 then
from = from + len + 1
end
if to < 0 then
to = to + len + 1
end
from = math.max(0, math.min(from, len))
to = math.max(0, math.min(to, len))
for i=from,to do
table.insert(ret, this[i])
end
return ret
end
function table.filter(this, func)
local ret = {}
table.foreach(this, function(k, v)
if func(k, v) then
if is_array(this) then
table.insert(ret, v)
else
ret[k] = v
end
end
end)
return ret
end
function table.copy(this)
local tbs = table.new()
local function cp(t)
if type(t) ~= 'table' then return t end
local ret = tbs[t]
if ret then return ret end
ret = table.new()
tbs[t] = ret
for k, v in pairs(t) do
ret[k] = cp(v)
end
return ret
end
return cp(this)
end
function table.count(this, countKeyType, countValueType)
local totalCount = 0
local keyCount = 0
local valueCount = 0
local k,v = next(this)
while k do
totalCount = totalCount + 1
if countKeyType and type(k) == countKeyType then
keyCount = keyCount + 1
end
if countValueType and type(v) == countValueType then
valueCount = valueCount + 1
end
k, v = next(this, k)
end
return totalCount, keyCount, valueCount
end
function table.is_array(this)
return is_array(this)
end
function table.implode(this, separator)
return table.concat(this, separator)
end
function table.explode(s, separator, maxCount)
return string.explode(s, separator, maxCount)
end
function table.read_from_file(path)
assert(is_string(path))
local c = files.read(path)
if not c then return end
return c:table()
end
function table.write_to_file(this, path)
assert(is_table(this))
assert(is_string(path))
files.write(path, table.string(this))
end
local function to_string(v)
local t = type(v)
if t == 'nil' or t == 'number' or t == 'string' then
return tostring(v)
end
end
local function convert_key(key, isEcho)
local t = type(key)
if t == 'number' then
return isEcho and "(" .. tostring(key) .. ")" or "[" .. key .. "]"
elseif t == 'string' then
return isEcho and "[" .. key .. "]" or tostring(key)
end
end
local function convert_value(value, isEcho)
local t = type(value)
if t == 'boolean' then
return tostring(value)
elseif t == 'number' then
return "" .. value .. ""
elseif t == 'string' then
return "\"" .. value .. "\""
elseif not isEcho then
return
elseif t == 'function' then
return "[" .. tostring(value) .. "]"
elseif is_class(value) then
return "[Class:" .. tostring(value) .. "]"
elseif is_object(value) then
return "[Object:" .. tostring(value) .. "]"
else
return tostring(value)
end
end
function table.string(this, blank, keys, isEcho, withHidden, arrKeyless, _storey, _record)
assert(is_table(this))
_storey = _storey or 1
_record = _record or {}
_record[this] = true
local result = table.new()
blank = blank or " "
local function try_convert(k, v, ignoreKey)
local valid = withHidden or not string.starts(k, "__")
local key = convert_key(k, isEcho)
local value = nil
if is_table(v) then
if _record[v] then
value = tostring(v)
else
value = table.string(v, blank, keys, isEcho, withHidden, arrKeyless, _storey + 1, _record)
end
else
value = convert_value(v, isEcho)
end
if valid and key and value then
if ignoreKey then
result:insert(blank:rep(_storey) .. value)
else
result:insert(blank:rep(_storey) .. key .. " = " .. value)
end
end
end
if table.is_array(this) then
for i,v in ipairs(this) do try_convert(i, v, arrKeyless) end
elseif keys then
for i,k in ipairs(keys) do
local v = this[k]
if v then
try_convert(k, v)
end
end
else
for k,v in pairs(this) do try_convert(k, v) end
end
local content = ""
if #result > 0 then
content = "\n" .. result:implode(",\n") .. ",\n"
content = content .. blank:rep(_storey - 1)
end
return string.new("{" .. content .. "}")
end
function table.printable(this, blank, keys)
return table.string(this, blank, keys, nil, true)
end
function table.encode(this)
assert(is_table(this))
return this:string()
end
function table.decode(st)
assert(is_string(st))
return st:table()
end
function table.print(this)
print("[table:" .. lua_get_pointer(this) .. "](" .. table.printable(this, "| ") .. ")")
end
function table.is_empty(this)
return next(this) == nil
end
function table.is_equal(this, that)
assert(is_table(this))
if not is_table(that) then
return false
end
for k, v in pairs(this) do
if is_table(v) then
if not table.is_equal(v, that[k]) then
return false
end
else
if v ~= that[k] then
return false
end
end
end
for k, v in pairs(that) do
if is_table(v) then
if not table.is_equal(v, this[k]) then
return false
end
else
if v ~= this[k] then
return false
end
end
end
return true
end
function table.is_same(this, that)
assert(is_table(this))
if not is_table(that) then
return false
end
for k, v in pairs(this) do
if is_table(v) then
if not table.is_same(v, that[k]) then
return false
end
else
if type(v) ~= type(that[k]) then
return false
end
end
end
for k, v in pairs(that) do
if is_table(v) then
if not table.is_same(v, this[k]) then
return false
end
else
if type(v) ~= type(this[k]) then
return false
end
end
end
return true
end
function table.find_value(this, value)
local rKey, rVal = nil, nil
table.foreach(this, function(k, v)
if v == value then
rKey, rVal = k, v
return true
end
end)
return rKey, rVal
end
function table.find_key(this, key)
local rKey, rVal = nil, nil
table.foreach(this, function(k, v)
if k == key then
rKey, rVal = k, v
return true
end
end)
return rKey, rVal
end
function table.find_if(this, func)
local rKey, rVal = nil, nil
table.foreach(this, function(k, v)
if func(k, v) then
rKey, rVal = k, v
return true
end
end)
return rKey, rVal
end
function table.reorder(this, isAsc, ...)
local conditions = {...}
if #conditions == 0 then
return this
end
local condition = nil
table.sort(this, function(t1, t2)
for i = 1, #conditions do
condition = conditions[i]
if t1[condition] ~= t2[condition] then
if isAsc then
return t1[condition] < t2[condition]
else
return t1[condition] > t2[condition]
end
end
end
return nil
end)
return this
end
function table.foreach(this, func)
if table.is_array(this) then
for i,v in ipairs(this) do
if func(i,v) then
return
end
end
else
for k,v in pairs(this) do
if func(k,v) then
break
end
end
end
end
function table.map(this, func)
local ret = {}
table.foreach(this, function(k, v)
local r = func(k,v)
if r then
if is_array(this) then
table.insert(ret, r)
else
ret[k] = r
end
end
end)
return ret
end
function table.reduce(this, func, accumulator)
if not is_function(func) and is_function(accumulator) then
local temp = func
func = accumulator
accumulator = temp
end
table.foreach(this, function(k, v)
if not accumulator then
if is_number(v) then
accumulator = 0
elseif is_string(v) then
accumulator = ""
elseif is_table(v) then
accumulator = {}
end
end
accumulator = func(accumulator, v)
end)
return accumulator
end
function table.reverse(this)
local ret = {}
for i = #this, 1, -1 do
table.insert(ret, this[i])
end
return ret
end
function table.append(this, other)
for i,v in ipairs(other) do
table.insert(this, v)
end
end
function table.remove_value(this, value, count)
local num = 0
local key = table.find_value(this, value)
while key and (not count or num < count) do
table.remove_key(this, key)
num = num + 1
key = table.find_value(this, value)
end
end
function table.remove_key(this, key)
if is_array(this) then
assert(is_number(key))
table.remove(this, key)
else
assert(is_number(key) or is_string(key))
this[key] = nil
end
end
function table.remove_if(this, func)
if is_array(this) then
for i = #this, 1, -1 do
if func(i, this[i]) then
table.remove(this, i)
end
end
else
for k,v in pairs(this) do
if func(k, this[k]) then
this[k] = nil
end
end
end
end
-- file:[./files/json.lua]
json = json or {}
function json.encodable(o)
local t = type(o)
return (t == 'string' or t == 'boolean' or t == 'number' or t == 'nil' or t == 'table') or (t == 'function' and o == null)
end
function json._encodeString(s)
s = string.gsub(s, '\\', '\\\\')
s = string.gsub(s, '"', '\\"')
s = string.gsub(s, "'", "\\'")
s = string.gsub(s, '\n', '\\n')
s = string.gsub(s, '\t', '\\t')
return s
end
function json._encode(v)
if is_nil(v) or v == null then return "null" end
if is_boolean(v) or is_number(v) then return tostring(v) end
if is_string(v) then return '"' .. json._encodeString(v) .. '"' end
local rval = {}
if table.is_array(v) then
for i = 1, #v do
table.insert(rval, json._encode(v[i]))
end
return '[' .. table.concat(rval, ',') .. ']'
end
if is_table(v) then
for i, j in pairs(v) do
if json.encodable(i) and json.encodable(j) then
table.insert(rval, '"' .. json._encodeString(i) .. '":' .. json._encode(j))
end
end
return '{' .. table.concat(rval, ',') .. '}'
end
assert(false, 'type not supported:' .. type(v))
end
function json._decode_scanWhitespace(s, startPos)
local stringLen = string.len(s)
while (string.find(" \n\r\t", string.sub(s, startPos, startPos), 1, true) and startPos <= stringLen) do
startPos = startPos + 1
end
return startPos
end
function json._decode_scanObject(s, startPos)
local object = {}
local stringLen = string.len(s)
local key, value
startPos = startPos + 1
repeat
startPos = json._decode_scanWhitespace(s, startPos)
assert(startPos <= stringLen, 'JSON string ended unexpectedly while scanning object.')
local curChar = string.sub(s, startPos, startPos)
if (curChar == '}') then return object, startPos + 1 end
if (curChar == ',') then startPos = json._decode_scanWhitespace(s, startPos + 1) end
assert(startPos <= stringLen, 'JSON string ended unexpectedly scanning object.')
key, startPos = json._decode(s, startPos)
assert(startPos <= stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
startPos = json._decode_scanWhitespace(s, startPos)
assert(startPos <= stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
assert(string.sub(s, startPos, startPos) == ':', 'JSON string ended unexpectedly searching for assignment at ' .. startPos)
startPos = json._decode_scanWhitespace(s, startPos + 1)
assert(startPos <= stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
value, startPos = json._decode(s, startPos)
object[key] = value
until false
end
function json._decode_scanArray(s, startPos)
local array = {}
local stringLen = string.len(s)
startPos = startPos + 1
repeat
startPos = json._decode_scanWhitespace(s, startPos)
assert(startPos <= stringLen, 'JSON String ended unexpectedly scanning array.')
local curChar = string.sub(s, startPos, startPos)
if (curChar == ']') then return array, startPos + 1 end
if (curChar == ',') then startPos = json._decode_scanWhitespace(s, startPos + 1) end
assert(startPos <= stringLen, 'JSON String ended unexpectedly scanning array.')
object, startPos = json._decode(s, startPos)
table.insert(array, object)
until false
end
function json._decode_scanNumber(s, startPos)
local endPos = startPos + 1
local stringLen = string.len(s)
while (string.find("+-0123456789.e", string.sub(s, endPos, endPos), 1, true) and endPos <= stringLen) do
endPos = endPos + 1
end
local stringValue = string.sub(s, startPos, endPos - 1)
local numberValue = tonumber(stringValue)
assert(numberValue ~= nil, 'invalid number [ ' .. stringValue .. '] at:' .. startPos .. ' : ' .. endPos)
return stringEval(), endPos
end
function json._decode_scanString(s, startPos)
local startChar = string.sub(s, startPos, startPos)
local escaped = false
local endPos = startPos + 1
local bEnded = false
local stringLen = string.len(s)
repeat
local curChar = string.sub(s, endPos, endPos)
if not escaped then
if curChar == [[\]] then
escaped = true
else
bEnded = curChar == startChar
end
else
escaped = false
end
endPos = endPos + 1
assert(endPos <= stringLen + 1, 'JSON string ended unexpectedly scanning string.')
until bEnded
local stringValue = 'return ' .. string.sub(s, startPos, endPos - 1)
local stringEval = loadstring(stringValue)
assert(stringEval, 'invalid string [ ' .. stringValue .. '] at ' .. startPos .. ' : ' .. endPos)
return stringEval(), endPos
end
function json._decode_scanComment(s, startPos)
local endPos = string.find(s, '*/', startPos + 2)
assert(endPos ~= nil, "invalid comment tag!")
return json._decode(s, endPos + 2)
end
function json._decode_scanConstants(s, startPos)
local constValues = {true, false, nil}
local constNames = {"true", "false", "null"}
for _, k in pairs(constNames) do
if string.sub(s, startPos, startPos + string.len(k) - 1) == k then
return constValues[k], startPos + string.len(k)
end
end
assert(false, 'invalid json value at:' .. startPos)
end
function json._decode(s, startPos)
startPos = startPos and startPos or 1
startPos = json._decode_scanWhitespace(s, startPos)
assert(startPos <= string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
local curChar = string.sub(s, startPos, startPos)
if string.sub(s, startPos, startPos + 1) == '/*' then return json._decode_scanComment(s, startPos) end
if curChar == '{' then return json._decode_scanObject(s, startPos) end
if curChar == '[' then return json._decode_scanArray(s, startPos) end
if curChar == [["]] or curChar == [[']] then return json._decode_scanString(s, startPos) end
if string.find("+-0123456789.e", curChar, 1, true) then return json._decode_scanNumber(s, startPos) end
return json._decode_scanConstants(s, startPos)
end
function json.encode(v)
local isOk, r = xpcall(function() return json._encode(v) end, function(err) return err end)
if isOk then return r, nil end
return nil, r
end
function json.decode(s)
local isOk, r = xpcall(function() return json._decode(s) end, function(err) return err end)
if isOk then return r, nil end
return nil, r
end
-- file:[./files/yaml.lua]
yaml = yaml or {}
function yaml.convert(val)