Debian Codegraph @

internal/scan/bytes.go

raw symbols SCIP
1 // Package scan has functions for scanning byte slices.
2 package scan
3
4 import (
5 "bytes"
6 "encoding/binary"
7 )
8
9 // Bytes is a byte slice with helper methods for easier scanning.
10 type Bytes []byte
11
12 func (b *Bytes) Advance(n int) bool {
13 if n < 0 || len(*b) < n {
14 return false
15 }
16 *b = (*b)[n:]
17 return true
18 }
19
20 // TrimLWS trims whitespace from beginning of the bytes.
21 func (b *Bytes) TrimLWS() {
22 firstNonWS := 0
23 for ; firstNonWS < len(*b) && ByteIsWS((*b)[firstNonWS]); firstNonWS++ {
24 }
25
26 *b = (*b)[firstNonWS:]
27 }
28
29 // TrimRWS trims whitespace from the end of the bytes.
30 func (b *Bytes) TrimRWS() {
31 lb := len(*b)
32 for lb > 0 && ByteIsWS((*b)[lb-1]) {
33 *b = (*b)[:lb-1]
34 lb--
35 }
36 }
37
38 // FirstNonWS returns the first non-whitespace character from b,
39 // or 0x00 if no such character is found.
40 func (b Bytes) FirstNonWS() byte {
41 for i := range b {
42 if ByteIsWS(b[i]) {
43 continue
44 }
45 return b[i]
46 }
47
48 return 0x00
49 }
50
51 // Peek one byte from b or 0x00 if b is empty.
52 func (b *Bytes) Peek() byte {
53 if len(*b) > 0 {
54 return (*b)[0]
55 }
56 return 0
57 }
58
59 // Pop one byte from b or 0x00 if b is empty.
60 func (b *Bytes) Pop() byte {
61 if len(*b) > 0 {
62 ret := (*b)[0]
63 *b = (*b)[1:]
64 return ret
65 }
66 return 0
67 }
68
69 // PopN pops n bytes from b or nil if b is empty.
70 func (b *Bytes) PopN(n int) []byte {
71 if len(*b) >= n {
72 ret := (*b)[:n]
73 *b = (*b)[n:]
74 return ret
75 }
76 return nil
77 }
78
79 // PopUntil will advance b until, but not including, the first occurrence of stopAt
80 // character. If no occurrence is found, then it will advance until the end of b.
81 // The returned Bytes is a slice of all the bytes that we're advanced over.
82 func (b *Bytes) PopUntil(stopAt ...byte) Bytes {
83 if len(*b) == 0 {
84 return Bytes{}
85 }
86 i := bytes.IndexAny(*b, string(stopAt))
87 if i == -1 {
88 i = len(*b)
89 }
90
91 prefix := (*b)[:i]
92 *b = (*b)[i:]
93 return prefix
94 }
95
96 // ReadSlice is the same as PopUntil, but the returned value includes stopAt as well.
97 func (b *Bytes) ReadSlice(stopAt byte) Bytes {
98 if len(*b) == 0 {
99 return Bytes{}
100 }
101 i := bytes.IndexByte(*b, stopAt)
102 if i == -1 {
103 i = len(*b)
104 } else {
105 i++
106 }
107
108 prefix := (*b)[:i]
109 *b = (*b)[i:]
110 return prefix
111 }
112
113 // Line returns the first line from b and advances b with the length of the
114 // line. One new line character is trimmed after the line if it exists.
115 func (b *Bytes) Line() Bytes {
116 line := b.PopUntil('\n')
117 lline := len(line)
118 if lline > 0 && line[lline-1] == '\r' {
119 line = line[:lline-1]
120 }
121 b.Advance(1)
122 return line
123 }
124
125 func (b *Bytes) Uint16() (uint16, bool) {
126 if len(*b) < 2 {
127 return 0, false
128 }
129 v := binary.LittleEndian.Uint16(*b)
130 *b = (*b)[2:]
131 return v, true
132 }
133
134 func (b *Bytes) Uint32() (uint32, bool) {
135 if len(*b) < 4 {
136 return 0, false
137 }
138 v := binary.LittleEndian.Uint32(*b)
139 *b = (*b)[4:]
140 return v, true
141 }
142
143 func (b *Bytes) Uint32be() (uint32, bool) {
144 if len(*b) < 4 {
145 return 0, false
146 }
147 v := binary.BigEndian.Uint32(*b)
148 *b = (*b)[4:]
149 return v, true
150 }
151
152 type Flags int
153
154 const (
155 // CompactWS will make one whitespace from pattern to match one or more spaces from input.
156 CompactWS Flags = 1 << iota
157 // IgnoreCase will match lower case from pattern with lower case from input.
158 // IgnoreCase will match upper case from pattern with both lower and upper case from input.
159 // This flag is not really well named,
160 IgnoreCase
161 // FullWord ensures the input ends with a full word (it's followed by spaces.)
162 FullWord
163 )
164
165 // Search for occurrences of pattern p inside b at any index.
166 // It returns the index where p was found in b and how many bytes were needed
167 // for matching the pattern.
168 func (b Bytes) Search(p []byte, flags Flags) (i int, l int) {
169 lb, lp := len(b), len(p)
170 if lp == 0 {
171 return 0, 0
172 }
173 if lb == 0 {
174 return -1, 0
175 }
176 if flags == 0 {
177 if i = bytes.Index(b, p); i == -1 {
178 return -1, 0
179 } else {
180 return i, lp
181 }
182 }
183
184 for i := range b {
185 if lb-i < lp {
186 return -1, 0
187 }
188 if l = b[i:].Match(p, flags); l != -1 {
189 return i, l
190 }
191 }
192
193 return -1, 0
194 }
195
196 // Match returns how many bytes were needed to match pattern p.
197 // It returns -1 if p does not match b.
198 func (b Bytes) Match(p []byte, flags Flags) int {
199 l := len(b)
200 if len(p) == 0 {
201 return 0
202 }
203 if l == 0 {
204 return -1
205 }
206 // Some cases we can handle with a simple bytes.HasPrefix.
207 if flags == 0 || flags == FullWord {
208 if bytes.HasPrefix(b, p) {
209 b = b[len(p):]
210 p = p[len(p):]
211 goto out
212 }
213 return -1
214 }
215 for len(b) > 0 {
216 // If we finished all we were looking for from p.
217 if len(p) == 0 {
218 goto out
219 }
220 if flags&IgnoreCase > 0 && isUpper(p[0]) {
221 if upper(b[0]) != p[0] {
222 return -1
223 }
224 b, p = b[1:], p[1:]
225 } else if flags&CompactWS > 0 && ByteIsWS(p[0]) {
226 p = p[1:]
227 if !ByteIsWS(b[0]) {
228 return -1
229 }
230 b = b[1:]
231 if len(p) > 0 && !ByteIsWS(p[0]) {
232 b.TrimLWS()
233 }
234 } else {
235 if b[0] != p[0] {
236 return -1
237 }
238 b, p = b[1:], p[1:]
239 }
240 }
241 out:
242 // If p still has leftover characters, it means it didn't fully match b.
243 if len(p) > 0 {
244 return -1
245 }
246 if flags&FullWord > 0 {
247 if len(b) > 0 && !ByteIsWS(b[0]) {
248 return -1
249 }
250 }
251 return l - len(b)
252 }
253
254 func isUpper(c byte) bool {
255 return c >= 'A' && c <= 'Z'
256 }
257 func upper(c byte) byte {
258 if c >= 'a' && c <= 'z' {
259 return c - ('a' - 'A')
260 }
261 return c
262 }
263
264 func ByteIsWS(b byte) bool {
265 return b == '\t' || b == '\n' || b == '\x0c' || b == '\r' || b == ' '
266 }
267
268 var (
269 ASCIISpaces = []byte{' ', '\r', '\n', '\x0c', '\t'}
270 ASCIIDigits = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
271 )