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 // DropLastLine drops the last incomplete line from b.
126 //
127 // mimetype limits itself to ReadLimit bytes when performing a detection.
128 // This means, for file formats like CSV for NDJSON, the last line of the input
129 // can be an incomplete line.
130 // If b length is less than readLimit, it means we received an incomplete file
131 // and proceed with dropping the last line.
132 func (b *Bytes) DropLastLine(readLimit uint32) {
133 if readLimit == 0 || uint64(len(*b)) < uint64(readLimit) {
134 return
135 }
136
137 for i := len(*b) - 1; i > 0; i-- {
138 if (*b)[i] == '\n' {
139 *b = (*b)[:i]
140 return
141 }
142 }
143 }
144
145 func (b *Bytes) Uint16() (uint16, bool) {
146 if len(*b) < 2 {
147 return 0, false
148 }
149 v := binary.LittleEndian.Uint16(*b)
150 *b = (*b)[2:]
151 return v, true
152 }
153
154 type Flags int
155
156 const (
157 // CompactWS will make one whitespace from pattern to match one or more spaces from input.
158 CompactWS Flags = 1 << iota
159 // IgnoreCase will match lower case from pattern with lower case from input.
160 // IgnoreCase will match upper case from pattern with both lower and upper case from input.
161 // This flag is not really well named,
162 IgnoreCase
163 // FullWord ensures the input ends with a full word (it's followed by spaces.)
164 FullWord
165 )
166
167 // Search for occurrences of pattern p inside b at any index.
168 // It returns the index where p was found in b and how many bytes were needed
169 // for matching the pattern.
170 func (b Bytes) Search(p []byte, flags Flags) (i int, l int) {
171 lb, lp := len(b), len(p)
172 if lp == 0 {
173 return 0, 0
174 }
175 if lb == 0 {
176 return -1, 0
177 }
178 if flags == 0 {
179 if i = bytes.Index(b, p); i == -1 {
180 return -1, 0
181 } else {
182 return i, lp
183 }
184 }
185
186 for i := range b {
187 if lb-i < lp {
188 return -1, 0
189 }
190 if l = b[i:].Match(p, flags); l != -1 {
191 return i, l
192 }
193 }
194
195 return -1, 0
196 }
197
198 // Match returns how many bytes were needed to match pattern p.
199 // It returns -1 if p does not match b.
200 func (b Bytes) Match(p []byte, flags Flags) int {
201 l := len(b)
202 if len(p) == 0 {
203 return 0
204 }
205 if l == 0 {
206 return -1
207 }
208 // If no flags, or scanning for full word at the end of pattern then
209 // do a fast HasPrefix check.
210 // For other flags it's not possible to use HasPrefix.
211 if flags == 0 || flags&FullWord > 0 {
212 if bytes.HasPrefix(b, p) {
213 b = b[len(p):]
214 p = p[len(p):]
215 goto out
216 }
217 return -1
218 }
219 for len(b) > 0 {
220 // If we finished all we were looking for from p.
221 if len(p) == 0 {
222 goto out
223 }
224 if flags&IgnoreCase > 0 && isUpper(p[0]) {
225 if upper(b[0]) != p[0] {
226 return -1
227 }
228 b, p = b[1:], p[1:]
229 } else if flags&CompactWS > 0 && ByteIsWS(p[0]) {
230 p = p[1:]
231 if !ByteIsWS(b[0]) {
232 return -1
233 }
234 b = b[1:]
235 if !ByteIsWS(p[0]) {
236 b.TrimLWS()
237 }
238 } else {
239 if b[0] != p[0] {
240 return -1
241 }
242 b, p = b[1:], p[1:]
243 }
244 }
245 out:
246 // If p still has leftover characters, it means it didn't fully match b.
247 if len(p) > 0 {
248 return -1
249 }
250 if flags&FullWord > 0 {
251 if len(b) > 0 && !ByteIsWS(b[0]) {
252 return -1
253 }
254 }
255 return l - len(b)
256 }
257
258 func isUpper(c byte) bool {
259 return c >= 'A' && c <= 'Z'
260 }
261 func upper(c byte) byte {
262 if c >= 'a' && c <= 'z' {
263 return c - ('a' - 'A')
264 }
265 return c
266 }
267
268 func ByteIsWS(b byte) bool {
269 return b == '\t' || b == '\n' || b == '\x0c' || b == '\r' || b == ' '
270 }
271
272 var (
273 ASCIISpaces = []byte{' ', '\r', '\n', '\x0c', '\t'}
274 ASCIIDigits = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
275 )