-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunes.go
118 lines (106 loc) · 2.32 KB
/
runes.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package regexer
import (
"bytes"
"io"
"iter"
"regexp"
"unicode/utf8"
)
type Runes struct {
rex *regexp.Regexp
src []rune
}
type runeReader struct {
inner []rune
consumed int
}
func (rr *runeReader) ReadRune() (rune, int, error) {
if rr.consumed >= len(rr.inner) {
return 0, 0, io.EOF
}
r := rr.inner[rr.consumed]
rr.consumed += 1
return r, utf8.RuneLen(r), nil
}
func (b Runes) Find() iter.Seq[RMatch] {
return func(yield func(RMatch) bool) {
shift := 0
for {
subSrc := b.src[shift:]
reader := runeReader{inner: subSrc}
spans := b.rex.FindReaderSubmatchIndex(&reader)
if spans == nil {
return
}
spanStart := spans[0]
spanEnd := spans[1]
matchSrc := subSrc[spanStart:spanEnd]
match := RMatch{
Content: matchSrc,
Span: Span{
Start: shift + spanStart,
End: shift + spanEnd,
},
Subs: RSubs{
shift: shift,
content: matchSrc,
rawSpans: spans,
},
}
more := yield(match)
if !more {
return
}
shift += spanEnd
}
}
}
func (b Runes) Replace(res *[]rune) iter.Seq[RReplacement] {
return func(yield func(RReplacement) bool) {
prevEnd := 0
for match := range b.Find() {
*res = append(*res, b.src[prevEnd:match.Span.Start]...)
ok := yield(RReplacement{
RMatch: match,
rex: b.rex,
src: b.src[prevEnd:],
result: res,
})
if !ok {
return
}
prevEnd = match.Span.End
}
*res = append(*res, b.src[prevEnd:]...)
}
}
func (b Runes) Contains() bool {
reader := runeReader{inner: b.src}
return b.rex.MatchReader(&reader)
}
type RReplacement struct {
RMatch
rex *regexp.Regexp
src []rune
result *[]rune
}
func (r RReplacement) ReplaceLiteral(val []rune) {
*r.result = append(*r.result, val...)
}
func (r RReplacement) ReplaceTemplate(val []rune) {
// TODO: decrease the number of type conversions.
suffix := r.rex.Expand(nil, runes2bytes(val), runes2bytes(r.src), r.Subs.rawSpans)
*r.result = append(*r.result, bytes.Runes(suffix)...)
}
func (r RReplacement) ReplaceFunc(f func([]rune) []rune) {
*r.result = append(*r.result, f(r.RMatch.Content)...)
}
func runes2bytes(runes []rune) []byte {
bytes := make([]byte, 0, len(runes))
for _, r := range runes {
bytes = append(bytes, 0, 0, 0, 0)
n := utf8.EncodeRune(bytes[len(bytes)-4:], r)
bytes = bytes[:len(bytes)-4+n]
}
return bytes
}