Commit 9033141f authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

gofmt

parent 3855a29d
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 ""
} }
...@@ -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}
} }
......
...@@ -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 {
......
...@@ -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
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment