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
package
multiaddr
import
(
import
(
"fmt"
"strings"
"encoding/binary"
"encoding/binary"
"fmt"
"net"
"net"
"strconv"
"strconv"
"strings"
)
)
func
StringToBytes
(
s
string
)
([]
byte
,
error
)
{
func
StringToBytes
(
s
string
)
([]
byte
,
error
)
{
b
:=
[]
byte
{}
b
:=
[]
byte
{}
sp
:=
strings
.
Split
(
s
,
"/"
)
sp
:=
strings
.
Split
(
s
,
"/"
)
...
@@ -16,7 +15,7 @@ func StringToBytes(s string) ([]byte, error) {
...
@@ -16,7 +15,7 @@ func StringToBytes(s string) ([]byte, error) {
// consume first empty elem
// consume first empty elem
sp
=
sp
[
1
:
]
sp
=
sp
[
1
:
]
for
;
len
(
sp
)
>
0
;
{
for
len
(
sp
)
>
0
{
p
:=
ProtocolWithName
(
sp
[
0
])
p
:=
ProtocolWithName
(
sp
[
0
])
if
p
==
nil
{
if
p
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no protocol with name %s"
,
sp
[
0
])
return
nil
,
fmt
.
Errorf
(
"no protocol with name %s"
,
sp
[
0
])
...
@@ -42,7 +41,7 @@ func BytesToString(b []byte) (ret string, err error) {
...
@@ -42,7 +41,7 @@ func BytesToString(b []byte) (ret string, err error) {
s
:=
""
s
:=
""
for
;
len
(
b
)
>
0
;
{
for
len
(
b
)
>
0
{
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
if
p
==
nil
{
if
p
==
nil
{
return
""
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
return
""
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
...
@@ -50,7 +49,7 @@ func BytesToString(b []byte) (ret string, err error) {
...
@@ -50,7 +49,7 @@ func BytesToString(b []byte) (ret string, err error) {
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
p
.
Name
},
""
)
b
=
b
[
1
:
]
b
=
b
[
1
:
]
a
:=
AddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)])
a
:=
AddressBytesToString
(
p
,
b
[
:
(
p
.
Size
/
8
)])
if
len
(
a
)
>
0
{
if
len
(
a
)
>
0
{
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
a
},
""
)
s
=
strings
.
Join
([]
string
{
s
,
"/"
,
a
},
""
)
}
}
...
@@ -95,4 +94,3 @@ func AddressBytesToString(p *Protocol, b []byte) string {
...
@@ -95,4 +94,3 @@ func AddressBytesToString(p *Protocol, b []byte) string {
return
""
return
""
}
}
index.go
View file @
9033141f
...
@@ -33,19 +33,19 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
...
@@ -33,19 +33,19 @@ func (m *Multiaddr) Protocols() (ret []*Protocol, err error) {
ps
:=
[]
*
Protocol
{}
ps
:=
[]
*
Protocol
{}
b
:=
m
.
Bytes
[
:
]
b
:=
m
.
Bytes
[
:
]
for
;
len
(
b
)
>
0
;
{
for
len
(
b
)
>
0
{
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
p
:=
ProtocolWithCode
(
int
(
b
[
0
]))
if
p
==
nil
{
if
p
==
nil
{
return
nil
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
return
nil
,
fmt
.
Errorf
(
"no protocol with code %d"
,
b
[
0
])
}
}
ps
=
append
(
ps
,
p
)
ps
=
append
(
ps
,
p
)
b
=
b
[
1
+
(
p
.
Size
/
8
)
:
]
b
=
b
[
1
+
(
p
.
Size
/
8
)
:
]
}
}
return
ps
,
nil
return
ps
,
nil
}
}
func
(
m
*
Multiaddr
)
Encapsulate
(
o
*
Multiaddr
)
*
Multiaddr
{
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
...
)
b
=
append
(
m
.
Bytes
,
o
.
Bytes
...
)
return
&
Multiaddr
{
Bytes
:
b
}
return
&
Multiaddr
{
Bytes
:
b
}
}
}
...
...
multiaddr_test.go
View file @
9033141f
...
@@ -2,11 +2,10 @@ package multiaddr
...
@@ -2,11 +2,10 @@ package multiaddr
import
(
import
(
"bytes"
"bytes"
"testing"
"encoding/hex"
"encoding/hex"
"testing"
)
)
func
TestStringToBytes
(
t
*
testing
.
T
)
{
func
TestStringToBytes
(
t
*
testing
.
T
)
{
testString
:=
func
(
s
string
,
h
string
)
{
testString
:=
func
(
s
string
,
h
string
)
{
...
@@ -49,7 +48,6 @@ func TestBytesToString(t *testing.T) {
...
@@ -49,7 +48,6 @@ func TestBytesToString(t *testing.T) {
testString
(
"/ip4/127.0.0.1/udp/1234"
,
"047f0000011104d2"
)
testString
(
"/ip4/127.0.0.1/udp/1234"
,
"047f0000011104d2"
)
}
}
func
TestProtocols
(
t
*
testing
.
T
)
{
func
TestProtocols
(
t
*
testing
.
T
)
{
m
,
err
:=
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
m
,
err
:=
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
protocols.go
View file @
9033141f
...
@@ -24,7 +24,7 @@ var Protocols = []*Protocol{
...
@@ -24,7 +24,7 @@ var Protocols = []*Protocol{
}
}
func
ProtocolWithName
(
s
string
)
*
Protocol
{
func
ProtocolWithName
(
s
string
)
*
Protocol
{
for
_
,
p
:=
range
(
Protocols
)
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Name
==
s
{
if
p
.
Name
==
s
{
return
p
return
p
}
}
...
@@ -33,7 +33,7 @@ func ProtocolWithName(s string) *Protocol {
...
@@ -33,7 +33,7 @@ func ProtocolWithName(s string) *Protocol {
}
}
func
ProtocolWithCode
(
c
int
)
*
Protocol
{
func
ProtocolWithCode
(
c
int
)
*
Protocol
{
for
_
,
p
:=
range
(
Protocols
)
{
for
_
,
p
:=
range
Protocols
{
if
p
.
Code
==
c
{
if
p
.
Code
==
c
{
return
p
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