mirror of
https://github.com/golang/go
synced 2024-11-21 22:14:41 -07:00
exp/regexp: add MustCompilePOSIX
R=r CC=golang-dev https://golang.org/cl/4962060
This commit is contained in:
parent
21e671dee6
commit
66b3fabf17
@ -351,7 +351,7 @@ func TestFowler(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var notab = MustCompile(`[^\t]+`)
|
||||
var notab = MustCompilePOSIX(`[^\t]+`)
|
||||
|
||||
func testFowler(t *testing.T, file string) {
|
||||
f, err := os.Open(file)
|
||||
|
@ -58,6 +58,7 @@ import (
|
||||
"exp/regexp/syntax"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"utf8"
|
||||
@ -195,11 +196,29 @@ func (re *Regexp) put(z *machine) {
|
||||
func MustCompile(str string) *Regexp {
|
||||
regexp, error := Compile(str)
|
||||
if error != nil {
|
||||
panic(`regexp: compiling "` + str + `": ` + error.String())
|
||||
panic(`regexp: Compile(` + quote(str) + `): ` + error.String())
|
||||
}
|
||||
return regexp
|
||||
}
|
||||
|
||||
// MustCompilePOSIX is like CompilePOSIX but panics if the expression cannot be parsed.
|
||||
// It simplifies safe initialization of global variables holding compiled regular
|
||||
// expressions.
|
||||
func MustCompilePOSIX(str string) *Regexp {
|
||||
regexp, error := CompilePOSIX(str)
|
||||
if error != nil {
|
||||
panic(`regexp: CompilePOSIX(` + quote(str) + `): ` + error.String())
|
||||
}
|
||||
return regexp
|
||||
}
|
||||
|
||||
func quote(s string) string {
|
||||
if strconv.CanBackquote(s) {
|
||||
return "`" + s + "`"
|
||||
}
|
||||
return strconv.Quote(s)
|
||||
}
|
||||
|
||||
// NumSubexp returns the number of parenthesized subexpressions in this Regexp.
|
||||
func (re *Regexp) NumSubexp() int {
|
||||
return re.numSubexp
|
||||
|
Loading…
Reference in New Issue
Block a user