diff options
| author | Russ Cox <rsc@golang.org> | 2023-06-28 17:45:26 -0400 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-08-16 16:02:30 +0000 |
| commit | 98c9f271d67b501ecf2ce995539abd2cdc81d505 (patch) | |
| tree | 41509327c105a85923bcdd368146570b87d531da /src/regexp/syntax/parse_test.go | |
| parent | 5a3048bf0eefd2f99382a980f975d6a1fb6b921a (diff) | |
| download | go-98c9f271d67b501ecf2ce995539abd2cdc81d505.tar.xz | |
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 <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/regexp/syntax/parse_test.go')
| -rw-r--r-- | src/regexp/syntax/parse_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/regexp/syntax/parse_test.go b/src/regexp/syntax/parse_test.go index d7999046e0..0f885bd5c8 100644 --- a/src/regexp/syntax/parse_test.go +++ b/src/regexp/syntax/parse_test.go @@ -590,3 +590,39 @@ func TestToStringEquivalentParse(t *testing.T) { } } } + +var stringTests = []struct { + re string + out string +}{ + {`x(?i:ab*c|d?e)1`, `x(?i:AB*C|D?E)1`}, + {`x(?i:ab*cd?e)1`, `x(?i:AB*CD?E)1`}, + {`0(?i:ab*c|d?e)1`, `(?i:0(?:AB*C|D?E)1)`}, + {`0(?i:ab*cd?e)1`, `(?i:0AB*CD?E1)`}, + {`x(?i:ab*c|d?e)`, `x(?i:AB*C|D?E)`}, + {`x(?i:ab*cd?e)`, `x(?i:AB*CD?E)`}, + {`0(?i:ab*c|d?e)`, `(?i:0(?:AB*C|D?E))`}, + {`0(?i:ab*cd?e)`, `(?i:0AB*CD?E)`}, + {`(?i:ab*c|d?e)1`, `(?i:(?:AB*C|D?E)1)`}, + {`(?i:ab*cd?e)1`, `(?i:AB*CD?E1)`}, + {`(?i:ab)[123](?i:cd)`, `(?i:AB[1-3]CD)`}, + {`(?i:ab*c|d?e)`, `(?i:AB*C|D?E)`}, + {`[Aa][Bb]`, `(?i:AB)`}, + {`[Aa][Bb]*[Cc]`, `(?i:AB*C)`}, + {`A(?:[Bb][Cc]|[Dd])[Zz]`, `A(?i:(?:BC|D)Z)`}, + {`[Aa](?:[Bb][Cc]|[Dd])Z`, `(?i:A(?:BC|D))Z`}, +} + +func TestString(t *testing.T) { + for _, tt := range stringTests { + re, err := Parse(tt.re, Perl) + if err != nil { + t.Errorf("Parse(%#q): %v", tt.re, err) + continue + } + out := re.String() + if out != tt.out { + t.Errorf("Parse(%#q).String() = %#q, want %#q", tt.re, out, tt.out) + } + } +} |
