Commit 66b3fabf authored by Russ Cox's avatar Russ Cox

exp/regexp: add MustCompilePOSIX

R=r
CC=golang-dev
https://golang.org/cl/4962060
parent 21e671de
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment