-
Notifications
You must be signed in to change notification settings - Fork 383
/
Copy pathregexp.go
59 lines (51 loc) · 1.47 KB
/
regexp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package regexp implements regular expression search tuned for
// use in grep-like programs.
package regexp
import "regexp/syntax"
func bug() {
panic("codesearch/regexp: internal error")
}
// Regexp is the representation of a compiled regular expression.
// A Regexp is NOT SAFE for concurrent use by multiple goroutines.
type Regexp struct {
Syntax *syntax.Regexp
expr string // original expression
m matcher
}
// String returns the source text used to compile the regular expression.
func (re *Regexp) String() string {
return re.expr
}
// Compile parses a regular expression and returns, if successful,
// a Regexp object that can be used to match against lines of text.
func Compile(expr string) (*Regexp, error) {
re, err := syntax.Parse(expr, syntax.Perl)
if err != nil {
return nil, err
}
sre := re.Simplify()
prog, err := syntax.Compile(sre)
if err != nil {
return nil, err
}
if err := toByteProg(prog); err != nil {
return nil, err
}
r := &Regexp{
Syntax: re,
expr: expr,
}
if err := r.m.init(prog); err != nil {
return nil, err
}
return r, nil
}
func (r *Regexp) Match(b []byte, beginText, endText bool) (end int) {
return r.m.match(b, beginText, endText)
}
func (r *Regexp) MatchString(s string, beginText, endText bool) (end int) {
return r.m.matchString(s, beginText, endText)
}