Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
0523e08a
Unverified
Commit
0523e08a
authored
Nov 29, 2018
by
Steven Allen
Committed by
GitHub
Nov 29, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #88 from bigs/bug/varintsize
Fix bug in VarintSize
parents
ec8630b6
11c75668
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
2 deletions
+31
-2
varint.go
varint.go
+8
-2
varint_test.go
varint_test.go
+23
-0
No files found.
varint.go
View file @
0523e08a
...
...
@@ -8,12 +8,18 @@ import (
// VarintSize returns the size (in bytes) of `num` encoded as a varint.
func
VarintSize
(
num
int
)
int
{
return
bits
.
Len
(
uint
(
num
))
/
7
+
1
bits
:=
bits
.
Len
(
uint
(
num
))
q
,
r
:=
bits
/
7
,
bits
%
7
size
:=
q
if
r
>
0
||
size
==
0
{
size
++
}
return
size
}
// CodeToVarint converts an integer to a varint-encoded []byte
func
CodeToVarint
(
num
int
)
[]
byte
{
buf
:=
make
([]
byte
,
bits
.
Len
(
uint
(
num
))
/
7
+
1
)
buf
:=
make
([]
byte
,
VarintSize
(
num
))
n
:=
binary
.
PutUvarint
(
buf
,
uint64
(
num
))
return
buf
[
:
n
]
}
...
...
varint_test.go
0 → 100644
View file @
0523e08a
package
multiaddr
import
(
"encoding/binary"
"testing"
)
func
checkVarint
(
t
*
testing
.
T
,
x
int
)
{
buf
:=
make
([]
byte
,
binary
.
MaxVarintLen64
)
expected
:=
binary
.
PutUvarint
(
buf
,
uint64
(
x
))
size
:=
VarintSize
(
x
)
if
size
!=
expected
{
t
.
Fatalf
(
"expected varintsize of %d to be %d, got %d"
,
x
,
expected
,
size
)
}
}
func
TestVarintSize
(
t
*
testing
.
T
)
{
max
:=
1
<<
16
for
x
:=
0
;
x
<
max
;
x
++
{
checkVarint
(
t
,
x
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment