Skip to content

Commit

Permalink
regexp/syntax: fix handling of \Q...\E
Browse files Browse the repository at this point in the history
It's not a group: must handle the inside as a sequence of literal chars,
not a single literal string.

That is, \Qab\E+ is the same as ab+, not (ab)+.

Fixes #11187.

Change-Id: I5406d05ccf7efff3a7f15395bdb0cfb2bd23a8ed
Reviewed-on: https://go-review.googlesource.com/17233
Reviewed-by: David Crawshaw <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
rsc authored and bradfitz committed Dec 1, 2015
1 parent 5fc5838 commit 0680e9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/regexp/syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,14 @@ func Parse(s string, flags Flags) (*Regexp, error) {
lit = t[2:i]
t = t[i+2:]
}
p.push(literalRegexp(lit, p.flags))
for lit != "" {
c, rest, err := nextRune(lit)
if err != nil {
return nil, err
}
p.literal(c)
lit = rest
}
break BigSwitch
case 'z':
p.op(OpEndText)
Expand Down
2 changes: 2 additions & 0 deletions src/regexp/syntax/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ var parseTests = []parseTest{
// Test Perl quoted literals
{`\Q+|*?{[\E`, `str{+|*?{[}`},
{`\Q+\E+`, `plus{lit{+}}`},
{`\Qab\E+`, `cat{lit{a}plus{lit{b}}}`},
{`\Q\\E`, `lit{\}`},
{`\Q\\\E`, `str{\\}`},

Expand Down Expand Up @@ -479,6 +480,7 @@ var invalidRegexps = []string{
`a{100000}`,
`a{100000,}`,
"((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})",
`\Q\E*`,
}

var onlyPerl = []string{
Expand Down

0 comments on commit 0680e9c

Please sign in to comment.