forked from rjkroege/edwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regx.go
62 lines (56 loc) · 1.51 KB
/
regx.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
60
61
62
package main
import (
"github.com/rjkroege/edwood/regexp"
"github.com/rjkroege/edwood/sam"
)
// AcmeRegexp is the representation of a compiled regular expression for acme.
type AcmeRegexp struct {
*regexp.Regexp
}
// rxcompile parses a regular expression and returns a regular expression object
// that can be used to match against text.
func rxcompile(r string) (*AcmeRegexp, error) {
re, err := regexp.CompileAcme(r)
if err != nil {
return nil, err
}
return &AcmeRegexp{
Regexp: re,
}, nil
}
// rxexecute searches forward in r[start:end] (from beginning of the slice to the end)
// and returns at most n matches. If r is nil, it is derived from t.
func (re *AcmeRegexp) rxexecute(t sam.Texter, r []rune, start int, end int, n int) []RangeSet {
if r == nil {
r = t.View(0, t.Nc())
}
return matchesToRangeSets(re.FindForward(r, start, end, n))
}
// rxbexecute derives the full rune slice r from t and searches backwards in r[:end]
// (from end of the slice to the beginning) and returns at most n matches.
func (re *AcmeRegexp) rxbexecute(t sam.Texter, end int, n int) RangeSet {
r := t.View(0, t.Nc())
matches := re.FindBackward(r, 0, end, n)
var rs RangeSet
for _, m := range matches {
rs = append(rs, Range{
q0: m[0],
q1: m[1],
})
}
return rs
}
func matchesToRangeSets(matches [][]int) []RangeSet {
var out []RangeSet
for _, m := range matches {
var rs RangeSet
for k := 0; k < len(m); k += 2 {
rs = append(rs, Range{
q0: m[k],
q1: m[k+1],
})
}
out = append(out, rs)
}
return out
}