-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf8.lua
1216 lines (996 loc) · 28.8 KB
/
utf8.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
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
--[[============================================================
$Id: utf8.lua 179 2009-04-03 18:10:03Z pasta $
Provides UTF-8 aware string functions implemented in pure Lua:
- utf8.char( unicode, ... )
- utf8.charlen( str, bytePos )
- utf8.charpattern // Not a function.
- utf8.codes( str )
- utf8.find( str, pattern, init, plain )
- utf8.gensub( str, sliceLen )
- utf8.gmatch( str, pattern, all )
- utf8.gsub( str, pattern, repl, limit )
- utf8.len( str )
- utf8.match( str, pattern, init )
- utf8.offset( str, i, bytePos )
- utf8.offsetend( str, i, bytePos )
- utf8.pos( str, bytePos )
- utf8.reverse( str )
- utf8.sub( str, i, j, bytePos )
- utf8.codepoint( str, i, j, bytePos )
All functions behave as their non UTF-8 aware counterparts with the exception that
UTF-8 characters are used instead of bytes for all units, unless otherwise specified.
Unchanged functions:
- dump( str )
- format( format, ... )
- lower( str )
- rep( str, times )
- upper( str )
Copyright © 2006-2007, Kyle Smith
All rights reserved.
Contributors:
Alimov Stepan
Marcus Thunström (2018-07-14)
License:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ABNF from RFC 3629:
UTF8-octets = *( UTF8-char )
UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
UTF8-1 = %x00-7F
UTF8-2 = %xC2-DF UTF8-tail
UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
%xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
%xF4 %x80-8F 2( UTF8-tail )
UTF8-tail = %x80-BF
--============================================================]]
local sbyte = string.byte
local schar = string.char
local sdump = string.dump
local sfind = string.find
local sformat = string.format
local slower = string.lower
local srep = string.rep
local ssub = string.sub
local supper = string.upper
--==============================================================
local utf8char
local utf8charbytes
local utf8codepoint
local utf8codes
local utf8find
local utf8gensub
local utf8gmatch
local utf8gsub
local utf8len
local utf8match
local utf8offset
local utf8pos
local utf8reverse
local utf8subAndBounds, utf8sub
-- Returns the number of bytes used by the UTF-8 character at byte i in str.
-- Also doubles as a UTF-8 character validator.
-- bytePos = utf8charbytes( str, i )
function utf8charbytes(s, i)
i = i or 1
if type(s) ~= "string" then
error("bad argument #1 to 'utf8charbytes' (string expected, got "..type(s)..")", 2)
end
if type(i) ~= "number" then
error("bad argument #2 to 'utf8charbytes' (number expected, got "..type(i)..")", 2)
end
local c = sbyte(s, i)
if not c then return nil end
-- Determine bytes needed for character, based on RFC 3629.
-- Validate byte 1.
if c > 0 and c <= 127 then
-- UTF8-1.
return 1
elseif c >= 194 and c <= 223 then
-- UTF8-2.
local c2 = sbyte(s, i+1)
if not c2 then
error("UTF-8 string terminated early after position "..i.." (2-byte char)")
end
-- Validate byte 2.
if c2 < 128 or c2 > 191 then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
end
return 2
elseif c >= 224 and c <= 239 then
-- UTF8-3.
local c2 = sbyte(s, i+1)
local c3 = sbyte(s, i+2)
if not c2 or not c3 then
error("UTF-8 string terminated early after position "..i.." (3-byte char)")
end
-- Validate byte 2.
if c == 224 and (c2 < 160 or c2 > 191) then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
elseif c == 237 and (c2 < 128 or c2 > 159) then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
elseif c2 < 128 or c2 > 191 then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
end
-- Validate byte 3.
if c3 < 128 or c3 > 191 then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..","..c3..")")
end
return 3
elseif c >= 240 and c <= 244 then
-- UTF8-4.
local c2 = sbyte(s, i+1)
local c3 = sbyte(s, i+2)
local c4 = sbyte(s, i+3)
if not c2 or not c3 or not c4 then
error("UTF-8 string terminated early after position "..i.." (4-byte char)")
end
-- Validate byte 2.
if c == 240 and (c2 < 144 or c2 > 191) then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
elseif c == 244 and (c2 < 128 or c2 > 143) then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
elseif (c2 < 128 or c2 > 191) then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..")")
end
-- Validate byte 3.
if c3 < 128 or c3 > 191 then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..","..c3..")")
end
-- Validate byte 4.
if c4 < 128 or c4 > 191 then
error("invalid UTF-8 character at byte "..i.." ("..c..","..c2..","..c3..","..c4..")")
end
return 4
else
error("invalid UTF-8 character at byte "..i.." ("..c..")")
end
end
-- Returns the number of characters in a UTF-8 string.
-- string.len
function utf8len(s)
if type(s) ~= "string" then
error("bad argument #1 to 'utf8len' (string expected, got "..type(s)..")", 2)
end
local bytePos = 1
local charLength = 0
while bytePos <= #s do
charLength = charLength+1
bytePos = bytePos+utf8charbytes(s, bytePos)
end
return charLength
end
-- string.sub
function utf8sub(s, i, j, bytePos)
return (utf8subAndBounds(s, i, j, bytePos))
end
function utf8subAndBounds(s, i, j, bytePos)
j = j or -1
bytePos = bytePos or 1
local charLen = (i >= 0 and j >= 0) or utf8len(s)
local charPosStart = (i >= 0 and i or charLen+i+1)
local charPosEnd = (j >= 0 and j or charLen+j+1)
-- Can't have start before end.
if charPosStart > charPosEnd then return "" end
-- Byte offsets to pass to string.sub().
local byteStart = 1
local byteEnd = #s
local charPos = 0
while bytePos <= #s do
charPos = charPos+1
if charPos == charPosStart then
byteStart = bytePos
end
bytePos = bytePos+utf8charbytes(s, bytePos)
if charPos == charPosEnd then
byteEnd = bytePos-1
break
end
end
if charPosStart > charPos then byteStart = #s+1 end
if charPosEnd < 1 then byteEnd = 0 end
return ssub(s, byteStart, byteEnd), byteStart, byteEnd
end
-- string.reverse
function utf8reverse(s)
if type(s) ~= "string" then
error("bad argument #1 to 'utf8reverse' (string expected, got "..type(s)..")", 2)
end
local bytePos = #s
local sReversed = ""
local charLen, byte
while bytePos > 0 do
byte = sbyte(s, bytePos)
while byte >= 128 and byte <= 191 do
bytePos = bytePos-1
byte = sbyte(s, bytePos)
end
charLen = utf8charbytes(s, bytePos)
sReversed = sReversed..ssub(s, bytePos, bytePos+charLen-1) -- @Speed @Memory: Don't concaticate so much!
bytePos = bytePos-1
end
return sReversed
end
-- http://en.wikipedia.org/wiki/Utf8
-- http://developer.coronalabs.com/code/utf-8-conversion-utility
local bytes = {}
-- str = utf8.char( codepoint1, ... )
function utf8char(...)
local bytePos = 1
local cp
for i = 1, select("#", ...) do
cp = select(i, ...)
if cp <= 0x7F then
bytes[bytePos] = cp
bytePos = bytePos+1
elseif cp <= 0x7FF then
bytes[bytePos ] = 0xC0 + math.floor(cp / 0x40)
bytes[bytePos+1] = 0x80 + cp % 0x40
bytePos = bytePos+2
elseif cp <= 0xFFFF then
bytes[bytePos ] = 0xE0 + math.floor(cp / 0x1000)
bytes[bytePos+1] = 0x80 + math.floor(cp / 0x40) % 0x40
bytes[bytePos+2] = 0x80 + cp % 0x40
bytePos = bytePos+3
elseif cp <= 0x10FFFF then
bytes[bytePos+3] = 0x80 + cp % 0x40
cp = math.floor(cp / 0x40)
bytes[bytePos+2] = 0x80 + cp % 0x40
cp = math.floor(cp / 0x40)
bytes[bytePos+1] = 0x80 + cp % 0x40
cp = math.floor(cp / 0x40)
bytes[bytePos ] = 0xF0 + cp
bytePos = bytePos+4
else
error("Unicode cannot be greater than U+10FFFF. ("..cp..")")
end
end
return schar(unpack(bytes, 1, bytePos-1))
end
local SHIFT_6 = 2^6
local SHIFT_12 = 2^12
local SHIFT_18 = 2^18
-- string.byte
function utf8codepoint(s, i, j, bytePos)
i = i or 1
j = j or i
if i > j then return --[[void]] end
local c, charLen
if bytePos then
if bytePos > #s then return end
charLen = utf8charbytes(s, bytePos)
c = ssub(s, bytePos, bytePos+charLen-1)
else
c, bytePos = utf8subAndBounds(s, i, i)
charLen = #c
end
local cp
if charLen == 1 then
cp = sbyte(c)
elseif charLen == 2 then
local byte1, byte2 = sbyte(c, 1, 2)
local cp1, cp2 = byte1-0xC0, byte2-0x80
cp = cp1*SHIFT_6 + cp2
elseif charLen == 3 then
local byte1, byte2, byte3 = sbyte(c, 1, 3)
local cp1, cp2, cp3 = byte1-0xE0, byte2-0x80, byte3-0x80
cp = cp1*SHIFT_12 + cp2*SHIFT_6 + cp3
elseif charLen == 4 then
local byte1, byte2, byte3, byte4 = sbyte(c, 1, 4)
local cp1, cp2, cp3, cp4 = byte1-0xF0, byte2-0x80, byte3-0x80, byte4-0x80
cp = cp1*SHIFT_18 + cp2*SHIFT_12 + cp3*SHIFT_6 + cp4
end
return cp, utf8codepoint(s, i+1, j, bytePos+charLen)
end
-- Returns an iterator which returns the next substring and its byte interval.
-- for slice, byteStart, byteEnd in utf8.gensub( str [, sliceLen=1 ] ) do
function utf8gensub(s, sliceLen)
sliceLen = sliceLen or 1
local bytePos = 1
return function(skipBytes)
if skipBytes then bytePos = bytePos+skipBytes end
local from = bytePos
for i = 1, sliceLen do
if bytePos > #s then return end
bytePos = bytePos+utf8charbytes(s, bytePos)
end
local to = bytePos-1
local slice = ssub(s, from, to)
return slice, from, to
end
end
local function binsearch(sortedTable, item, comp)
local head, tail = 1, #sortedTable
local mid = math.floor((head+tail)/2)
if not comp then
while tail-head > 1 do
if sortedTable[tonumber(mid)] > item then
tail = mid
else
head = mid
end
mid = math.floor((head+tail)/2)
end
end
if sortedTable[tonumber(head)] == item then
return true, tonumber(head)
elseif sortedTable[tonumber(tail)] == item then
return true, tonumber(tail)
else
return false
end
end
local function classMatchGenerator(class, plain)
local codes = {}
local ranges = {}
local ignore = false
local range = false
local firstletter = true
local unmatch = false
local it = utf8gensub(class)
local skip
for c, _, be in it do
skip = be
if not ignore and not plain then
if c == "%" then
ignore = true
elseif c == "-" then
table.insert(codes, utf8codepoint(c))
range = true
elseif c == "^" then
if not firstletter then
error("!!!")
else
unmatch = true
end
elseif c == "]" then
break
else
if not range then
table.insert(codes, utf8codepoint(c))
else
table.remove(codes) -- Removing '-'.
table.insert(ranges, {table.remove(codes), utf8codepoint(c)})
range = false
end
end
elseif ignore and not plain then
if c == "a" then -- %a: Represents all ASCII letters.
table.insert(ranges, {65, 90}) -- A - Z
table.insert(ranges, {97, 122}) -- a - z
elseif c == "c" then -- %c: Represents all control characters.
table.insert(ranges, {0, 31})
table.insert(codes, 127)
elseif c == "d" then -- %d: Represents all digits.
table.insert(ranges, {48, 57}) -- 0 - 9
elseif c == "g" then -- %g: Represents all printable characters except space.
table.insert(ranges, {1, 8})
table.insert(ranges, {14, 31})
table.insert(ranges, {33, 132})
table.insert(ranges, {134, 159})
table.insert(ranges, {161, 5759})
table.insert(ranges, {5761, 8191})
table.insert(ranges, {8203, 8231})
table.insert(ranges, {8234, 8238})
table.insert(ranges, {8240, 8286})
table.insert(ranges, {8288, 12287})
elseif c == "l" then -- %l: Represents all lowercase ASCII letters.
table.insert(ranges, {97, 122}) -- a - z
elseif c == "p" then -- %p: Represents all ASCII punctuation characters.
table.insert(ranges, {33, 47})
table.insert(ranges, {58, 64})
table.insert(ranges, {91, 96})
table.insert(ranges, {123, 126})
elseif c == "s" then -- %s: Represents all space characters.
table.insert(ranges, {9, 13})
table.insert(codes, 32)
table.insert(codes, 133)
table.insert(codes, 160)
table.insert(codes, 5760)
table.insert(ranges, {8192, 8202})
table.insert(codes, 8232)
table.insert(codes, 8233)
table.insert(codes, 8239)
table.insert(codes, 8287)
table.insert(codes, 12288)
elseif c == "u" then -- %u: Represents all uppercase ASCII letters.
table.insert(ranges, {65, 90}) -- A - Z
elseif c == "w" then -- %w: Represents all alphanumeric ASCII characters.
table.insert(ranges, {48, 57}) -- 0 - 9
table.insert(ranges, {65, 90}) -- A - Z
table.insert(ranges, {97, 122}) -- a - z
elseif c == "x" then -- %x: Represents all hexadecimal digits.
table.insert(ranges, {48, 57}) -- 0 - 9
table.insert(ranges, {65, 70}) -- A - F
table.insert(ranges, {97, 102}) -- a - f
else
if not range then
table.insert(codes, utf8codepoint(c))
else
table.remove(codes) -- Removing '-'.
table.insert(ranges, {table.remove(codes), utf8codepoint(c)})
range = false
end
end
ignore = false
else
if not range then
table.insert(codes, utf8codepoint(c))
else
table.remove(codes) -- Removing '-'.
table.insert(ranges, {table.remove(codes), utf8codepoint(c)})
range = false
end
ignore = false
end
firstletter = false
end
table.sort(codes)
local function inRanges(charCode)
for _,r in ipairs(ranges) do
if r[1] <= charCode and charCode <= r[2] then
return true
end
end
return false
end
if not unmatch then
return function(charCode)
return binsearch(codes, charCode) or inRanges(charCode)
end, skip
else
return function(charCode)
return charCode ~= -1 and not (binsearch(codes, charCode) or inRanges(charCode))
end, skip
end
end
local cache = setmetatable({}, {__mode="kv"})
local cachePlain = setmetatable({}, {__mode="kv"})
local function matcherGenerator(pat, plain)
local matcher = {functions={}, captures={}}
if not plain then
cache[pat] = matcher
else
cachePlain[pat] = matcher
end
local function simple(func)
return function(cC)
if func(cC) then
matcher:nextFunc()
matcher:nextStr()
else
matcher:reset()
end
end
end
local function star(func)
return function(cC)
if func(cC) then
matcher:fullResetOnNextFunc()
matcher:nextStr()
else
matcher:nextFunc()
end
end
end
local function minus(func)
return function(cC)
if func(cC) then
matcher:fullResetOnNextStr()
end
matcher:nextFunc()
end
end
local function question(func)
return function(cC)
if func(cC) then
matcher:fullResetOnNextFunc()
matcher:nextStr()
end
matcher:nextFunc()
end
end
local function capture(id)
return function(_)
local l = matcher.captures[id][2]-matcher.captures[id][1]
local captured = utf8sub(matcher.string, matcher.captures[id][1], matcher.captures[id][2])
local check = utf8sub(matcher.string, matcher.str, matcher.str+l)
if captured == check then
for _ = 0, l do
matcher:nextStr()
end
matcher:nextFunc()
else
matcher:reset()
end
end
end
local function captureStart(id)
return function(_)
matcher.captures[id][1] = matcher.str
matcher:nextFunc()
end
end
local function captureStop(id)
return function(_)
matcher.captures[id][2] = matcher.str-1
matcher:nextFunc()
end
end
local function balancer(str)
local sum = 0
local bc = utf8sub(str, 1, 1)
local ec = utf8sub(str, 2, 2)
local skip = #bc+#ec
bc, ec = utf8codepoint(bc), utf8codepoint(ec)
return function(cC)
if cC == ec and sum > 0 then
sum = sum-1
if sum == 0 then
matcher:nextFunc()
end
matcher:nextStr()
elseif cC == bc then
sum = sum+1
matcher:nextStr()
else
if sum == 0 or cC == -1 then
sum = 0
matcher:reset()
else
matcher:nextStr()
end
end
end, skip
end
matcher.functions[1] = function(_)
matcher:fullResetOnNextStr()
matcher.seqStart = matcher.str
matcher:nextFunc()
if (matcher.str > matcher.startStr and matcher.fromStart) or matcher.str >= matcher.stringLen then
matcher.stop = true
matcher.seqStart = nil
end
end
local ignore = false
local skip = nil
local lastFunc
local it = (function()
local gen = utf8gensub(pat)
return function()
return gen(skip)
end
end)()
local cs = {}
for c, bs, be in it do
skip = nil
if plain then
table.insert(matcher.functions, simple(classMatchGenerator(c, plain)))
else
if ignore then
if sfind("123456789", c, 1, true) then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
lastFunc = nil
end
table.insert(matcher.functions, capture(tonumber(c)))
elseif c == "b" then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
lastFunc = nil
end
local b
b, skip = balancer(ssub(pat, be+1, be+9))
table.insert(matcher.functions, b)
else
lastFunc = classMatchGenerator("%"..c)
end
ignore = false
else
if c == "*" then
if lastFunc then
table.insert(matcher.functions, star(lastFunc))
lastFunc = nil
else
error("invalid pat after "..ssub(pat, 1, bs))
end
elseif c == "+" then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
table.insert(matcher.functions, star(lastFunc))
lastFunc = nil
else
error("invalid pat after "..ssub(pat, 1, bs))
end
elseif c == "-" then
if lastFunc then
table.insert(matcher.functions, minus(lastFunc))
lastFunc = nil
else
error("invalid pat after "..ssub(pat, 1, bs))
end
elseif c == "?" then
if lastFunc then
table.insert(matcher.functions, question(lastFunc))
lastFunc = nil
else
error("invalid pat after "..ssub(pat, 1, bs))
end
elseif c == "^" then
if bs == 1 then
matcher.fromStart = true
else
error("invalid pat after "..ssub(pat, 1, bs))
end
elseif c == "$" then
if be == #pat then
matcher.toEnd = true
else
error("invalid pat after "..ssub(pat, 1, bs))
end
elseif c == "[" then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
end
lastFunc, skip = classMatchGenerator(ssub(pat, be+1))
elseif c == "(" then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
lastFunc = nil
end
local matchCaps = matcher.captures
table.insert(matchCaps, {})
table.insert(cs, #matchCaps)
table.insert(matcher.functions, captureStart(cs[#cs]))
if ssub(pat, be+1, be+1) == ")" then matchCaps[#matchCaps].empty = true end
elseif c == ")" then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
lastFunc = nil
end
local cap = table.remove(cs)
if not cap then
error("invalid capture: '(' missing")
end
table.insert(matcher.functions, captureStop(cap))
elseif c == "." then
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
end
lastFunc = function(cC) return cC ~= -1 end
elseif c == "%" then
ignore = true
else
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
end
lastFunc = classMatchGenerator(c)
end
end
end
end
if #cs > 0 then
error("invalid capture: ')' missing")
end
if lastFunc then
table.insert(matcher.functions, simple(lastFunc))
end
table.insert(matcher.functions, function()
if matcher.toEnd and matcher.str ~= matcher.stringLen then
matcher:reset()
else
matcher.stop = true
end
end)
matcher.nextFunc = function(self)
self.func = self.func+1
end
matcher.nextStr = function(self)
self.str = self.str+1
end
matcher.strReset = function(self)
local oldReset = self.reset
local str = self.str
self.reset = function(s)
s.str = str
s.reset = oldReset
end
end
matcher.fullResetOnNextFunc = function(self)
local oldReset = self.reset
local func = self.func +1
local str = self.str
self.reset = function(s)
s.func = func
s.str = str
s.reset = oldReset
end
end
matcher.fullResetOnNextStr = function(self)
local oldReset = self.reset
local str = self.str+1
local func = self.func
self.reset = function(s)
s.func = func
s.str = str
s.reset = oldReset
end
end
matcher.process = function(self, str, start)
start = start or 1
self.func = 1
self.startStr = start >= 0 and start or utf8len(str)+start+1
self.seqStart = self.startStr
self.str = self.startStr
self.stringLen = utf8len(str)+1
self.string = str
self.stop = false
self.reset = function(s)
s.func = 1
end
local c
while not self.stop do
if self.str < self.stringLen then
c = utf8sub(str, self.str,self.str)
self.functions[self.func](utf8codepoint(c))
else
self.functions[self.func](-1)
end
end
if self.seqStart then
local captures = {}
for _,pair in pairs(self.captures) do
if pair.empty then
table.insert(captures, pair[1])
else
table.insert(captures, utf8sub(str, pair[1], pair[2]))
end
end
return self.seqStart, self.str-1, unpack(captures)
end
end
return matcher
end
-- string.find
function utf8find(s, pat, init, plain)
local matcher = cache[pat] or matcherGenerator(pat, plain)
return matcher:process(s, init)
end
-- string.match
function utf8match(s, pat, init)
init = init or 1
local found = {utf8find(s, pat, init)}
if found[1] then
if found[3] then
return unpack(found, 3)
end
return utf8sub(s, found[1], found[2])
end
end
-- string.gmatch
function utf8gmatch(s, pat, includeRange)
pat = utf8sub(pat, 1, 1) ~= "^" and pat or "%"..pat
local lastChar = 1
return function()
local found = {utf8find(s, pat, lastChar)}
if found[1] then
lastChar = found[2]+1
if found[includeRange and 1 or 3] then
return unpack(found, includeRange and 1 or 3)
end
return utf8sub(s, found[1], found[2])
end
end
end
local function replace(repl, args)
local ret = ""
if type(repl) == "string" then
local ignore = false
local num
for c in utf8gensub(repl) do
if not ignore then
if c == "%" then
ignore = true
else
ret = ret..c
end
else
num = tonumber(c)
if num then
ret = ret..args[num]
else
ret = ret..c
end
ignore = false
end
end
elseif type(repl) == "table" then
ret = repl[args[1] or args[0]] or ""
elseif type(repl) == "function" then
if #args > 0 then
ret = repl(unpack(args, 1)) or ""
else
ret = repl(args[0]) or ""
end
end
return ret
end
-- string.gsub
function utf8gsub(s, pat, repl, limit)