summaryrefslogtreecommitdiff
path: root/src/lib/regexp/regexp.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-01-06 13:54:53 -0800
committerRob Pike <r@golang.org>2009-01-06 13:54:53 -0800
commit5a82b4d527c321949a6d73cd408c921d4eaf6958 (patch)
tree1577c9ef1c81369916609e681e7e30c593e0540a /src/lib/regexp/regexp.go
parentcc5e0e10416a32c327ed0b5563ebc9f534157faf (diff)
downloadgolang-5a82b4d527c321949a6d73cd408c921d4eaf6958.tar.gz
A richer interface for regexps. Simple boolean matcher, a vector of strings rather than
indexes, and a global boolean function for quick action. R=rsc DELTA=152 (127 added, 12 deleted, 13 changed) OCL=22140 CL=22142
Diffstat (limited to 'src/lib/regexp/regexp.go')
-rw-r--r--src/lib/regexp/regexp.go34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/lib/regexp/regexp.go b/src/lib/regexp/regexp.go
index 218fbd601..383db09a4 100644
--- a/src/lib/regexp/regexp.go
+++ b/src/lib/regexp/regexp.go
@@ -580,9 +580,11 @@ func Compiler(str string, ch chan *RE) {
ch <- re;
}
-// Public interface has only execute functionality (not yet implemented)
+// Public interface has only execute functionality
export type Regexp interface {
- Execute(s string) []int
+ Execute(s string) []int;
+ Match(s string) bool;
+ MatchStrings(s string) []string;
}
// Compile in separate goroutine; wait for result
@@ -715,3 +717,31 @@ func (re *RE) DoExecute(str string, pos int) []int {
func (re *RE) Execute(s string) []int {
return re.DoExecute(s, 0)
}
+
+
+func (re *RE) Match(s string) bool {
+ return len(re.DoExecute(s, 0)) > 0
+}
+
+
+func (re *RE) MatchStrings(s string) []string {
+ r := re.DoExecute(s, 0);
+ if r == nil {
+ return nil
+ }
+ a := new([]string, len(r)/2);
+ for i := 0; i < len(r); i += 2 {
+ a[i/2] = s[r[i] : r[i+1]]
+ }
+ return a
+}
+
+// Exported function for simple boolean check. Anything more fancy
+// needs a call to Compile.
+export func Match(pattern string, s string) (matched bool, error *os.Error) {
+ re, err := Compile(pattern);
+ if err != nil {
+ return false, err
+ }
+ return re.Match(s), nil
+}