From 98c9f271d67b501ecf2ce995539abd2cdc81d505 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 28 Jun 2023 17:45:26 -0400 Subject: regexp/syntax: use more compact Regexp.String output Compact the Regexp.String output. It was only ever intended for debugging, but there are at least some uses in the wild where regexps are built up using regexp/syntax and then formatted using the String method. Compact the output to help that use case. Specifically: - Compact 2-element character class ranges: [a-b] -> [ab]. - Aggregate flags: (?i:A)(?i:B)*(?i:C)|(?i:D)?(?i:E) -> (?i:AB*C|D?E). Fixes #57950. Change-Id: I1161d0e3aa6c3ae5a302677032bb7cd55caae5fb Reviewed-on: https://go-review.googlesource.com/c/go/+/507015 TryBot-Result: Gopher Robot Reviewed-by: Than McIntosh Run-TryBot: Russ Cox Reviewed-by: Rob Pike Auto-Submit: Russ Cox --- src/regexp/syntax/parse.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/regexp/syntax/parse.go') diff --git a/src/regexp/syntax/parse.go b/src/regexp/syntax/parse.go index a4ccfe3bdb..6b360b8700 100644 --- a/src/regexp/syntax/parse.go +++ b/src/regexp/syntax/parse.go @@ -1863,6 +1863,22 @@ func cleanClass(rp *[]rune) []rune { return r[:w] } +// inCharClass reports whether r is in the class. +// It assumes the class has been cleaned by cleanClass. +func inCharClass(r rune, class []rune) bool { + _, ok := sort.Find(len(class)/2, func(i int) int { + lo, hi := class[2*i], class[2*i+1] + if r > hi { + return +1 + } + if r < lo { + return -1 + } + return 0 + }) + return ok +} + // appendLiteral returns the result of appending the literal x to the class r. func appendLiteral(r []rune, x rune, flags Flags) []rune { if flags&FoldCase != 0 { -- cgit v1.3