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
9033141f
Commit
9033141f
authored
Jul 04, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gofmt
parent
3855a29d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
225 additions
and
229 deletions
+225
-229
codec.go
codec.go
+74
-76
index.go
index.go
+43
-43
multiaddr_test.go
multiaddr_test.go
+84
-86
protocols.go
protocols.go
+24
-24
No files found.
codec.go
View file @
9033141f
package
multiaddr
import
(
"fmt"
"strings"
import
(
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
)
func
StringToBytes
(
s
string
)
([]
byte
,
error
)
{
b
:=
[]
byte
{}
sp
:=
strings
.
Split
(
s
,
"/"
)
...
...
@@ -16,7 +15,7 @@ func StringToBytes(s string) ([]byte, error) {
// consume first empty elem
sp
=
sp
[
1
:
]
for
;
len
(
sp
)
>
0
;
{
for
len
(
sp
)
>
0
{
p
:=
ProtocolWithName
(
sp
[
0
])
if
p
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no protocol with name %s"
,
sp
[
0
])
...
...
@@ -42,7 +41,7 @@ func BytesToString(b []byte) (ret string, err error) {
s
:=
""
for
;
len
(
b
)
>
0
;
{
for
len
(
b
)
>
0
{
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
if
p
==
nil
{
return
""
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
...
...
@@ -50,7 +49,7 @@ func BytesToString(b []byte) (ret string, err error) {
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
b
=
b
[
1
:
]
a
:=
AddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)])
a
:=
AddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)])
if
len
(
a
)
>
0
{
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
a
},
""
)
}
...
...
@@ -95,4 +94,3 @@ func AddressBytesToString(p *Protocol, b []byte) string {
return
""
}
index.go
View file @
9033141f
...
...
@@ -33,19 +33,19 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
ps
:=
[]
*
Protocol
{}
b
:=
m
.
Bytes
[
:
]
for
;
len
(
b
)
>
0
;
{
for
len
(
b
)
>
0
{
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
if
p
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
}
ps
=
append
(
ps
,
p
)
b
=
b
[
1
+
(
p
.
Size
/
8
)
:
]
b
=
b
[
1
+
(
p
.
Size
/
8
)
:
]
}
return
ps
,
nil
}
func
(
m
*
Multiaddr
)
Encapsulate
(
o
*
Multiaddr
)
*
Multiaddr
{
b
:=
make
([]
byte
,
len
(
m
.
Bytes
)
+
len
(
o
.
Bytes
))
b
:=
make
([]
byte
,
len
(
m
.
Bytes
)
+
len
(
o
.
Bytes
))
b
=
append
(
m
.
Bytes
,
o
.
Bytes
...
)
return
&
Multiaddr
{
Bytes
:
b
}
}
...
...
multiaddr_test.go
View file @
9033141f
...
...
@@ -2,11 +2,10 @@ package multiaddr
import
(
"bytes"
"testing"
"encoding/hex"
"testing"
)
func
TestStringToBytes
(
t
*
testing
.
T
)
{
testString
:=
func
(
s
string
,
h
string
)
{
...
...
@@ -49,7 +48,6 @@ func TestBytesToString(t *testing.T) {
testString
(
"/ip4/127.0.0.1/udp/1234"
,
"047f0000011104d2"
)
}
func
TestProtocols
(
t
*
testing
.
T
)
{
m
,
err
:=
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
...
...
protocols.go
View file @
9033141f
...
...
@@ -24,7 +24,7 @@ var Protocols = []*Protocol{
}
func
ProtocolWithName
(
s
string
)
*
Protocol
{
for
_
,
p
:=
range
(
Protocols
)
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Name
==
s
{
return
p
}
...
...
@@ -33,7 +33,7 @@ func ProtocolWithName(s string) *Protocol {
}
func
ProtocolWithCode
(
c
int
)
*
Protocol
{
for
_
,
p
:=
range
(
Protocols
)
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Code
==
c
{
return
p
}
...
...
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