Newer
Older
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
package tokenizer
import (
"reflect"
"runtime"
"unsafe"
)
// b2s converts byte slice to a string without memory allocation.
// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ .
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// s2b converts string to a byte slice without memory allocation.
//
// Note it may break if string and/or slice header will change
// in the future go versions.
func s2b(s string) (b []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
runtime.KeepAlive(&s)
return b
}
func isNumberByte(b byte) bool {
return '0' <= b && b <= '9'
}
func bytesStarts(prefix []byte, b []byte) bool {
if len(prefix) > len(b) {
return false
}
return b2s(prefix) == b2s(b[0:len(prefix)])
}
func bytesEnds(suffix []byte, b []byte) bool {
if len(suffix) > len(b) {
return false
}
return b2s(suffix) == b2s(b[len(b)-len(suffix):])
}