regexp: change Expr() to String(); add HasOperator method to Regexp.

It reports whether a regular expression has operators
as opposed to matching literal text.

R=rsc, gri
CC=golang-dev
https://golang.org/cl/3731041
This commit is contained in:
Rob Pike 2010-12-17 10:23:46 -08:00
parent 8ec6f7cd11
commit a9e7c9381e
3 changed files with 42 additions and 31 deletions

View file

@ -599,8 +599,8 @@ Loop:
re.prefix = string(b)
}
// Expr returns the source text used to compile the regular expression.
func (re *Regexp) Expr() string {
// String returns the source text used to compile the regular expression.
func (re *Regexp) String() string {
return re.expr
}
@ -849,6 +849,24 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
return final.match.m
}
// LiteralPrefix returns a literal string that must begin any match
// of the regular expression re. It returns the boolean true if the
// literal string comprises the entire regular expression.
func (re *Regexp) LiteralPrefix() (prefix string, complete bool) {
c := make([]int, len(re.inst)-2) // minus start and end.
// First instruction is start; skip that.
i := 0
for inst := re.inst[0].next; inst.kind != iEnd; inst = inst.next {
// stop if this is not a char
if inst.kind != iChar {
return string(c[:i]), false
}
c[i] = inst.char
i++
}
return string(c[:i]), true
}
// MatchString returns whether the Regexp matches the string s.
// The return value is a boolean: true for match, false for no match.
func (re *Regexp) MatchString(s string) bool { return len(re.doExecute(s, nil, 0)) > 0 }
@ -1003,18 +1021,6 @@ func QuoteMeta(s string) string {
return string(b[0:j])
}
// HasMeta returns a boolean indicating whether the string contains
// any regular expression metacharacters.
func HasMeta(s string) bool {
// A byte loop is correct because all metacharacters are ASCII.
for i := 0; i < len(s); i++ {
if special(int(s[i])) {
return true
}
}
return false
}
// Find matches in slice b if b is non-nil, otherwise find matches in string s.
func (re *Regexp) allMatches(s string, b []byte, n int, deliver func([]int)) {
var end int