FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
prysm/encoding/bytesutil/bytes.go at develop · tmors/prysm · GitHub
tmors
/
prysm
Public
forked from
OffchainLabs/prysm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
prysm
/
encoding
/
bytesutil
/
bytes.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
147 lines (130 loc) · 3.63 KB
Breadcrumbs
prysm
/
encoding
/
bytesutil
/
bytes.go
Copy path
File metadata and controls
147 lines (130 loc) · 3.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Package bytesutil defines helper methods for converting integers to byte slices.
package
bytesutil
import
(
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// ToBytes48Array is a convenience method for converting an array of
// byte slices to an array of fixed-sized byte arrays.
func
ToBytes48Array
(
x
[][]
byte
) [][
48
]
byte
{
y
:=
make
([][
48
]
byte
,
len
(
x
))
for
i
:=
range
x
{
y
[
i
]
=
ToBytes48
(
x
[
i
])
}
return
y
}
// ToBool is a convenience method for converting a byte to a bool.
// This method will use the first bit of the 0 byte to generate the returned value.
func
ToBool
(
x
byte
)
bool
{
return
x
&
1
==
1
}
// FromBool is a convenience method for converting a bool to a byte.
// This method will use the first bit to generate the returned value.
func
FromBool
(
x
bool
)
byte
{
if
x
{
return
1
}
return
0
}
// FromBytes48 is a convenience method for converting a fixed-size byte array
// to a byte slice.
func
FromBytes48
(
x
[
48
]
byte
) []
byte
{
return
x
[:]
}
// FromBytes48Array is a convenience method for converting an array of
// fixed-size byte arrays to an array of byte slices.
func
FromBytes48Array
(
x
[][
48
]
byte
) [][]
byte
{
y
:=
make
([][]
byte
,
len
(
x
))
for
i
:=
range
x
{
y
[
i
]
=
x
[
i
][:]
}
return
y
}
// Trunc truncates the byte slices to 6 bytes.
func
Trunc
(
x
[]
byte
) []
byte
{
if
len
(
x
)
>
6
{
return
x
[:
6
]
}
return
x
}
// SafeCopyRootAtIndex takes a copy of an 32-byte slice in a slice of byte slices. Returns error if index out of range.
func
SafeCopyRootAtIndex
(
input
[][]
byte
,
idx
uint64
) ([]
byte
,
error
) {
if
input
==
nil
{
return
nil
,
nil
}
if
uint64
(
len
(
input
))
<=
idx
{
return
nil
,
fmt
.
Errorf
(
"index %d out of range"
,
idx
)
}
item
:=
make
([]
byte
,
32
)
copy
(
item
,
input
[
idx
])
return
item
,
nil
}
// SafeCopyBytes will copy and return a non-nil byte slice, otherwise it returns nil.
func
SafeCopyBytes
(
cp
[]
byte
) []
byte
{
if
cp
!=
nil
{
if
len
(
cp
)
==
32
{
copied
:=
[
32
]
byte
(
cp
)
return
copied
[:]
}
copied
:=
make
([]
byte
,
len
(
cp
))
copy
(
copied
,
cp
)
return
copied
}
return
nil
}
// SafeCopy2dBytes will copy and return a non-nil 2d byte slice, otherwise it returns nil.
func
SafeCopy2dBytes
(
ary
[][]
byte
) [][]
byte
{
if
ary
!=
nil
{
copied
:=
make
([][]
byte
,
len
(
ary
))
for
i
,
a
:=
range
ary
{
copied
[
i
]
=
SafeCopyBytes
(
a
)
}
return
copied
}
return
nil
}
// SafeCopy2d32Bytes will copy and return a non-nil 2d byte slice, otherwise it returns nil.
func
SafeCopy2d32Bytes
(
ary
[][
32
]
byte
) [][
32
]
byte
{
if
ary
!=
nil
{
copied
:=
make
([][
32
]
byte
,
len
(
ary
))
copy
(
copied
,
ary
)
return
copied
}
return
nil
}
// SafeCopy2dHexUtilBytes will copy and return a non-nil 2d hex util byte slice, otherwise it returns nil.
func
SafeCopy2dHexUtilBytes
(
ary
[]hexutil.
Bytes
) [][]
byte
{
if
ary
!=
nil
{
copied
:=
make
([][]
byte
,
len
(
ary
))
for
i
,
a
:=
range
ary
{
copied
[
i
]
=
SafeCopyBytes
(
a
)
}
return
copied
}
return
nil
}
// ReverseBytes32Slice will reverse the provided slice's order.
func
ReverseBytes32Slice
(
arr
[][
32
]
byte
) [][
32
]
byte
{
for
i
,
j
:=
0
,
len
(
arr
)
-
1
;
i
<
j
;
i
,
j
=
i
+
1
,
j
-
1
{
arr
[
i
],
arr
[
j
]
=
arr
[
j
],
arr
[
i
]
}
return
arr
}
// PadTo pads a byte slice to the given size. If the byte slice is larger than the given size, the
// original slice is returned.
func
PadTo
(
b
[]
byte
,
size
int
) []
byte
{
if
len
(
b
)
>=
size
{
return
b
}
return
append
(
b
,
make
([]
byte
,
size
-
len
(
b
))
...
)
}
// ReverseByteOrder Switch the endianness of a byte slice by reversing its order.
// This function does not modify the actual input bytes.
func
ReverseByteOrder
(
input
[]
byte
) []
byte
{
b
:=
make
([]
byte
,
len
(
input
))
copy
(
b
,
input
)
for
i
:=
0
;
i
<
len
(
b
)
/
2
;
i
++
{
b
[
i
],
b
[
len
(
b
)
-
i
-
1
]
=
b
[
len
(
b
)
-
i
-
1
],
b
[
i
]
}
return
b
}
Back
|
FazBrowse Home
|
New Git URL