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
91752fd5
Commit
91752fd5
authored
May 04, 2016
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a bit more cleanup, use a bytes.Buffer instead of appending bytes
parent
9e13209d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
7 deletions
+13
-7
codec.go
codec.go
+13
-7
No files found.
codec.go
View file @
91752fd5
package
multiaddr
package
multiaddr
import
(
import
(
"bytes"
"encoding/base32"
"encoding/base32"
"encoding/binary"
"encoding/binary"
"fmt"
"fmt"
...
@@ -16,7 +17,7 @@ func stringToBytes(s string) ([]byte, error) {
...
@@ -16,7 +17,7 @@ func stringToBytes(s string) ([]byte, error) {
// consume trailing slashes
// consume trailing slashes
s
=
strings
.
TrimRight
(
s
,
"/"
)
s
=
strings
.
TrimRight
(
s
,
"/"
)
b
:=
[]
byte
{}
b
:=
new
(
bytes
.
Buffer
)
sp
:=
strings
.
Split
(
s
,
"/"
)
sp
:=
strings
.
Split
(
s
,
"/"
)
if
sp
[
0
]
!=
""
{
if
sp
[
0
]
!=
""
{
...
@@ -31,7 +32,7 @@ func stringToBytes(s string) ([]byte, error) {
...
@@ -31,7 +32,7 @@ func stringToBytes(s string) ([]byte, error) {
if
p
.
Code
==
0
{
if
p
.
Code
==
0
{
return
nil
,
fmt
.
Errorf
(
"no protocol with name %s"
,
sp
[
0
])
return
nil
,
fmt
.
Errorf
(
"no protocol with name %s"
,
sp
[
0
])
}
}
b
=
append
(
b
,
CodeToVarint
(
p
.
Code
)
...
)
b
.
Write
(
CodeToVarint
(
p
.
Code
))
sp
=
sp
[
1
:
]
sp
=
sp
[
1
:
]
if
p
.
Size
==
0
{
// no length.
if
p
.
Size
==
0
{
// no length.
...
@@ -41,18 +42,19 @@ func stringToBytes(s string) ([]byte, error) {
...
@@ -41,18 +42,19 @@ func stringToBytes(s string) ([]byte, error) {
if
len
(
sp
)
<
1
{
if
len
(
sp
)
<
1
{
return
nil
,
fmt
.
Errorf
(
"protocol requires address, none given: %s"
,
p
.
Name
)
return
nil
,
fmt
.
Errorf
(
"protocol requires address, none given: %s"
,
p
.
Name
)
}
}
a
,
err
:=
addressStringToBytes
(
p
,
sp
[
0
])
a
,
err
:=
addressStringToBytes
(
p
,
sp
[
0
])
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to parse %s: %s %s"
,
p
.
Name
,
sp
[
0
],
err
)
return
nil
,
fmt
.
Errorf
(
"failed to parse %s: %s %s"
,
p
.
Name
,
sp
[
0
],
err
)
}
}
b
=
append
(
b
,
a
...
)
b
.
Write
(
a
)
sp
=
sp
[
1
:
]
sp
=
sp
[
1
:
]
}
}
return
b
,
nil
return
b
.
Bytes
(),
nil
}
}
func
validateBytes
(
b
[]
byte
)
(
err
error
)
{
func
validateBytes
(
b
[]
byte
)
(
err
error
)
{
// panic handler, in case we try accessing bytes incorrectly.
for
len
(
b
)
>
0
{
for
len
(
b
)
>
0
{
code
,
n
,
err
:=
ReadVarintCode
(
b
)
code
,
n
,
err
:=
ReadVarintCode
(
b
)
b
=
b
[
n
:
]
b
=
b
[
n
:
]
...
@@ -104,6 +106,10 @@ func bytesToString(b []byte) (ret string, err error) {
...
@@ -104,6 +106,10 @@ func bytesToString(b []byte) (ret string, err error) {
return
""
,
err
return
""
,
err
}
}
if
len
(
b
)
<
size
||
size
<
0
{
return
""
,
fmt
.
Errorf
(
"invalid value for size"
)
}
a
,
err
:=
addressBytesToString
(
p
,
b
[
:
size
])
a
,
err
:=
addressBytesToString
(
p
,
b
[
:
size
])
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
...
@@ -265,7 +271,7 @@ func addressBytesToString(p Protocol, b []byte) (string, error) {
...
@@ -265,7 +271,7 @@ func addressBytesToString(p Protocol, b []byte) (string, error) {
return
""
,
err
return
""
,
err
}
}
return
m
.
B58String
(),
nil
return
m
.
B58String
(),
nil
}
default
:
return
""
,
fmt
.
Errorf
(
"unknown protocol"
)
return
""
,
fmt
.
Errorf
(
"unknown protocol"
)
}
}
}
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