]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: use slices to simplify the code
authorapocelipes <seve3r@outlook.com>
Mon, 25 Mar 2024 05:58:00 +0000 (05:58 +0000)
committerEmmanuel Odeke <emmanuel@orijtech.com>
Mon, 25 Mar 2024 19:36:03 +0000 (19:36 +0000)
Replace some "reflect.DeepEqual" calls in the tests with
"slices.Equal" which is much faster for slice comparisons.

Remove unnecessary "runeSlice" and redundant helper functions.

Change-Id: Ib5dc41848d7a3c5149f41701d60471a487cff476
GitHub-Last-Rev: 87b5ed043d2935b971aa676cc52b9b2c5b45736b
GitHub-Pull-Request: golang/go#66509
Reviewed-on: https://go-review.googlesource.com/c/go/+/573977
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
src/regexp/all_test.go
src/regexp/exec_test.go
src/regexp/onepass.go
src/regexp/onepass_test.go

index 124313d1af970e5a16396253cd2f352a126c83c5..c9c046b61d09beeb9848e2db4b9a755d6e8c3b3f 100644 (file)
@@ -7,6 +7,7 @@ package regexp
 import (
        "reflect"
        "regexp/syntax"
+       "slices"
        "strings"
        "testing"
        "unicode/utf8"
@@ -519,13 +520,13 @@ func TestSplit(t *testing.T) {
                }
 
                split := re.Split(test.s, test.n)
-               if !reflect.DeepEqual(split, test.out) {
+               if !slices.Equal(split, test.out) {
                        t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out)
                }
 
                if QuoteMeta(test.r) == test.r {
                        strsplit := strings.SplitN(test.s, test.r, test.n)
-                       if !reflect.DeepEqual(split, strsplit) {
+                       if !slices.Equal(split, strsplit) {
                                t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit)
                        }
                }
index 1694230345177cb3c23b7d7d10b1875cf23e7b96..ad33169a21abf96f926b3cc57dbaec9dd6d21023 100644 (file)
@@ -13,6 +13,7 @@ import (
        "os"
        "path/filepath"
        "regexp/syntax"
+       "slices"
        "strconv"
        "strings"
        "testing"
@@ -167,7 +168,7 @@ func testRE2(t *testing.T, file string) {
                        for i := range res {
                                have, suffix := run[i](re, refull, text)
                                want := parseResult(t, file, lineno, res[i])
-                               if !same(have, want) {
+                               if !slices.Equal(have, want) {
                                        t.Errorf("%s:%d: %#q%s.FindSubmatchIndex(%#q) = %v, want %v", file, lineno, re, suffix, text, have, want)
                                        if nfail++; nfail >= 100 {
                                                t.Fatalf("stopping after %d errors", nfail)
@@ -309,18 +310,6 @@ func parseResult(t *testing.T, file string, lineno int, res string) []int {
        return out
 }
 
-func same(x, y []int) bool {
-       if len(x) != len(y) {
-               return false
-       }
-       for i, xi := range x {
-               if xi != y[i] {
-                       return false
-               }
-       }
-       return true
-}
-
 // TestFowler runs this package's regexp API against the
 // POSIX regular expression tests collected by Glenn Fowler
 // at http://www2.research.att.com/~astopen/testregex/testregex.html.
@@ -547,7 +536,7 @@ Reading:
                        if len(have) > len(pos) {
                                have = have[:len(pos)]
                        }
-                       if !same(have, pos) {
+                       if !slices.Equal(have, pos) {
                                t.Errorf("%s:%d: %#q.FindSubmatchIndex(%#q) = %v, want %v", file, lineno, pattern, text, have, pos)
                        }
                }
index b3066e88ee436bddc9f364d4c452f4f64c5c5613..53cbd958394120f513e75a0790c65e619b87c139 100644 (file)
@@ -6,7 +6,7 @@ package regexp
 
 import (
        "regexp/syntax"
-       "sort"
+       "slices"
        "strings"
        "unicode"
        "unicode/utf8"
@@ -282,13 +282,6 @@ func onePassCopy(prog *syntax.Prog) *onePassProg {
        return p
 }
 
-// runeSlice exists to permit sorting the case-folded rune sets.
-type runeSlice []rune
-
-func (p runeSlice) Len() int           { return len(p) }
-func (p runeSlice) Less(i, j int) bool { return p[i] < p[j] }
-func (p runeSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
-
 var anyRuneNotNL = []rune{0, '\n' - 1, '\n' + 1, unicode.MaxRune}
 var anyRune = []rune{0, unicode.MaxRune}
 
@@ -383,7 +376,7 @@ func makeOnePass(p *onePassProg) *onePassProg {
                                for r1 := unicode.SimpleFold(r0); r1 != r0; r1 = unicode.SimpleFold(r1) {
                                        runes = append(runes, r1, r1)
                                }
-                               sort.Sort(runeSlice(runes))
+                               slices.Sort(runes)
                        } else {
                                runes = append(runes, inst.Rune...)
                        }
@@ -407,7 +400,7 @@ func makeOnePass(p *onePassProg) *onePassProg {
                                for r1 := unicode.SimpleFold(r0); r1 != r0; r1 = unicode.SimpleFold(r1) {
                                        runes = append(runes, r1, r1)
                                }
-                               sort.Sort(runeSlice(runes))
+                               slices.Sort(runes)
                        } else {
                                runes = append(runes, inst.Rune[0], inst.Rune[0])
                        }
index 6a42eda391c2c7ad09068c687ca1d84cbd95356f..3f44dc7b150533de1c8dab01cd603637b48b22ee 100644 (file)
@@ -5,8 +5,8 @@
 package regexp
 
 import (
-       "reflect"
        "regexp/syntax"
+       "slices"
        "strings"
        "testing"
 )
@@ -125,10 +125,10 @@ var runeMergeTests = []struct {
 func TestMergeRuneSet(t *testing.T) {
        for ix, test := range runeMergeTests {
                merged, next := mergeRuneSets(&test.left, &test.right, test.leftPC, test.rightPC)
-               if !reflect.DeepEqual(merged, test.merged) {
+               if !slices.Equal(merged, test.merged) {
                        t.Errorf("mergeRuneSet :%d (%v, %v) merged\n have\n%v\nwant\n%v", ix, test.left, test.right, merged, test.merged)
                }
-               if !reflect.DeepEqual(next, test.next) {
+               if !slices.Equal(next, test.next) {
                        t.Errorf("mergeRuneSet :%d(%v, %v) next\n have\n%v\nwant\n%v", ix, test.left, test.right, next, test.next)
                }
        }