-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupid.go
63 lines (51 loc) · 1.45 KB
/
groupid.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package expr
import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
)
// groupID represents a group ID. Layout, in bytes:
// - 2: an int16 size of the expression group,
// - 1: optimization flag, for optimizing "!=" in string matching
// - 5: random ID for group
type groupID [8]byte
// type internedGroupID unique.Handle[groupID]
//
// func (i internedGroupID) Value() groupID {
// return unique.Handle[groupID](i).Value()
// }
//
// func (i internedGroupID) Size() uint16 {
// // Uses unsafe pointers to access the underlying groupID
// // to return the size without a copy.
// handlePtr := unsafe.Pointer(&i)
// unsafe.Slice(
// // return (*groupID)(unsafe.Pointer(unsafe.SliceData(([8]byte)(handlePtr)))).Size()
// }
var rander = rand.Read
type RandomReader func(p []byte) (n int, err error)
const (
OptimizeNone = 0x0
)
func (g groupID) String() string {
return hex.EncodeToString(g[:])
}
func (g groupID) Size() uint16 {
return binary.NativeEndian.Uint16(g[0:2])
}
func (g groupID) Flag() byte {
return g[2]
}
func newGroupID(size uint16, optimizeFlag byte) groupID {
return newGroupIDWithReader(size, optimizeFlag, rander)
}
func newGroupIDWithReader(size uint16, optimizeFlag byte, rander RandomReader) groupID {
id := make([]byte, 8)
binary.NativeEndian.PutUint16(id, size)
// Set the optimize byte.
id[2] = optimizeFlag
_, _ = rander(id[3:])
gid := groupID([8]byte(id[0:8]))
// interned := internedGroupID(unique.Make(gid))
return gid
}