-
Notifications
You must be signed in to change notification settings - Fork 84
/
regexp_test.go
1378 lines (1213 loc) · 36.8 KB
/
regexp_test.go
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
package regexp2
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/dlclark/regexp2/syntax"
)
func TestBacktrack_CatastrophicTimeout(t *testing.T) {
r, err := Compile("(.+)*\\?", 0)
if err != nil {
t.Fatal(err)
}
t.Logf("code dump: %v", r.code.Dump())
const subject = "Do you think you found the problem string!"
const earlyAllowance = 10 * time.Millisecond
var lateAllowance = clockPeriod + 500*time.Millisecond // Large allowance in case machine is slow
for _, timeout := range []time.Duration{
-1 * time.Millisecond,
0 * time.Millisecond,
1 * time.Millisecond,
10 * time.Millisecond,
100 * time.Millisecond,
500 * time.Millisecond,
1000 * time.Millisecond,
} {
t.Run(fmt.Sprint(timeout), func(t *testing.T) {
r.MatchTimeout = timeout
start := time.Now()
m, err := r.FindStringMatch(subject)
elapsed := time.Since(start)
if err == nil {
t.Errorf("expected timeout err")
}
if m != nil {
t.Errorf("Expected no match")
}
t.Logf("timeed out after %v", elapsed)
if elapsed < timeout-earlyAllowance {
t.Errorf("Match timed out too quickly (%v instead of expected %v)", elapsed, timeout-earlyAllowance)
}
if elapsed > timeout+lateAllowance {
t.Errorf("Match timed out too late (%v instead of expected %v)", elapsed, timeout+lateAllowance)
}
})
}
}
func TestSetPrefix(t *testing.T) {
r := MustCompile(`^\s*-TEST`, 0)
if r.code.FcPrefix == nil {
t.Fatalf("Expected prefix set [-\\s] but was nil")
}
if r.code.FcPrefix.PrefixSet.String() != "[-\\s]" {
t.Fatalf("Expected prefix set [\\s-] but was %v", r.code.FcPrefix.PrefixSet.String())
}
}
func TestSetInCode(t *testing.T) {
r := MustCompile(`(?<body>\s*(?<name>.+))`, 0)
t.Logf("code dump: %v", r.code.Dump())
if want, got := 1, len(r.code.Sets); want != got {
t.Fatalf("r.code.Sets wanted %v, got %v", want, got)
}
if want, got := "[\\s]", r.code.Sets[0].String(); want != got {
t.Fatalf("first set wanted %v, got %v", want, got)
}
}
func TestRegexp_Basic(t *testing.T) {
r, err := Compile("test(?<named>ing)?", 0)
//t.Logf("code dump: %v", r.code.Dump())
if err != nil {
t.Errorf("unexpected compile err: %v", err)
}
m, err := r.FindStringMatch("this is a testing stuff")
if err != nil {
t.Errorf("unexpected match err: %v", err)
}
if m == nil {
t.Error("Nil match, expected success")
} else {
//t.Logf("Match: %v", m.dump())
}
}
// check all our functions and properties around basic capture groups and referential for Group 0
func TestCapture_Basic(t *testing.T) {
r := MustCompile(`.*\B(SUCCESS)\B.*`, 0)
m, err := r.FindStringMatch("adfadsfSUCCESSadsfadsf")
if err != nil {
t.Fatalf("Unexpected match error: %v", err)
}
if m == nil {
t.Fatalf("Should have matched")
}
if want, got := "adfadsfSUCCESSadsfadsf", m.String(); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 0, m.Index; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 22, m.Length; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 1, len(m.Captures); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := m.String(), m.Captures[0].String(); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 0, m.Captures[0].Index; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 22, m.Captures[0].Length; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
g := m.Groups()
if want, got := 2, len(g); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
// group 0 is always the match
if want, got := m.String(), g[0].String(); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 1, len(g[0].Captures); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
// group 0's capture is always the match
if want, got := m.Captures[0].String(), g[0].Captures[0].String(); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
// group 1 is our first explicit group (unnamed)
if want, got := 7, g[1].Index; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := 7, g[1].Length; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
if want, got := "SUCCESS", g[1].String(); want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
}
func TestEscapeUnescape_Basic(t *testing.T) {
s1 := "#$^*+(){}<>\\|. "
s2 := Escape(s1)
s3, err := Unescape(s2)
if err != nil {
t.Fatalf("Unexpected error during unescape: %v", err)
}
//confirm one way
if want, got := `\#\$\^\*\+\(\)\{\}<>\\\|\.\ `, s2; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
//confirm round-trip
if want, got := s1, s3; want != got {
t.Fatalf("Wanted '%v'\nGot '%v'", want, got)
}
}
func TestGroups_Basic(t *testing.T) {
type d struct {
p string
s string
name []string
num []int
strs []string
}
data := []d{
d{"(?<first_name>\\S+)\\s(?<last_name>\\S+)", // example
"Ryan Byington",
[]string{"0", "first_name", "last_name"},
[]int{0, 1, 2},
[]string{"Ryan Byington", "Ryan", "Byington"}},
d{"((?<One>abc)\\d+)?(?<Two>xyz)(.*)", // example
"abc208923xyzanqnakl",
[]string{"0", "1", "2", "One", "Two"},
[]int{0, 1, 2, 3, 4},
[]string{"abc208923xyzanqnakl", "abc208923", "anqnakl", "abc", "xyz"}},
d{"((?<256>abc)\\d+)?(?<16>xyz)(.*)", // numeric names
"0272saasdabc8978xyz][]12_+-",
[]string{"0", "1", "2", "16", "256"},
[]int{0, 1, 2, 16, 256},
[]string{"abc8978xyz][]12_+-", "abc8978", "][]12_+-", "xyz", "abc"}},
d{"((?<4>abc)(?<digits>\\d+))?(?<2>xyz)(?<everything_else>.*)", // mix numeric and string names
"0272saasdabc8978xyz][]12_+-",
[]string{"0", "1", "2", "digits", "4", "everything_else"},
[]int{0, 1, 2, 3, 4, 5},
[]string{"abc8978xyz][]12_+-", "abc8978", "xyz", "8978", "abc", "][]12_+-"}},
d{"(?<first_name>\\S+)\\s(?<first_name>\\S+)", // dupe string names
"Ryan Byington",
[]string{"0", "first_name"},
[]int{0, 1},
[]string{"Ryan Byington", "Byington"}},
d{"(?<15>\\S+)\\s(?<15>\\S+)", // dupe numeric names
"Ryan Byington",
[]string{"0", "15"},
[]int{0, 15},
[]string{"Ryan Byington", "Byington"}},
// *** repeated from above, but with alt cap syntax ***
d{"(?'first_name'\\S+)\\s(?'last_name'\\S+)", //example
"Ryan Byington",
[]string{"0", "first_name", "last_name"},
[]int{0, 1, 2},
[]string{"Ryan Byington", "Ryan", "Byington"}},
d{"((?'One'abc)\\d+)?(?'Two'xyz)(.*)", // example
"abc208923xyzanqnakl",
[]string{"0", "1", "2", "One", "Two"},
[]int{0, 1, 2, 3, 4},
[]string{"abc208923xyzanqnakl", "abc208923", "anqnakl", "abc", "xyz"}},
d{"((?'256'abc)\\d+)?(?'16'xyz)(.*)", // numeric names
"0272saasdabc8978xyz][]12_+-",
[]string{"0", "1", "2", "16", "256"},
[]int{0, 1, 2, 16, 256},
[]string{"abc8978xyz][]12_+-", "abc8978", "][]12_+-", "xyz", "abc"}},
d{"((?'4'abc)(?'digits'\\d+))?(?'2'xyz)(?'everything_else'.*)", // mix numeric and string names
"0272saasdabc8978xyz][]12_+-",
[]string{"0", "1", "2", "digits", "4", "everything_else"},
[]int{0, 1, 2, 3, 4, 5},
[]string{"abc8978xyz][]12_+-", "abc8978", "xyz", "8978", "abc", "][]12_+-"}},
d{"(?'first_name'\\S+)\\s(?'first_name'\\S+)", // dupe string names
"Ryan Byington",
[]string{"0", "first_name"},
[]int{0, 1},
[]string{"Ryan Byington", "Byington"}},
d{"(?'15'\\S+)\\s(?'15'\\S+)", // dupe numeric names
"Ryan Byington",
[]string{"0", "15"},
[]int{0, 15},
[]string{"Ryan Byington", "Byington"}},
}
fatalf := func(re *Regexp, v d, format string, args ...interface{}) {
args = append(args, v, re.code.Dump())
t.Fatalf(format+" using test data: %#v\ndump:%v", args...)
}
validateGroupNamesNumbers := func(re *Regexp, v d) {
if len(v.name) != len(v.num) {
fatalf(re, v, "Invalid data, group name count and number count must match")
}
groupNames := re.GetGroupNames()
if !reflect.DeepEqual(groupNames, v.name) {
fatalf(re, v, "group names expected: %v, actual: %v", v.name, groupNames)
}
groupNums := re.GetGroupNumbers()
if !reflect.DeepEqual(groupNums, v.num) {
fatalf(re, v, "group numbers expected: %v, actual: %v", v.num, groupNums)
}
// make sure we can freely get names and numbers from eachother
for i := range groupNums {
if want, got := groupNums[i], re.GroupNumberFromName(groupNames[i]); want != got {
fatalf(re, v, "group num from name Wanted '%v'\nGot '%v'", want, got)
}
if want, got := groupNames[i], re.GroupNameFromNumber(groupNums[i]); want != got {
fatalf(re, v, "group name from num Wanted '%v'\nGot '%v'", want, got)
}
}
}
for _, v := range data {
// compile the regex
re := MustCompile(v.p, 0)
// validate our group name/num info before execute
validateGroupNamesNumbers(re, v)
m, err := re.FindStringMatch(v.s)
if err != nil {
fatalf(re, v, "Unexpected error in match: %v", err)
}
if m == nil {
fatalf(re, v, "Match is nil")
}
if want, got := len(v.strs), m.GroupCount(); want != got {
fatalf(re, v, "GroupCount() Wanted '%v'\nGot '%v'", want, got)
}
g := m.Groups()
if want, got := len(v.strs), len(g); want != got {
fatalf(re, v, "len(m.Groups()) Wanted '%v'\nGot '%v'", want, got)
}
// validate each group's value from the execute
for i := range v.name {
grp1 := m.GroupByName(v.name[i])
grp2 := m.GroupByNumber(v.num[i])
// should be identical reference
if grp1 != grp2 {
fatalf(re, v, "Expected GroupByName and GroupByNumber to return same result for %v, %v", v.name[i], v.num[i])
}
if want, got := v.strs[i], grp1.String(); want != got {
fatalf(re, v, "Value[%v] Wanted '%v'\nGot '%v'", i, want, got)
}
}
// validate our group name/num info after execute
validateGroupNamesNumbers(re, v)
}
}
func TestErr_GroupName(t *testing.T) {
// group 0 is off limits
if _, err := Compile("foo(?<0>bar)", 0); err == nil {
t.Fatalf("zero group, expected error during compile")
} else if want, got := "error parsing regexp: capture number cannot be zero in `foo(?<0>bar)`", err.Error(); want != got {
t.Fatalf("invalid error text, want '%v', got '%v'", want, got)
}
if _, err := Compile("foo(?'0'bar)", 0); err == nil {
t.Fatalf("zero group, expected error during compile")
} else if want, got := "error parsing regexp: capture number cannot be zero in `foo(?'0'bar)`", err.Error(); want != got {
t.Fatalf("invalid error text, want '%v', got '%v'", want, got)
}
// group tag can't start with a num
if _, err := Compile("foo(?<1bar>)", 0); err == nil {
t.Fatalf("invalid group name, expected error during compile")
} else if want, got := "error parsing regexp: invalid group name: group names must begin with a word character and have a matching terminator in `foo(?<1bar>)`", err.Error(); want != got {
t.Fatalf("invalid error text, want '%v', got '%v'", want, got)
}
if _, err := Compile("foo(?'1bar')", 0); err == nil {
t.Fatalf("invalid group name, expected error during compile")
} else if want, got := "error parsing regexp: invalid group name: group names must begin with a word character and have a matching terminator in `foo(?'1bar')`", err.Error(); want != got {
t.Fatalf("invalid error text, want '%v', got '%v'", want, got)
}
// missing closing group tag
if _, err := Compile("foo(?<bar)", 0); err == nil {
t.Fatalf("invalid group name, expected error during compile")
} else if want, got := "error parsing regexp: invalid group name: group names must begin with a word character and have a matching terminator in `foo(?<bar)`", err.Error(); want != got {
t.Fatalf("invalid error text, want '%v', got '%v'", want, got)
}
if _, err := Compile("foo(?'bar)", 0); err == nil {
t.Fatalf("invalid group name, expected error during compile")
} else if want, got := "error parsing regexp: invalid group name: group names must begin with a word character and have a matching terminator in `foo(?'bar)`", err.Error(); want != got {
t.Fatalf("invalid error text, want '%v', got '%v'", want, got)
}
}
func TestConstantUneffected(t *testing.T) {
// had a bug where "constant" sets would get modified with alternations and be broken in memory until restart
// this meant that if you used a known-set (like \s) in a larger set it would "poison" \s for the process
re := MustCompile(`(\s|\*)test\s`, 0)
if want, got := 2, len(re.code.Sets); want != got {
t.Fatalf("wanted %v sets, got %v", want, got)
}
if want, got := "[\\*\\s]", re.code.Sets[0].String(); want != got {
t.Fatalf("wanted set 0 %v, got %v", want, got)
}
if want, got := "[\\s]", re.code.Sets[1].String(); want != got {
t.Fatalf("wanted set 1 %v, got %v", want, got)
}
}
func TestAlternationConstAndEscape(t *testing.T) {
re := MustCompile(`\:|\s`, 0)
if want, got := 1, len(re.code.Sets); want != got {
t.Fatalf("wanted %v sets, got %v", want, got)
}
if want, got := "[:\\s]", re.code.Sets[0].String(); want != got {
t.Fatalf("wanted set 0 %v, got %v", want, got)
}
}
func TestStartingCharsOptionalNegate(t *testing.T) {
// to maintain matching with the corefx we've made the negative char classes be negative and the
// categories they contain positive. This means they're not combinable or suitable for prefixes.
// In general this could be a fine thing since negatives are extremely wide groups and not
// missing much on prefix optimizations.
// the below expression *could* have a prefix of [\S\d] but
// this requires a change in charclass.go when setting
// NotSpaceClass = getCharSetFromCategoryString()
// to negate the individual categories rather than the CharSet itself
// this would deviate from corefx
re := MustCompile(`(^(\S{2} )?\S{2}(\d+|/) *\S{3}\S{3} ?\d{2,4}[A-Z] ?\d{2}[A-Z]{3}|(\S{2} )?\d{2,4})`, 0)
if re.code.FcPrefix != nil {
t.Fatalf("FcPrefix wanted nil, got %v", re.code.FcPrefix)
}
}
func TestParseNegativeDigit(t *testing.T) {
re := MustCompile(`\D`, 0)
if want, got := 1, len(re.code.Sets); want != got {
t.Fatalf("wanted %v sets, got %v", want, got)
}
if want, got := "[\\P{Nd}]", re.code.Sets[0].String(); want != got {
t.Fatalf("wanted set 0 %v, got %v", want, got)
}
}
func TestRunNegativeDigit(t *testing.T) {
re := MustCompile(`\D`, 0)
m, err := re.MatchString("this is a test")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !m {
t.Fatalf("Expected match")
}
}
func TestCancellingClasses(t *testing.T) {
// [\w\W\s] should become "." because it means "anything"
re := MustCompile(`[\w\W\s]`, 0)
if want, got := 1, len(re.code.Sets); want != got {
t.Fatalf("wanted %v sets, got %v", want, got)
}
if want, got := syntax.AnyClass().String(), re.code.Sets[0].String(); want != got {
t.Fatalf("wanted set 0 %v, got %v", want, got)
}
}
func TestConcatLoopCaptureSet(t *testing.T) {
//(A|B)*?CD different Concat/Loop/Capture/Set (had [A-Z] should be [AB])
// we were not copying the Sets in the prefix FC stack, so the underlying sets were unexpectedly mutating
// so set [AB] becomes [ABC] when we see the the static C in FC stack generation (which are the valid start chars),
// but that was mutating the tree node's original set [AB] because even though we copied the slie header,
// the two header's pointed to the same underlying byte array...which was mutated.
re := MustCompile(`(A|B)*CD`, 0)
if want, got := 1, len(re.code.Sets); want != got {
t.Fatalf("wanted %v sets, got %v", want, got)
}
if want, got := "[AB]", re.code.Sets[0].String(); want != got {
t.Fatalf("wanted set 0 %v, got %v", want, got)
}
}
func TestFirstcharsIgnoreCase(t *testing.T) {
//((?i)AB(?-i)C|D)E different Firstchars (had [da] should be [ad])
// we were not canonicalizing when converting the prefix set to lower case
// so our set's were potentially not searching properly
re := MustCompile(`((?i)AB(?-i)C|D)E`, 0)
if re.code.FcPrefix == nil {
t.Fatalf("wanted prefix, got nil")
}
if want, got := "[ad]", re.code.FcPrefix.PrefixSet.String(); want != got {
t.Fatalf("wanted prefix %v, got %v", want, got)
}
}
func TestRepeatingGroup(t *testing.T) {
re := MustCompile(`(data?)+`, 0)
m, err := re.FindStringMatch("datadat")
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if m == nil {
t.Fatalf("Expected match")
}
g := m.GroupByNumber(1)
if g == nil {
t.Fatalf("Expected group")
}
if want, got := 2, len(g.Captures); want != got {
t.Fatalf("wanted cap count %v, got %v", want, got)
}
if want, got := g.Captures[1].String(), g.Capture.String(); want != got {
t.Fatalf("expected last capture of the group to be embedded")
}
if want, got := "data", g.Captures[0].String(); want != got {
t.Fatalf("expected cap 0 to be %v, got %v", want, got)
}
if want, got := "dat", g.Captures[1].String(); want != got {
t.Fatalf("expected cap 1 to be %v, got %v", want, got)
}
}
func TestFindNextMatch_Basic(t *testing.T) {
re := MustCompile(`(T|E)(?=h|E|S|$)`, 0)
m, err := re.FindStringMatch(`This is a TEST`)
if err != nil {
t.Fatalf("Unexpected err 0: %v", err)
}
if m == nil {
t.Fatalf("Expected match 0")
}
if want, got := 0, m.Index; want != got {
t.Fatalf("expected match 0 to start at %v, got %v", want, got)
}
m, err = re.FindNextMatch(m)
if err != nil {
t.Fatalf("Unexpected err 1: %v", err)
}
if m == nil {
t.Fatalf("Expected match 1")
}
if want, got := 10, m.Index; want != got {
t.Fatalf("expected match 1 to start at %v, got %v", want, got)
}
m, err = re.FindNextMatch(m)
if err != nil {
t.Fatalf("Unexpected err 2: %v", err)
}
if m == nil {
t.Fatalf("Expected match 2")
}
if want, got := 11, m.Index; want != got {
t.Fatalf("expected match 2 to start at %v, got %v", want, got)
}
m, err = re.FindNextMatch(m)
if err != nil {
t.Fatalf("Unexpected err 3: %v", err)
}
if m == nil {
t.Fatalf("Expected match 3")
}
if want, got := 13, m.Index; want != got {
t.Fatalf("expected match 3 to start at %v, got %v", want, got)
}
}
func TestUnicodeSupplementaryCharSetMatch(t *testing.T) {
//0x2070E 0x20731 𠜱 0x20779 𠝹
re := MustCompile("[𠜎-𠝹]", 0)
if m, err := re.MatchString("\u2070"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if m {
t.Fatalf("Unexpected match")
}
if m, err := re.MatchString("𠜱"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
}
func TestUnicodeSupplementaryCharInRange(t *testing.T) {
//0x2070E 0x20731 𠜱 0x20779 𠝹
re := MustCompile(".", 0)
if m, err := re.MatchString("\u2070"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
if m, err := re.MatchString("𠜱"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
}
func TestUnicodeScriptSets(t *testing.T) {
re := MustCompile(`\p{Katakana}+`, 0)
if m, err := re.MatchString("\u30A0\u30FF"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
}
func TestHexadecimalCurlyBraces(t *testing.T) {
re := MustCompile(`\x20`, 0)
if m, err := re.MatchString(" "); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{C4}`, 0)
if m, err := re.MatchString("Ä"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{0C5}`, 0)
if m, err := re.MatchString("Å"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{00C6}`, 0)
if m, err := re.MatchString("Æ"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{1FF}`, 0)
if m, err := re.MatchString("ǿ"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{02FF}`, 0)
if m, err := re.MatchString("˿"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{1392}`, 0)
if m, err := re.MatchString("᎒"); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
re = MustCompile(`\x{0010ffff}`, 0)
if m, err := re.MatchString(string(rune(0x10ffff))); err != nil {
t.Fatalf("Unexpected err: %v", err)
} else if !m {
t.Fatalf("Expected match")
}
if _, err := Compile(`\x2R`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x0`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{2`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{2R`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{2R}`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{}`, 0); err == nil {
t.Fatalf("Expected error")
}
if _, err := Compile(`\x{10000`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{1234`, 0); err == nil {
t.Fatal("Expected error")
}
if _, err := Compile(`\x{123456789}`, 0); err == nil {
t.Fatal("Expected error")
}
}
func TestEmptyCharClass(t *testing.T) {
if _, err := Compile("[]", 0); err == nil {
t.Fatal("Empty char class isn't valid outside of ECMAScript mode")
}
}
func TestECMAEmptyCharClass(t *testing.T) {
re := MustCompile("[]", ECMAScript)
if m, err := re.MatchString("a"); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
}
func TestDot(t *testing.T) {
re := MustCompile(".", 0)
if m, err := re.MatchString("\r"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestECMADot(t *testing.T) {
re := MustCompile(".", ECMAScript)
if m, err := re.MatchString("\r"); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
}
func TestDecimalLookahead(t *testing.T) {
re := MustCompile(`\1(A)`, 0)
m, err := re.FindStringMatch("AA")
if err != nil {
t.Fatal(err)
} else if m != nil {
t.Fatal("Expected no match")
}
}
func TestECMADecimalLookahead(t *testing.T) {
re := MustCompile(`\1(A)`, ECMAScript)
m, err := re.FindStringMatch("AA")
if err != nil {
t.Fatal(err)
}
if c := m.GroupCount(); c != 2 {
t.Fatalf("Group count !=2 (%d)", c)
}
if s := m.GroupByNumber(0).String(); s != "A" {
t.Fatalf("Group0 != 'A' ('%s')", s)
}
if s := m.GroupByNumber(1).String(); s != "A" {
t.Fatalf("Group1 != 'A' ('%s')", s)
}
}
func TestECMAOctal(t *testing.T) {
re := MustCompile(`\100`, ECMAScript)
if m, err := re.MatchString("@"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
if m, err := re.MatchString("x"); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
re = MustCompile(`\377`, ECMAScript)
if m, err := re.MatchString("\u00ff"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
re = MustCompile(`\400`, ECMAScript)
if m, err := re.MatchString(" 0"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestECMAInvalidEscape(t *testing.T) {
re := MustCompile(`\x0`, ECMAScript)
if m, err := re.MatchString("x0"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
re = MustCompile(`\x0z`, ECMAScript)
if m, err := re.MatchString("x0z"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestECMANamedGroup(t *testing.T) {
re := MustCompile(`\k`, ECMAScript)
if m, err := re.MatchString("k"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
re = MustCompile(`\k'test'`, ECMAScript)
if m, err := re.MatchString(`k'test'`); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
re = MustCompile(`\k<test>`, ECMAScript)
if m, err := re.MatchString(`k<test>`); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
_, err := Compile(`(?<title>\w+), yes \k'title'`, ECMAScript)
if err == nil {
t.Fatal("Expected error")
}
re = MustCompile(`(?<title>\w+), yes \k<title>`, ECMAScript)
if m, err := re.MatchString("sir, yes sir"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
re = MustCompile(`\k<title>, yes (?<title>\w+)`, ECMAScript)
if m, err := re.MatchString(", yes sir"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
_, err = Compile(`\k<(?<name>)>`, ECMAScript)
if err == nil {
t.Fatal("Expected error")
}
MustCompile(`\k<(<name>)>`, ECMAScript)
_, err = Compile(`\k<(<name>)>`, 0)
if err == nil {
t.Fatal("Expected error")
}
re = MustCompile(`\'|\<?`, 0)
if m, err := re.MatchString("'"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
if m, err := re.MatchString("<"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestECMAInvalidEscapeCharClass(t *testing.T) {
re := MustCompile(`[\x0]`, ECMAScript)
if m, err := re.MatchString("x"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
if m, err := re.MatchString("0"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
if m, err := re.MatchString("z"); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
}
func TestECMAScriptXCurlyBraceEscape(t *testing.T) {
re := MustCompile(`\x{20}`, ECMAScript)
if m, err := re.MatchString(" "); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
if m, err := re.MatchString("xxxxxxxxxxxxxxxxxxxx"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestEcmaScriptUnicodeRange(t *testing.T) {
r, err := Compile(`([\u{001a}-\u{ffff}]+)`, ECMAScript|Unicode)
if err != nil {
panic(err)
}
m, err := r.FindStringMatch("qqqq")
if err != nil {
panic(err)
}
if m == nil {
t.Fatal("Expected non-nil, got nil")
}
}
func TestNegateRange(t *testing.T) {
re := MustCompile(`[\D]`, 0)
if m, err := re.MatchString("A"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestECMANegateRange(t *testing.T) {
re := MustCompile(`[\D]`, ECMAScript)
if m, err := re.MatchString("A"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestDollar(t *testing.T) {
// PCRE/C# allow \n to match to $ at end-of-string in singleline mode...
// a weird edge-case kept for compatibility, ECMAScript/RE2 mode don't allow it
re := MustCompile(`ac$`, 0)
if m, err := re.MatchString("ac\n"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestECMADollar(t *testing.T) {
re := MustCompile(`ac$`, ECMAScript)
if m, err := re.MatchString("ac\n"); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
}
func TestThreeByteUnicode_InputOnly(t *testing.T) {
// confirm the bmprefix properly ignores 3-byte unicode in the input value
// this used to panic
re := MustCompile("高", 0)
if m, err := re.MatchString("📍Test高"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestMultibyteUnicode_MatchPartialPattern(t *testing.T) {
re := MustCompile("猟な", 0)
if m, err := re.MatchString("なあ🍺な"); err != nil {
t.Fatal(err)
} else if m {
t.Fatal("Expected no match")
}
}
func TestMultibyteUnicode_Match(t *testing.T) {
re := MustCompile("猟な", 0)
if m, err := re.MatchString("なあ🍺猟な"); err != nil {
t.Fatal(err)
} else if !m {
t.Fatal("Expected match")
}
}
func TestAlternationNamedOptions_Errors(t *testing.T) {
// all of these should give an error "error parsing regexp:"
data := []string{
"(?(?e))", "(?(?a)", "(?(?", "(?(", "?(a:b)", "?(a)", "?(a|b)", "?((a)", "?((a)a", "?((a)a|", "?((a)a|b",
"(?(?i))", "(?(?I))", "(?(?m))", "(?(?M))", "(?(?s))", "(?(?S))", "(?(?x))", "(?(?X))", "(?(?n))", "(?(?N))", " (?(?n))",
}
for _, p := range data {
re, err := Compile(p, 0)
if err == nil {
t.Fatal("Expected error, got nil")
}
if re != nil {
t.Fatal("Expected unparsed regexp, got non-nil")
}
if !strings.HasPrefix(err.Error(), "error parsing regexp: ") {
t.Fatalf("Wanted parse error, got '%v'", err)
}
}
}
func TestAlternationNamedOptions_Success(t *testing.T) {
data := []struct {
pattern string
input string
expectSuccess bool
matchVal string
}{
{"(?(cat)|dog)", "cat", true, ""},