-
Notifications
You must be signed in to change notification settings - Fork 46
/
byte_string.go
203 lines (166 loc) · 3.99 KB
/
byte_string.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package longbits
import (
"io"
"math/bits"
"strings"
)
const EmptyByteString = ByteString("")
func NewByteStringOf(s string) ByteString {
return ByteString(s)
}
func NewByteString(v []byte) ByteString {
return ByteString(v)
}
func NewZeroByteString(len int) ByteString {
return ByteString(make([]byte, len))
}
func NewFillByteString(len int, fill byte) ByteString {
if fill == 0 {
return NewZeroByteString(len)
}
return ByteString(strings.Repeat(string([]byte{fill}), len))
}
var _ FoldableReader = EmptyByteString.AsReader()
type ByteString string
// TODO check behavior with nil/zero strings
func (v ByteString) AsReader() FoldableReader {
return &v
}
func (v *ByteString) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write([]byte(*v))
return int64(n), err
}
func (v *ByteString) Read(p []byte) (n int, err error) {
return copy(p, *v), nil
}
func (v *ByteString) AsBytes() []byte {
return ([]byte)(*v)
}
func (v *ByteString) AsByteString() ByteString {
return *v
}
func (v *ByteString) FixedByteSize() int {
return len(*v)
}
func (v *ByteString) FoldToUint64() uint64 {
folded := v.FoldToBits64()
return folded.FoldToUint64()
}
func (v ByteString) BitMasked(index int) (value byte, mask uint8) {
bytePos, bitPos := v.BitPos(index)
mask = 1 << bitPos
return v[bytePos] & mask, mask
}
func (v ByteString) BitBool(index int) bool {
if b, _ := v.BitMasked(index); b != 0 {
return true
}
return false
}
func (v ByteString) BitByte(index int) byte {
if b, _ := v.BitMasked(index); b != 0 {
return 1
}
return 0
}
func (v ByteString) Byte(index int) byte {
return v[index]
}
func (v ByteString) BitPos(index int) (bytePos int, bitPos uint8) {
bytePos, bitPos = BitPos(index)
if bytePos >= len(v) {
panic("out of bounds")
}
return bytePos, bitPos
}
func (v ByteString) BitLen() int {
return len(v) << 3
}
func (v ByteString) SearchBit(startAt int, bit bool) int {
if startAt < 0 {
panic("illegal value")
}
if startAt>>3 >= len(v) {
return -1
}
if bit {
return v.searchBit1(startAt)
}
return v.searchBit0(startAt)
}
func (v ByteString) searchBit1(startAt int) int {
bytePos := startAt >> 3
bitPos := byte(startAt & 0x7)
b := v[bytePos] >> bitPos
if b != 0 {
return bytePos<<3 + int(bitPos) + bits.TrailingZeros8(b)
}
for bytePos++; bytePos < len(v); bytePos++ {
b := v[bytePos]
if b != 0 {
return bytePos<<3 + bits.TrailingZeros8(b)
}
}
return -1
}
func (v ByteString) searchBit0(startAt int) int {
bytePos := startAt >> 3
bitPos := byte(startAt & 0x7)
b := (^v[bytePos]) >> bitPos
if b != 0 {
return bytePos<<3 + int(bitPos) + bits.TrailingZeros8(b)
}
for bytePos++; bytePos < len(v); bytePos++ {
b := v[bytePos]
if b != 0xFF {
return bytePos<<3 + bits.TrailingZeros8(^b)
}
}
return -1
}
func BitPos(index int) (bytePos int, bitPos uint8) {
if index < 0 {
panic("illegal value")
}
if index == 0 {
return 0, 0
}
return index >> 3, uint8(index & 0x07)
}
func (v ByteString) FoldToBits64() Bits64 {
var folded Bits64
if len(v) == 0 {
return folded
}
alignedLen := len(v) & (len(folded) - 1)
copy(folded[alignedLen:], v)
for i := 0; i < alignedLen; i += len(folded) {
folded[0] ^= v[i+0]
folded[1] ^= v[i+1]
folded[2] ^= v[i+2]
folded[3] ^= v[i+3]
folded[4] ^= v[i+4]
folded[5] ^= v[i+5]
folded[6] ^= v[i+6]
folded[7] ^= v[i+7]
}
return folded
}
func (v ByteString) String() string {
return bitsToStringDefault(&v)
}