mirror of https://github.com/golang/go.git
regexp: allow escaping of any punctuation
More in line with other regexp packages and egrep; accommodates overzealous escapers. R=r CC=golang-dev https://golang.org/cl/1008041
This commit is contained in:
parent
43409ed2c6
commit
6f33f34bbc
|
|
@ -28,6 +28,7 @@ var good_re = []string{
|
||||||
`[abc]`,
|
`[abc]`,
|
||||||
`[^1234]`,
|
`[^1234]`,
|
||||||
`[^\n]`,
|
`[^\n]`,
|
||||||
|
`\!\\`,
|
||||||
}
|
}
|
||||||
|
|
||||||
type stringError struct {
|
type stringError struct {
|
||||||
|
|
@ -100,6 +101,14 @@ var matches = []tester{
|
||||||
// fixed bugs
|
// fixed bugs
|
||||||
tester{`ab$`, "cab", vec{1, 3}},
|
tester{`ab$`, "cab", vec{1, 3}},
|
||||||
tester{`axxb$`, "axxcb", vec{}},
|
tester{`axxb$`, "axxcb", vec{}},
|
||||||
|
|
||||||
|
// can backslash-escape any punctuation
|
||||||
|
tester{`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,
|
||||||
|
`!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, vec{0, 31}},
|
||||||
|
tester{`[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`,
|
||||||
|
`!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, vec{0, 31}},
|
||||||
|
tester{"\\`", "`", vec{0, 1}},
|
||||||
|
tester{"[\\`]+", "`", vec{0, 1}},
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileTest(t *testing.T, expr string, error os.Error) *Regexp {
|
func compileTest(t *testing.T, expr string, error os.Error) *Regexp {
|
||||||
|
|
|
||||||
|
|
@ -298,8 +298,8 @@ func special(c int) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func specialcclass(c int) bool {
|
func ispunct(c int) bool {
|
||||||
for _, r := range `\-[]` {
|
for _, r := range "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" {
|
||||||
if c == r {
|
if c == r {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -344,7 +344,7 @@ func (p *parser) charClass() instr {
|
||||||
p.error(ErrExtraneousBackslash)
|
p.error(ErrExtraneousBackslash)
|
||||||
case c == 'n':
|
case c == 'n':
|
||||||
c = '\n'
|
c = '\n'
|
||||||
case specialcclass(c):
|
case ispunct(c):
|
||||||
// c is as delivered
|
// c is as delivered
|
||||||
default:
|
default:
|
||||||
p.error(ErrBadBackslash)
|
p.error(ErrBadBackslash)
|
||||||
|
|
@ -439,7 +439,7 @@ func (p *parser) term() (start, end instr) {
|
||||||
p.error(ErrExtraneousBackslash)
|
p.error(ErrExtraneousBackslash)
|
||||||
case c == 'n':
|
case c == 'n':
|
||||||
c = '\n'
|
c = '\n'
|
||||||
case special(c):
|
case ispunct(c):
|
||||||
// c is as delivered
|
// c is as delivered
|
||||||
default:
|
default:
|
||||||
p.error(ErrBadBackslash)
|
p.error(ErrBadBackslash)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue