-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
56 lines (49 loc) · 851 Bytes
/
reader.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
package regex
type Reader struct {
runes []rune
cur int
}
func NewReader(source string) *Reader {
return &Reader{runes: []rune(source)}
}
func (r *Reader) Peek() rune {
if r.cur == len(r.runes) {
return 0
}
return r.runes[r.cur]
}
func (r *Reader) Next() rune {
if r.cur == len(r.runes) {
return 0
}
ret := r.runes[r.cur]
r.cur++
return ret
}
func (r *Reader) hasNext() bool {
return r.cur < len(r.runes)
}
func (r *Reader) getSubRegex(other *Reader) string {
cntParam := 1
regex := ""
for other.hasNext() {
ch := other.Next()
if ch == '(' {
cntParam++
} else if ch == ')' {
cntParam--
if cntParam == 0 {
break
}
}
regex += string(ch)
}
return regex
}
func (r *Reader) getRemainRegex(other *Reader) string {
regex := ""
for other.hasNext() {
regex += string(other.Next())
}
return regex
}