Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regexp/syntax: optimize OpCharClass fold case in calcFlags #69304

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 87 additions & 12 deletions src/regexp/syntax/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,7 @@ func calcFlags(re *Regexp, flags *map[*Regexp]printFlags) (must, cant printFlags
case OpCharClass:
// If literal is fold-sensitive, return 0, flagI - (?i) has been compiled out.
// If literal is not fold-sensitive, return 0, 0.
for i := 0; i < len(re.Rune); i += 2 {
lo := max(minFold, re.Rune[i])
hi := min(maxFold, re.Rune[i+1])
for r := lo; r <= hi; r++ {
for f := unicode.SimpleFold(r); f != r; f = unicode.SimpleFold(f) {
if !(lo <= f && f <= hi) && !inCharClass(f, re.Rune) {
return 0, flagI
}
}
}
}
return 0, 0
return calcFlagsI(re)

case OpAnyCharNotNL: // (?-s).
return 0, flagS
Expand Down Expand Up @@ -221,6 +210,92 @@ func calcFlags(re *Regexp, flags *map[*Regexp]printFlags) (must, cant printFlags
}
}

// calcFlagsI determines if the case-folding flag (?i) is required for character classes.
// It checks whether to look inside or outside character ranges for case folding, depending on which range is smaller, to minimize unnecessary checks.
func calcFlagsI(re *Regexp) (must, cant printFlags) {
if len(re.Rune) < 2 {
return 0, 0
}

maxRange := min(maxFold, re.Rune[len(re.Rune)-1])
pre := rune(minFold)
inside, outside := 0, int(maxFold-maxRange)
checkInRange := true

// If last range dominates the whole range,
// we can check outside the range.
lastRange := max(0, maxRange-re.Rune[len(re.Rune)-2])
if lastRange > rune(outside)+re.Rune[len(re.Rune)-2] {
checkInRange = false
goto check
}

// If the range from last rune to maxFold dominates,
// we can check inside the range.
if outside > int(re.Rune[len(re.Rune)-1]) {
goto check
}

// 2 conditions above should catch most cases.
// If not, do a slow calculation
for i := 0; i < len(re.Rune); i += 2 {
lo := max(minFold, re.Rune[i])
hi := min(maxFold, re.Rune[i+1])
if lo > hi {
continue
}

inside += int(hi - lo + 1)
outside += int(lo - pre)
pre = max(minFold, hi+1)
}

checkInRange = inside < outside
check:
if checkInRange {
for i := 0; i < len(re.Rune); i += 2 {
lo := max(minFold, re.Rune[i])
hi := min(maxFold, re.Rune[i+1])
for r := lo; r <= hi; r++ {
for f := unicode.SimpleFold(r); f != r; f = unicode.SimpleFold(f) {
if !(lo <= f && f <= hi) && !inCharClass(f, re.Rune) {
return 0, flagI
}
}
}
}

return 0, 0
}

// Check characters outside the defined range
pre = minFold
for i := 0; i < len(re.Rune); i += 2 {
lo := re.Rune[i]
hi := min(maxFold, re.Rune[i+1])
// Between `pre` and `lo` (exclusive)
for r := pre; r < lo; r++ {
for f := unicode.SimpleFold(r); f != r; f = unicode.SimpleFold(f) {
if inCharClass(f, re.Rune) {
return 0, flagI
}
}
}
pre = max(minFold, hi+1)
}

// Check characters between `pre` and `maxFold`
for r := pre; r <= maxFold; r++ {
for f := unicode.SimpleFold(r); f != r; f = unicode.SimpleFold(f) {
if inCharClass(f, re.Rune) {
return 0, flagI
}
}
}

return 0, 0
}

// writeRegexp writes the Perl syntax for the regular expression re to b.
func writeRegexp(b *strings.Builder, re *Regexp, f printFlags, flags map[*Regexp]printFlags) {
f |= flags[re]
Expand Down
30 changes: 30 additions & 0 deletions src/regexp/syntax/regexp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syntax

import "testing"

var benchmarkTests = []string{
`^(.*);$|^;(.*)`,
`(foo|bar$)x*`,
`[^=,]`,
`([^=,]+)=([^=,]+)`,
`([^=,]+)=([^=,]+),.*`,
}

func BenchmarkString(b *testing.B) {
for _, tt := range benchmarkTests {
re, err := Parse(tt, Perl|DotNL)
if err != nil {
b.Fatalf("Parse(%#q) = error %v", tt, err)
}

b.Run(tt, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = re.String()
}
})
}
}
1 change: 1 addition & 0 deletions src/regexp/syntax/simplify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var simplifyTests = []struct {
{`(?i)[a-z]`, "[A-Za-z\u017F\u212A]"},
{`(?i)[\x00-\x{FFFD}]`, "[\\x00-\uFFFD]"},
{`(?i)[\x00-\x{10FFFF}]`, `(?s:.)`},
{`[xX][O-u]`, `(?i:X)[O-u]`},

// Empty string as a regular expression.
// The empty string must be preserved inside parens in order
Expand Down