| 1 |
|
| 2 |
package scan |
| 3 |
|
| 4 |
import ( |
| 5 |
"bytes" |
| 6 |
"encoding/binary" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 39 |
|
| 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 |
|
| 52 |
func (b *Bytes) Peek() byte { |
| 53 |
if len(*b) > 0 { |
| 54 |
return (*b)[0] |
| 55 |
} |
| 56 |
return 0 |
| 57 |
} |
| 58 |
|
| 59 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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 |
|
| 114 |
|
| 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 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 158 |
CompactWS Flags = 1 << iota |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
IgnoreCase |
| 163 |
|
| 164 |
FullWord |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 199 |
|
| 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 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 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 |
|
| 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 |
) |