regexp: add HasMeta and regexp.Expr().

The former is a boolean function to test whether a string
contains a regular expression metacharacter; the second
returns the string used to compile the regexp.

R=gri, rsc
CC=golang-dev
https://golang.org/cl/3728041
This commit is contained in:
Rob Pike 2010-12-16 16:55:26 -08:00
parent be45ba712b
commit 5bd4094d2e
3 changed files with 34 additions and 1 deletions

View file

@ -269,6 +269,18 @@ func TestQuoteMeta(t *testing.T) {
}
}
func TestHasMeta(t *testing.T) {
for _, tc := range quoteMetaTests {
// HasMeta should be false if QuoteMeta returns the original string;
// true otherwise.
quoted := QuoteMeta(tc.pattern)
if HasMeta(tc.pattern) != (quoted != tc.pattern) {
t.Errorf("HasMeta(`%s`) = %t; want %t",
tc.pattern, HasMeta(tc.pattern), quoted != tc.pattern)
}
}
}
type numSubexpCase struct {
input string
expected int