...
1
2
3
4
5 package runes
6
7 import (
8 "unicode/utf8"
9
10 "golang.org/x/text/transform"
11 )
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 func If(s Set, tIn, tNotIn transform.Transformer) Transformer {
35 if tIn == nil && tNotIn == nil {
36 return Transformer{transform.Nop}
37 }
38 if tIn == nil {
39 tIn = transform.Nop
40 }
41 if tNotIn == nil {
42 tNotIn = transform.Nop
43 }
44 sIn, ok := tIn.(transform.SpanningTransformer)
45 if !ok {
46 sIn = dummySpan{tIn}
47 }
48 sNotIn, ok := tNotIn.(transform.SpanningTransformer)
49 if !ok {
50 sNotIn = dummySpan{tNotIn}
51 }
52
53 a := &cond{
54 tIn: sIn,
55 tNotIn: sNotIn,
56 f: s.Contains,
57 }
58 a.Reset()
59 return Transformer{a}
60 }
61
62 type dummySpan struct{ transform.Transformer }
63
64 func (d dummySpan) Span(src []byte, atEOF bool) (n int, err error) {
65 return 0, transform.ErrEndOfSpan
66 }
67
68 type cond struct {
69 tIn, tNotIn transform.SpanningTransformer
70 f func(rune) bool
71 check func(rune) bool
72 t transform.SpanningTransformer
73 }
74
75
76 func (t *cond) Reset() {
77 t.check = t.is
78 t.t = t.tIn
79 t.t.Reset()
80 }
81
82 func (t *cond) is(r rune) bool {
83 if t.f(r) {
84 return true
85 }
86 t.check = t.isNot
87 t.t = t.tNotIn
88 t.tNotIn.Reset()
89 return false
90 }
91
92 func (t *cond) isNot(r rune) bool {
93 if !t.f(r) {
94 return true
95 }
96 t.check = t.is
97 t.t = t.tIn
98 t.tIn.Reset()
99 return false
100 }
101
102
103
104
105
106
107 func (t *cond) Span(src []byte, atEOF bool) (n int, err error) {
108 p := 0
109 for n < len(src) && err == nil {
110
111
112 const maxChunk = 4096
113 max := len(src)
114 if v := n + maxChunk; v < max {
115 max = v
116 }
117 atEnd := false
118 size := 0
119 current := t.t
120 for ; p < max; p += size {
121 r := rune(src[p])
122 if r < utf8.RuneSelf {
123 size = 1
124 } else if r, size = utf8.DecodeRune(src[p:]); size == 1 {
125 if !atEOF && !utf8.FullRune(src[p:]) {
126 err = transform.ErrShortSrc
127 break
128 }
129 }
130 if !t.check(r) {
131
132 atEnd = true
133 break
134 }
135 }
136 n2, err2 := current.Span(src[n:p], atEnd || (atEOF && p == len(src)))
137 n += n2
138 if err2 != nil {
139 return n, err2
140 }
141
142 p = n + size
143 }
144 return n, err
145 }
146
147 func (t *cond) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
148 p := 0
149 for nSrc < len(src) && err == nil {
150
151
152
153 const maxChunk = 4096
154 max := len(src)
155 if n := nSrc + maxChunk; n < len(src) {
156 max = n
157 }
158 atEnd := false
159 size := 0
160 current := t.t
161 for ; p < max; p += size {
162 r := rune(src[p])
163 if r < utf8.RuneSelf {
164 size = 1
165 } else if r, size = utf8.DecodeRune(src[p:]); size == 1 {
166 if !atEOF && !utf8.FullRune(src[p:]) {
167 err = transform.ErrShortSrc
168 break
169 }
170 }
171 if !t.check(r) {
172
173 atEnd = true
174 break
175 }
176 }
177 nDst2, nSrc2, err2 := current.Transform(dst[nDst:], src[nSrc:p], atEnd || (atEOF && p == len(src)))
178 nDst += nDst2
179 nSrc += nSrc2
180 if err2 != nil {
181 return nDst, nSrc, err2
182 }
183
184 p = nSrc + size
185 }
186 return nDst, nSrc, err
187 }
188
View as plain text