Repository navigation
Expand file tree
/
Copy pathbyteptr.go
More file actions
149 lines (124 loc) · 3.11 KB
/
Copy pathbyteptr.go
File metadata and controls
149 lines (124 loc) · 3.11 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package vector
import (
"unsafe"
"github.com/koykov/bitset"
"github.com/koykov/byteconv"
"github.com/koykov/entry"
"github.com/koykov/indirect"
)
const byteptrSize = 32
type Byteptr struct {
addr, vptr uintptr
bits bitset.Bitset32
offset, len, cap uint32
}
func (p *Byteptr) TakeAddr(s []byte) *Byteptr {
if s == nil {
return p
}
h := (*byteconv.SliceHeader)(unsafe.Pointer(&s))
p.addr, p.cap = h.Data, uint32(h.Cap)
return p
}
func (p *Byteptr) TakeStringAddr(s string) *Byteptr {
if len(s) == 0 {
return p
}
h := (*byteconv.StringHeader)(unsafe.Pointer(&s))
p.addr, p.cap = h.Data, uint32(h.Len)
return p
}
// DEPRECATED: use TakeStringAddr instead.
func (p *Byteptr) TakeStrAddr(s string) *Byteptr {
return p.TakeStringAddr(s)
}
func (p *Byteptr) Init(s []byte, offset, len int) *Byteptr {
return p.TakeAddr(s).SetOffset(offset).SetLen(len)
}
func (p *Byteptr) InitRaw(addr uintptr, offset, len int) *Byteptr {
p.addr = addr
p.offset = uint32(offset)
p.len = uint32(len)
return p
}
func (p *Byteptr) InitString(s string, offset, len int) *Byteptr {
return p.TakeStringAddr(s).SetOffset(offset).SetLen(len)
}
// DEPRECATED: use InitString instead.
func (p *Byteptr) InitStr(s string, offset, len int) *Byteptr {
return p.InitString(s, offset, len)
}
func (p *Byteptr) SetAddr(addr uintptr, cap int) *Byteptr {
p.addr, p.cap = addr, uint32(cap)
return p
}
func (p *Byteptr) SetOffset(offset int) *Byteptr {
p.offset = uint32(offset)
return p
}
func (p *Byteptr) SetLen(len int) *Byteptr {
p.len = uint32(len)
return p
}
func (p *Byteptr) Offset() int { return int(p.offset) }
func (p *Byteptr) Len() int { return int(p.len) }
func (p *Byteptr) Bytes() []byte {
if vec := p.indirectVector(); vec != nil && vec.codec_ != nil {
b, _ := vec.codec_.Decode(p)
return b
}
return p.RawBytes()
}
func (p *Byteptr) String() string {
if vec := p.indirectVector(); vec != nil && vec.codec_ != nil {
b, _ := vec.codec_.Decode(p)
return byteconv.B2S(b)
}
return p.RawString()
}
func (p *Byteptr) RawBytes() []byte {
if p.addr == 0 || p.len == 0 {
return nil
}
h := byteconv.SliceHeader{
Data: p.addr + uintptr(p.offset),
Len: int(p.len),
Cap: int(p.len),
}
return *(*[]byte)(unsafe.Pointer(&h))
}
func (p *Byteptr) RawString() string {
if p.addr == 0 || p.len == 0 {
return ""
}
h := byteconv.StringHeader{
Data: p.addr + uintptr(p.offset),
Len: int(p.len),
}
return *(*string)(unsafe.Pointer(&h))
}
func (p *Byteptr) SetBit(pos int, value bool) { p.bits.SetBit(pos, value) }
func (p *Byteptr) CheckBit(pos int) bool { return p.bits.CheckBit(pos) }
func (p *Byteptr) Reset() {
p.addr = 0
p.vptr = 0
p.bits.Reset()
p.offset = 0
p.len = 0
p.cap = 0
}
// Restore the entire object from the unsafe pointer.
//
// This needs to reduce pointers count and avoids redundant GC checks.
func (p *Byteptr) indirectVector() *Vector {
if p.vptr == 0 {
return nil
}
return (*Vector)(indirect.ToUnsafePtr(p.vptr))
}
// Check if p equals to substring of s described by e.
func (p *Byteptr) equalKE(s string, e entry.Entry64) bool {
lo, hi := e.Decode()
ks := s[lo:hi]
return ks == p.String()
}