Repository navigation
Expand file tree
/
Copy pathstringx.lua
More file actions
974 lines (862 loc) · 29.3 KB
/
Copy pathstringx.lua
File metadata and controls
974 lines (862 loc) · 29.3 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
function string.qsplit(s, pat) -- respects single and double quotes
if isbuffer(s) then
s = tostring(s)
end
local quotes = {}
local retVal = {}
local w = ''
local i = 1
while i <= #s do
local si = string.find(s, pat, i)
if si == i and #quotes == 0 then
i = i + #pat
if #w > 0 then
retVal[#retVal + 1] = w
w = ''
end
else
local c = string.sub(s, i, i)
if c == '"' or c == "'" then
if #quotes == 0 or quotes[#quotes] ~= c then
w = w .. c
if #quotes > 1 then
quotes = {}
else
quotes[#quotes + 1] = c
end
else
table.remove(quotes)
w = w .. c
end
else
w = w .. c
end
i = i + 1
end
end
if #w > 0 then
retVal[#retVal + 1] = w
end
return retVal
end
function string.gsplit(text, pattern, plain)
if isbuffer(text) then
text = tostring(text)
end
local splitStart, length = 1, #text
return function()
if splitStart then
local sepStart, sepEnd = string.find(text, pattern, splitStart, plain)
local ret
if not sepStart then
ret = string.sub(text, splitStart)
splitStart = nil
elseif sepEnd < sepStart then
-- empty separator
ret = string.sub(text, splitStart, sepStart)
if sepStart < length then
splitStart = sepStart + 1
else
splitStart = nil
end
else
ret = sepStart > splitStart and string.sub(text, splitStart, sepStart - 1) or ''
splitStart = sepEnd + 1
end
return ret
end
end
end
function string.split(text, pattern, plain)
if isbuffer(text) then
text = tostring(text)
end
local ret = {}
for match in string.gsplit(text, pattern, plain) do table.insert(ret, match) end
return ret
end
function string.startswith(s, prefix)
if isbuffer(s) then
s = tostring(s)
end
return prefix == '' or s:sub(1, #prefix) == prefix
end
function string.endswith(s, suffix)
if isbuffer(s) then
s = tostring(s)
end
return suffix == '' or s:sub(-#suffix) == suffix
end
function string.trim(s)
if isbuffer(s) then
s = tostring(s)
end
return s:gsub('^%s*(.-)%s*$', '%1')
end
function string.ltrim(s)
if isbuffer(s) then
s = tostring(s)
end
return s:gsub('^%s*', '')
end
function string.rtrim(s)
if isbuffer(s) then
s = tostring(s)
end
local n = #s
while n > 0 and s:find('^%s', n) do n = n - 1 end
return s:sub(1, n)
end
function string.chars(s)
if isbuffer(s) then
s = tostring(s)
end
local ret = {}
for i = 1, #s do table.insert(ret, s:sub(i, i)) end
return ret
end
function string.bytes(s)
if isbuffer(s) then
s = tostring(s)
end
local ret = {}
for i = 1, #s do table.insert(ret, string.byte(s:sub(i, i))) end
return ret
end
function string.escpat(x)
if isbuffer(x) then
x = tostring(x)
end
return (x
:gsub('%%', '%%%%')
:gsub('^%^', '%%^')
:gsub('%$$', '%%$')
:gsub('%(', '%%(')
:gsub('%)', '%%)')
:gsub('%.', '%%.')
:gsub('%[', '%%[')
:gsub('%]', '%%]')
:gsub('%*', '%%*')
:gsub('%+', '%%+')
:gsub('%-', '%%-')
:gsub('%?', '%%?')
)
end
function string.isalnum(s)
if isbuffer(s) then
s = tostring(s)
end
return s:match('^%w*$')
end
function string.isalpha(s)
if isbuffer(s) then
s = tostring(s)
end
return s:match('^%a*$')
end
function string.isidentifier(s)
if isbuffer(s) then
s = tostring(s)
end
return s:match('^[_%a][_%w]*$')
end
function string.islower(s)
if isbuffer(s) then
s = tostring(s)
end
return s:lower() == s
end
function string.isnumeric(s)
if isbuffer(s) then
s = tostring(s)
end
return s:match('^%d*$')
end
function string.isprintable(s)
if isbuffer(s) then
s = tostring(s)
end
if #s == 0 then return true end
-- Check if the string is valid UTF-8
local ok, val = pcall(utf8.len, s)
if not ok then return false end -- e.g.: "invalid UTF-8 sequence" error (?)
if val == nil then return false end -- utf8.len returns nil for invalid seq
-- Check for non-printable ASCII control characters (0-31, 127)
if s:match("[%c]") then
return false -- Contains control characters
end
return true -- String is valid UTF-8 and contains only printable characters
end
function string.isspace(s)
if isbuffer(s) then
s = tostring(s)
end
return s:match('^%s*$')
end
function string.isupper(s)
if isbuffer(s) then
s = tostring(s)
end
return s:upper() == s
end
function string.capitalize(s)
if isbuffer(s) then
s = tostring(s)
end
return s:sub(1, 1):upper() .. s:sub(2)
end
function string.escapequotes(s, context)
context = context or '\''
if context ~= "'" and context ~= '"' then
error("Invalid context. Use '\'' for single quotes or '\"' for double quotes.")
end
-- Escape backslashes first to avoid double escaping
s = s:gsub('\\', '\\\\')
-- Escape the appropriate quote type
local pattern = context == "'" and "'" or '"'
s = s:gsub(pattern, '\\' .. pattern)
return s
end
function string.escapehtml(s, opts)
if isbuffer(s) then
s = tostring(s)
end
opts = opts or {}
opts.entities = opts.entities ~= false
if opts.entities then
local htmlEntities = {
["&"] = "&",
["<"] = "<",
[">"] = ">",
['"'] = """,
["'"] = "'",
}
s = s:gsub("[&<>\"']", function(match) return htmlEntities[match] end)
end
return s
end
function string.stripmarkdown(s, opts)
if isbuffer(s) then
s = tostring(s)
end
opts = opts or {}
-- Remove headers at start of a line, with or without trailing newline
s = s:gsub("^##+ ([^\n]*)\n", "%1\n")
s = s:gsub("\n##+ ([^\n]*)\n", "\n%1\n")
s = s:gsub("^##+ ([^\n]*)$", "%1")
s = s:gsub("\n##+ ([^\n]*)$", "\n%1")
-- Remove bold
s = s:gsub("%*%*([^*]+)%*%*", "%1")
s = s:gsub("__([^_]+)__", "%1")
-- Remove italic
s = s:gsub("%*([^*]*)%*", "%1")
s = s:gsub("_([^_]*)_", "%1")
-- Remove inline code (`code`)
s = s:gsub("`([^`]*)`", "%1")
if not opts.keeplinks then
-- Remove links [text](url)
s = s:gsub("%[([^%]]+)%]%([^%)]+%)", "%1")
end
return s
end
function string.renderxml(node, opts, _indent)
if type(opts) == 'string' then
-- backward compatibility fix: second arg was attrs_order
opts = {attrsOrder = opts}
end
opts = opts or {}
_indent = _indent or ""
local nl = "\n"
local shortFormat = nil
if node.tag == nil then
assert(type(node[1]) == 'string', 'missing tag')
shortFormat = true
else
shortFormat = false
end
local xml = {}
local tag = shortFormat and node[1] or node.tag
local function normalizeAttribute(a)
if opts.attrUnderscoreToDash ~= false then
a = a:gsub('_', '-')
end
return a
end
local attrs = shortFormat and node or node.attrs
local attrsEntries = {}
if attrs then
local attrsNorm = {}
for k, v in pairs(attrs) do
if type(k) == 'string' then
k = normalizeAttribute(k)
attrsNorm[k] = v
end
end
local done = {}
for _, k in ipairs(opts.attrsOrder or {}) do
k = normalizeAttribute(k)
local v = attrsNorm[k]
if v then
table.insert(attrsEntries, {k, v})
done[k] = true
end
end
for k, v in pairs(attrsNorm) do
if not done[k] then
table.insert(attrsEntries, {k, v})
end
end
end
local attrsStr = ""
for _, attrEntry in ipairs(attrsEntries) do
local k, v = table.unpack(attrEntry)
v = string.escapehtml(tostring(v))
attrsStr = attrsStr .. string.format(' %s="%s"', k, string.escapehtml(tostring(v)))
end
local children = shortFormat and node or node.children or {}
local numChildren = shortFormat and (#children - 1) or #children
local i = shortFormat and 2 or 1 -- starting index
if numChildren == 0 then
table.insert(xml, string.format("%s<%s%s />", _indent, tag, attrsStr))
return table.concat(xml, "")
end
-- exactly one text child -> inline start/end tag
if numChildren == 1 and type(children[i]) == "string" then
table.insert(xml, string.format("%s<%s%s>%s</%s>", _indent, tag, attrsStr, string.escapehtml(children[i]), tag))
return table.concat(xml)
end
table.insert(xml, string.format("%s<%s%s>", _indent, tag, attrsStr))
while children[i] do
if type(children[i]) == "string" then
table.insert(xml, string.escapehtml(children[i]))
else
table.insert(xml, nl .. string.renderxml(children[i], opts, _indent .. " "))
end
i = i + 1
end
table.insert(xml, string.format("%s</%s>", nl .. _indent, tag))
return table.concat(xml)
end
function string.stripprefix(s, prefix)
if string.startswith(s, prefix) then
return s:sub(#prefix + 1)
else
return s
end
end
function string.stripsuffix(s, suffix)
if string.endswith(s, suffix) then
return s:sub(1, #s - #suffix)
else
return s
end
end
function string.elide(s, maxLen, opts)
maxLen = maxLen or 80
opts = opts or {}
local ret = s
if opts.truncateAtNewLine then
local n = ret:find("\n")
if n then
ret = ret:sub(1, n - 1)
end
end
if #ret > maxLen then
ret = ret:sub(1, maxLen)
end
if #ret < #s then
ret = ret .. "..."
end
return ret
end
function string.utf8sub(s, i, j)
local start_byte = utf8.offset(s, i) or (#s + 1)
local end_byte = j and (utf8.offset(s, j + 1) or (#s + 1)) - 1 or #s
return s:sub(start_byte, end_byte)
end
function string._len(s)
-- undocumented
if s == '' then return 0 end
local l, o = utf8.len(s)
if l then
return l
else
return utf8.len(s:sub(1, o - 1)) + 1 + string._len(s:sub(o + 1))
end
end
function string.pad(s, width, clip)
if clip then
s = string.utf8sub(s, 1, math.abs(width))
end
local p = string.rep(' ', math.abs(width) - string._len(s))
if width < 0 then
return p .. s
elseif width > 0 then
return s .. p
else
return ''
end
end
function string.blocksize(s)
local lines = string.split(s, '\n')
local width, height = 0, #lines
for i = 1, #lines do
width = math.max(width, string._len(lines[i]))
end
return width, height
end
function string.blockpad(s, width, height, clip)
if width == nil and height == nil then
width, height = string.blocksize(s)
end
local lines = string.split(s, '\n')
if clip then
lines = table.slice(lines, 1, math.abs(height))
end
for i = 1, math.max(math.abs(height) - #lines, 0) do
if height < 0 then
table.insert(lines, 1, '')
elseif height > 0 then
table.insert(lines, '')
end
end
for i = 1, #lines do
lines[i] = string.pad(lines[i], width, clip)
end
return table.join(lines, '\n')
end
function string.blockhstack(blocks, spacing)
spacing = spacing or 1
local sz = map(function(x) return {string.blocksize(x)} end, blocks)
local h = 0
for i = 1, #sz do
h = math.max(h, sz[i][2])
end
local paddedblocks, lines = {}, {}
for i = 1, #blocks do
paddedblocks[i] = string.blockpad(blocks[i], sz[i][1], -h, true)
lines[i] = string.split(paddedblocks[i], '\n')
end
local r, spc = {}, string.rep(' ', spacing)
for j = 1, h do
r[j] = ''
for i = 1, #paddedblocks do
r[j] = r[j] .. (i > 1 and spc or '') .. lines[i][j]
end
end
return table.join(r, '\n')
end
function string.numbertostring(x, opts)
if math.type(x) ~= 'float' then
return tostring(x)
end
opts = opts and table.clone(opts) or {}
opts.numFloatDigits = math.max(0, opts.numFloatDigits or 6)
opts.stripTrailingZeros = opts.stripTrailingZeros ~= false
local s = string.format('%.' .. opts.numFloatDigits .. 'f', x)
if opts.stripTrailingZeros then
local i, d = table.unpack(string.split(s, '%.'))
d = string.gsub(d or '', '0*$', '')
s = i .. '.' .. d
end
return s
end
function string.functostring(x, opts)
assert(type(x) == 'function', 'bad type')
local s = tostring(x)
s = s:gsub(': 0x*', '_')
return s
end
function string.getshortstring(x, opts)
opts = opts or {}
opts.omitQuotes = opts.omitQuotes == true
opts.escapeNewline = opts.escapeNewline ~= false
opts.allowBinary = opts.allowBinary == true
opts.allowBinary = true
opts.longStringThreshold = opts.longStringThreshold or -1
if type(x) == 'string' then
if not string.isprintable(x) and not opts.allowBinary then
return string.format('[binary string (%s bytes)]', #x)
end
if opts.longStringThreshold >= 0 and #x > opts.longStringThreshold then
return string.format('[long string (%s bytes)]', #x)
end
if not opts.omitQuotes then
x = "'" .. string.escapequotes(x, '\'') .. "'"
end
if opts.escapeNewline then
x = x:gsub('\n', '\\n')
end
return x
end
return "[not a string]"
end
function string.isidentifier(x)
return type(x) == 'string' and x:match('^[a-zA-Z_][a-zA-Z0-9_]*$') ~= nil
end
function string.anytostring(x, opts)
opts = opts or {}
local t = type(x)
if t == 'nil' then
return tostring(nil)
elseif t == 'table' then
local mt = getmetatable(x) or {}
if opts.display and mt.__todisplay then return mt.__todisplay(x, opts) end
if isbuffer(x) then return mt.__todisplay(x, opts) end
if mt.__tostring then return mt.__tostring(x, opts) end
-- displays inside table won't render good:
require 'tablex'
opts = table.update({}, opts, {display = false})
return table.tostring(x, opts)
elseif t == 'string' then
return string.getshortstring(x, opts)
elseif t == 'number' then
return string.numbertostring(x, opts)
else
return tostring(x)
end
end
--- Grep-like search over a multiline string.
-- @param text string The haystack.
-- @param expr string Lua pattern (default) or plain substring (opts.plain = true).
-- @param opts table Optional:
-- plain (bool) treat expr as a literal string
-- ignorecase (bool) case-insensitive match
-- context (number) lines of context before & after
-- before (number) override lines before
-- after (number) override lines after
-- @return table list of matches, each = {
-- line number 1-based line number
-- column number 1-based column of match start within the line
-- start number 1-based absolute start offset in text
-- stop, end number 1-based absolute end offset in text
-- match,text string the matched substring
-- lineStart number absolute offset of line start
-- lineEnd number absolute offset of line end (before \n)
-- lineText string the full line
-- captures table capture groups from the pattern
-- context table|nil surrounding lines (includes the matched line)
-- }
function string.grep(text, expr, opts)
opts = opts or {}
if type(text) ~= "string" then
error("string.grep: text must be a string", 2)
end
if type(expr) ~= "string" then
error("string.grep: expr must be a string", 2)
end
local results = {}
if expr == "" then return results end
local plain = opts.plain or false
local icase = opts.ignorecase or false
local before = opts.before or opts.context or 0
local after = opts.after or opts.context or 0
-- ---- 1. Split text into lines, tracking absolute offsets -------------
local lines = {}
do
local n = #text
if n == 0 then
lines[1] = { num = 1, start = 1, stop = 0, text = "" }
else
local i, num = 1, 0
while i <= n do
num = num + 1
local nl = text:find("\n", i, true)
local stop = nl and (nl - 1) or n
local body = text:sub(i, stop)
if body:sub(-1) == "\r" then -- strip CR in CRLF
body = body:sub(1, -2)
stop = stop - 1
end
lines[#lines + 1] = {
num = num, start = i, stop = stop, text = body,
}
if not nl then break end
i = nl + 1
end
end
end
-- ---- 2. Prepare the search pattern -----------------------------------
local pat = expr
if plain then
pat = (expr:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1"))
end
-- ---- 3. Scan every line ---------------------------------------------
for li = 1, #lines do
local line = lines[li]
local content = line.text
local searchContent = icase and content:lower() or content
local searchPat = icase and pat:lower() or pat
local init = 1
local clen = #searchContent
while init <= clen + 1 do
local f = { searchContent:find(searchPat, init) }
local ms, me = f[1], f[2]
if not ms then break end
local captures = {}
for k = 3, #f do captures[#captures + 1] = f[k] end
local matchText = content:sub(ms, me)
local absStart = line.start + ms - 1
local absStop = line.start + me - 1
local context = nil
if before > 0 or after > 0 then
context = {}
local lo = math.max(1, li - before)
local hi = math.min(#lines, li + after)
for k = lo, hi do
context[#context + 1] = lines[k].text
end
end
results[#results + 1] = {
line = line.num,
column = ms,
start = absStart,
stop = absStop,
["end"] = absStop,
match = matchText,
text = matchText,
lineStart = line.start,
lineEnd = line.stop,
lineText = content,
captures = captures,
context = context,
}
-- advance; guard against zero-width matches
init = (me < ms) and (ms + 1) or (me + 1)
end
end
return results
end
function string.unittest()
-- fix for "attempt to call a nil value (global 'isbuffer')"
isbuffer = isbuffer or function(x) return false end
require 'tablex'
-- helpers:
local function assert_eq(actual, expected, msg)
if not table.eq(actual, expected) then
error(string.format('%s\nactual: %s\nexpected: %s', msg or 'assert_eq failed', table.tostring(actual), table.tostring(expected)), 2)
end
end
local function assert_eq_s(actual, expected, msg)
if actual ~= expected then
error(string.format('%s\nactual: %s\nexpected: %s', msg or 'assert_eq_s failed', tostring(actual), tostring(expected)), 2)
end
end
do
local it = string.gsplit('a,b,c', ',')
assert_eq_s(it(), 'a')
assert_eq_s(it(), 'b')
assert_eq_s(it(), 'c')
assert(it() == nil)
end
assert_eq(string.split('a%b%c', '%', true), {'a', 'b', 'c'})
assert_eq(string.split('a', '%', true), {'a'})
assert_eq(string.split('a%', '%', true), {'a', ''})
assert_eq(string.split('a%--b', '%-', true), {'a', '-b'})
assert_eq(string.split('"a b" "c d"', '"', true), {'', 'a b', ' ', 'c d', ''})
assert_eq(string.split('"a b" "c d"', ' ', true), {'"a', 'b"', '"c', 'd"'})
assert_eq(string.split('', ','), {''})
assert_eq(string.split('a,b,', ','), {'a', 'b', ''})
assert_eq(string.split(',a,b', ','), {'', 'a', 'b'})
assert_eq(string.split('a,,b', ','), {'a', '', 'b'})
assert_eq(string.split('abc', ''), {'a', 'b', 'c'})
assert_eq(string.split('abc', 'b'), {'a', 'c'})
assert_eq(string.qsplit('"a b" "c d"', ' '), {'"a b"', '"c d"'})
assert_eq(string.qsplit("'a b' 'c d'", ' '), {"'a b'", "'c d'"})
assert_eq(string.qsplit('"a\' b" "c d"', ' '), {'"a\' b"', '"c d"'})
assert_eq(string.qsplit("'a\" b' 'c d'", ' '), {"'a\" b'", "'c d'"})
assert_eq(string.qsplit('', ' '), {})
assert_eq(string.qsplit(' ', ' '), {})
assert_eq(string.qsplit('a b', ' '), {'a', 'b'})
assert_eq(string.qsplit('a "b c" d', ' '), {'a', '"b c"', 'd'})
assert_eq(string.qsplit('"a \'b\' c" d', ' '), {'"a \'b\' c"', 'd'})
assert_eq(string.qsplit('a "b c', ' '), {'a', '"b c'})
assert(string.startswith('abcde', 'abc'))
assert(string.startswith('abc', 'abc'))
assert(not string.startswith('bcde', 'abc'))
assert(string.startswith('abc', ''))
assert(not string.startswith('', 'a'))
assert(string.endswith('abcde', 'cde'))
assert(string.endswith('abc', 'abc'))
assert(not string.endswith('bcde', 'bcd'))
assert(string.endswith('abc', ''))
assert(not string.endswith('', 'a'))
assert_eq_s(string.trim(' abc '), 'abc')
assert_eq_s(string.ltrim(' abc '), 'abc ')
assert_eq_s(string.rtrim(' abc '), ' abc')
assert_eq_s(string.trim(''), '')
assert_eq_s(string.trim(' '), '')
assert_eq_s(string.ltrim(' '), '')
assert_eq_s(string.rtrim(' '), '')
assert_eq(string.chars('abc'), {'a', 'b', 'c'})
assert_eq(string.chars(''), {})
assert_eq(string.chars('\xC3\xA9'), {'\xC3', '\xA9'})
assert_eq(string.bytes('abc'), {0x61, 0x62, 0x63})
assert_eq(string.bytes(''), {})
assert_eq(string.bytes('\xC3\xA9'), {0xC3, 0xA9}) -- UTF-8 é
assert_eq_s(string.escpat('[[--x'), '%[%[%-%-x')
assert_eq_s(string.escpat('^()%.[]*+-?$'), '%^%(%)%%%.%[%]%*%+%-%?%$')
assert(string.isalnum 'abcABC123')
assert(not string.isalnum 'abc-ABC123')
assert(string.isalnum '')
assert(string.isalpha 'abcABC')
assert(not string.isalpha 'abcABC123')
assert(string.isalpha '')
assert(string.islower 'abc123')
assert(not string.islower 'abcABC123')
assert(string.islower '')
assert(string.isupper 'ABC123')
assert(not string.isupper 'abcABC123')
assert(string.isupper '')
assert(string.isnumeric '123')
assert(not string.isnumeric '123abcABC')
assert(string.isnumeric '')
assert(string.isspace ' \n')
assert(not string.isspace 'abc ABC')
assert(string.isspace '')
assert(string.isidentifier 'abcABC3')
assert(string.isidentifier '_3')
assert(not string.isidentifier '3abcABC123')
assert(not string.isidentifier 'abc ABC123')
assert(not string.isidentifier '')
assert(string.isprintable 'abc,:ABC!123')
assert(not string.isprintable '\xff\x00123abcABC')
assert(not string.isprintable '\001')
assert(string.isprintable '\xC3\xA9') -- é
assert(not string.isprintable '\255') -- invalid UTF-8
assert(string.isprintable '')
assert_eq_s(string.capitalize 'robot', 'Robot')
assert_eq_s(string.capitalize 'robot', 'Robot')
assert_eq_s(string.capitalize 'robotArray', 'RobotArray')
assert_eq_s(string.capitalize '', '')
assert_eq_s(string.escapequotes('abc'), 'abc')
assert_eq_s(string.escapequotes('a = \'b\'', '\''), 'a = \\\'b\\\'')
assert_eq_s(string.escapequotes('a = \'b\'', '\"'), 'a = \'b\'')
assert_eq_s(string.escapequotes('a = "b"', '\''), 'a = "b"')
assert_eq_s(string.escapequotes('a = "b"', '"'), 'a = \\"b\\"')
assert_eq_s(string.escapequotes('a\\b', "'"), 'a\\\\b')
assert_eq_s(string.escapequotes('a\\b', '"'), 'a\\\\b')
do
local ok = pcall(string.escapequotes, 'x', '`')
assert(not ok)
end
assert_eq_s(string.escapehtml('&<>"\''), '&<>"'')
assert_eq_s(string.escapehtml('&<>', {entities = false}), '&<>')
assert_eq_s(string.stripmarkdown('## Header\n**bold** _italic_ `code` [link](url)'), 'Header\nbold italic code link')
assert_eq_s(string.stripmarkdown('[link](url)', {keeplinks = true}), '[link](url)')
assert_eq_s(string.stripprefix('foobar', 'foo'), 'bar')
assert_eq_s(string.stripprefix('foobar', 'bar'), 'foobar')
assert_eq_s(string.stripsuffix('foobar', 'bar'), 'foo')
assert_eq_s(string.stripsuffix('foobar', 'foo'), 'foobar')
assert_eq_s(string.elide('abcdef', 3), 'abc...')
assert_eq_s(string.elide('abc', 5), 'abc')
assert_eq_s(string.elide('a\nb', 10, {truncateAtNewLine = true}), 'a...')
local e = '\xC3\xA9' -- é
do
local s = 'a' .. e .. 'bc'
assert_eq_s(string.utf8sub(s, 1, 1), 'a')
assert_eq_s(string.utf8sub(s, 2, 2), e)
assert_eq_s(string.utf8sub(s, 3, 3), 'b')
assert_eq_s(string.utf8sub(s, 4, 4), 'c')
assert_eq_s(string.utf8sub(s, 1, 4), s)
assert_eq_s(string.utf8sub(s, 2), e .. 'bc') -- j omitted → to end
assert_eq_s(string.utf8sub(s, 2, 100), e .. 'bc') -- j past end
assert_eq_s(string.utf8sub(s, 5, 5), '') -- i past end
assert_eq_s(string._len('a' .. e .. 'bc'), 4)
assert_eq_s(string._len(''), 0)
end
assert_eq_s(string.pad(e, 2), e .. ' ')
assert_eq_s(string.pad(e, -2), ' ' .. e)
assert_eq_s(string.pad('aaa', 4), 'aaa ')
assert_eq_s(string.pad('aaa', -4), ' aaa')
assert_eq_s(string.pad('aaa', 2), 'aaa')
assert_eq_s(string.pad('aaa', 2, true), 'aa')
assert_eq({string.blocksize(e .. '\nabc')}, {3, 2})
assert_eq({string.blocksize('aaa\naaa')}, {3, 2})
assert_eq_s(string.blockpad('a\nbb', 3, -3), ' \na \nbb ')
assert_eq_s(string.blockpad('a\naa', 4, 3), 'a \naa \n ')
assert_eq_s(string.blockhstack({'aa', 'bbb'}, 2), 'aa bbb')
assert_eq_s(string.blockhstack({'aa\naa', 'bbb\nbbb\nbbb'}), ' bbb\naa bbb\naa bbb')
assert_eq_s(string.numbertostring(42), '42')
assert_eq_s(string.numbertostring(1.23456789, {numFloatDigits = 2}), '1.23')
assert_eq_s(string.numbertostring(1.2, {stripTrailingZeros = false, numFloatDigits = 4}), '1.2000')
assert_eq_s(string.numbertostring(1.2, {numFloatDigits = 4}), '1.2')
assert_eq_s(string.getshortstring('abc'), "'abc'")
assert_eq_s(string.getshortstring("a'b"), "'a\\'b'")
assert_eq_s(string.getshortstring('a\nb'), "'a\\nb'")
assert_eq_s(string.getshortstring('abcdef', {longStringThreshold = 3}), '[long string (6 bytes)]')
assert_eq_s(string.getshortstring('abc', {omitQuotes = true}), 'abc')
assert_eq_s(string.anytostring(nil), 'nil')
assert_eq_s(string.anytostring(42), '42')
assert_eq_s(string.anytostring('abc'), "'abc'")
do
local opts = {
attrsOrder = {
'title', 'closeable', 'resizable', 'on-close',
'id', 'style', 'read-only'
}
}
local xml = string.renderxml({
tag = 'ui',
attrs = {
title = 'thetitle',
closeable = true,
resizable = 4,
['on-close'] = ':onClose'
},
children = {
{
tag = 'text-browser',
attrs = {
id = 1,
style = 'color: red;',
['read-only'] = true
}
}
}
}, opts)
assert_eq_s(
xml,
'<ui title="thetitle" closeable="true" resizable="4" on-close=":onClose">\n' ..
' <text-browser id="1" style="color: red;" read-only="true" />\n' ..
'</ui>'
)
end
assert_eq_s(string.renderxml{tag = 'p', children = {'hello'}}, '<p>hello</p>')
assert_eq_s(string.renderxml{'br'}, '<br />')
assert_eq_s(
string.renderxml{
tag = 'ui',
attrs = {
title = 'thetitle',
closeable = true,
resizable = 4,
['on-close'] = ':onClose',
},
children = {
{
tag = 'text-browser',
attrs = {
id = 1,
style = 'color: red;',
['read-only'] = true,
}
},
},
},
string.renderxml{
'ui',
title = 'thetitle',
closeable = true,
resizable = 4,
on_close = ':onClose',
{
'text-browser',
id = 1,
style = 'color: red;',
read_only = true,
},
}
)
print(debug.getinfo(1, 'S').source, 'tests passed')
end