This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
byteslice.go
152 lines (137 loc) · 3.87 KB
/
byteslice.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
package byteslice
import (
"errors"
"math"
)
// Reverse change the order of the byte slice.
func Reverse(data []byte) []byte {
if len(data) < 2 {
return data
}
sliceLength := len(data)
sliceHalfLength := int(float64(sliceLength / 2))
reversedSlice := make([]byte, sliceLength)
for i := 0; i <= sliceHalfLength; i++ {
reversedSlice[i] = data[sliceLength-1-i]
reversedSlice[sliceLength-1-i] = data[i]
}
return reversedSlice
}
// LShift apply left shift operation to an byte slice.
func LShift(data []byte, shift uint64) []byte {
if shift == 0 {
return data
}
dataLength := len(data)
result := make([]byte, dataLength)
if shift > maxBitsLength {
copy(result, data[1:])
result = LShift(result, shift-maxBitsLength)
} else {
for i := dataLength - 1; i >= 0; i-- {
if i > 0 {
result[i-1] = data[i] >> (maxBitsLength - shift)
}
result[i] = result[i] | (data[i] << shift)
}
}
return result
}
// RShift apply right shift operation to an byte slice.
func RShift(data []byte, shift uint64) []byte {
if shift == 0 {
return data
}
dataLength := len(data)
result := make([]byte, dataLength)
if shift > maxBitsLength {
shiftedData := append(make([]byte, 1), data[:dataLength-1]...)
result = RShift(shiftedData, shift-maxBitsLength)
} else {
for i := 0; i < dataLength; i++ {
if i < dataLength-1 {
result[i+1] = data[i] << (maxBitsLength - shift)
}
result[i] = result[i] | (data[i] >> shift)
}
}
return result
}
// LPad pads the left-side of a byte slice with a filler byte.
func LPad(data []byte, length int, filler byte) []byte {
dataLength := len(data)
if length < 1 || length <= dataLength {
return data
}
result := make([]byte, length-dataLength)
for i := range result {
result[i] = filler
}
result = append(result, data...)
return result
}
// RPad pads the right-side of a byte slice with a filler byte.
func RPad(data []byte, length int, filler byte) []byte {
dataLength := len(data)
if length < 1 || length <= dataLength {
return data
}
result := make([]byte, length-dataLength)
for i := range result {
result[i] = filler
}
result = append(data, result...)
return result
}
// Unset apply AND operation on a byte slice with an "unset" byte slice (must have the same size).
func Unset(data, unsetData []byte) ([]byte, error) {
var dataLength = len(data)
var unsetDataLength = len(unsetData)
if dataLength != unsetDataLength {
return nil, errors.New("data and unsetData must have the same size")
}
result := make([]byte, dataLength)
for i := 0; i < dataLength; i++ {
result[i] = data[i] & unsetData[i]
}
return result, nil
}
// Set apply OR operation on a byte slice with an "set" byte slice (must have the same size).
func Set(data, setData []byte) ([]byte, error) {
dataLength := len(data)
setDataLength := len(setData)
if dataLength != setDataLength {
return nil, errors.New("data and setData must have the same size")
}
result := make([]byte, dataLength)
for i := 0; i < dataLength; i++ {
result[i] = data[i] | setData[i]
}
return result, nil
}
// Toggle apply XOR operation on a byte slice with an "toggle" byte slice (must have the same size).
func Toggle(data, toggleData []byte) ([]byte, error) {
dataLength := len(data)
toggleDataLength := len(toggleData)
if dataLength != toggleDataLength {
return nil, errors.New("data and toggleData must have the same size")
}
result := make([]byte, dataLength)
for i := 0; i < dataLength; i++ {
result[i] = data[i] ^ toggleData[i]
}
return result, nil
}
// Flip apply NOT operation to a byte slice to flip it.
func Flip(data []byte) []byte {
dataLength := len(data)
result := make([]byte, dataLength)
for i := 0; i < dataLength; i++ {
result[i] = ^data[i]
}
return result
}
func computeSize(leastSignificantBit, mostSignificantBit uint64) uint64 {
count := float64(mostSignificantBit-leastSignificantBit) / float64(maxBitsLength)
return uint64(math.Ceil(count))
}