]> Cypherpunks repositories - gostls13.git/commitdiff
regexp/syntax: simplify the code
authorapocelipes <seve3r@outlook.com>
Mon, 25 Mar 2024 14:13:35 +0000 (14:13 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 26 Mar 2024 19:50:03 +0000 (19:50 +0000)
Use the slices package and the built-in max to simplify the code.
There's no noticeable performance change in this modification.

Change-Id: I96e46ba8ab1323f1ba0b8c9b827836e217772cf2
GitHub-Last-Rev: f0111ac7e220f7dac03290125a3a83831012f235
GitHub-Pull-Request: golang/go#66511
Reviewed-on: https://go-review.googlesource.com/c/go/+/573978
Auto-Submit: Keith Randall <khr@golang.org>
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>
Reviewed-by: Than McIntosh <thanm@google.com>
src/regexp/syntax/parse.go
src/regexp/syntax/regexp.go

index 6a11b53fb1806d121010d7551cf528a208f57158..6ed6491c807a2fa098bb2088eb73ece41aa19a6e 100644 (file)
@@ -249,9 +249,7 @@ func (p *parser) calcSize(re *Regexp, force bool) int64 {
                size = int64(re.Max)*sub + int64(re.Max-re.Min)
        }
 
-       if size < 1 {
-               size = 1
-       }
+       size = max(1, size)
        p.size[re] = size
        return size
 }
index 4fa7d0e2f8330dca2cdf4388502c88459a05dafa..8ad3653abba3ccc36b4e38673a7048e583de2e0d 100644 (file)
@@ -8,6 +8,7 @@ package syntax
 // In this package, re is always a *Regexp and r is always a rune.
 
 import (
+       "slices"
        "strconv"
        "strings"
        "unicode"
@@ -75,24 +76,10 @@ func (x *Regexp) Equal(y *Regexp) bool {
                }
 
        case OpLiteral, OpCharClass:
-               if len(x.Rune) != len(y.Rune) {
-                       return false
-               }
-               for i, r := range x.Rune {
-                       if r != y.Rune[i] {
-                               return false
-                       }
-               }
+               return slices.Equal(x.Rune, y.Rune)
 
        case OpAlternate, OpConcat:
-               if len(x.Sub) != len(y.Sub) {
-                       return false
-               }
-               for i, sub := range x.Sub {
-                       if !sub.Equal(y.Sub[i]) {
-                               return false
-                       }
-               }
+               return slices.EqualFunc(x.Sub, y.Sub, func(a, b *Regexp) bool { return a.Equal(b) })
 
        case OpStar, OpPlus, OpQuest:
                if x.Flags&NonGreedy != y.Flags&NonGreedy || !x.Sub[0].Equal(y.Sub[0]) {