mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
be45ba712b
commit
5bd4094d2e
3 changed files with 34 additions and 1 deletions
|
|
@ -599,6 +599,11 @@ Loop:
|
|||
re.prefix = string(b)
|
||||
}
|
||||
|
||||
// Expr returns the source text used to compile the regular expression.
|
||||
func (re *Regexp) Expr() string {
|
||||
return re.expr
|
||||
}
|
||||
|
||||
// Compile parses a regular expression and returns, if successful, a Regexp
|
||||
// object that can be used to match against text.
|
||||
func Compile(str string) (regexp *Regexp, error os.Error) {
|
||||
|
|
@ -998,6 +1003,18 @@ 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue