1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 17:39:54 +02:00

Shorten Message.String() output

This commit is contained in:
Jesse
2019-12-26 11:21:14 -05:00
parent f2768fefd6
commit 5201cfc0b4
2 changed files with 24 additions and 27 deletions

View File

@@ -116,38 +116,35 @@ func (m *Message) String() string {
return "KeepAlive" return "KeepAlive"
} }
var idName string
switch m.ID { switch m.ID {
case MsgChoke: case MsgChoke:
idName = "Choke" return "Choke"
case MsgUnchoke: case MsgUnchoke:
idName = "Unchoke" return "Unchoke"
case MsgInterested: case MsgInterested:
idName = "Interested" return "Interested"
case MsgNotInterested: case MsgNotInterested:
idName = "NotInterested" return "NotInterested"
case MsgHave: case MsgHave:
idName = "Have" return "Have"
case MsgBitfield: case MsgBitfield:
idName = "Bitfield" return "Bitfield"
case MsgRequest: case MsgRequest:
idName = "Request" return "Request"
case MsgPiece: case MsgPiece:
idName = "Piece" return "Piece"
case MsgCancel: case MsgCancel:
idName = "Cancel" return "Cancel"
case MsgPort: case MsgPort:
idName = "Port" return "Port"
default: default:
idName = fmt.Sprintf("Unknown#%d", m.ID) return fmt.Sprintf("Unknown#%d", m.ID)
} }
return fmt.Sprintf("%s\t[% x]", idName, m.Payload)
} }
// HasPiece tells if a bitfield has a particular index set // HasPiece tells if a bitfield has a particular index set
func (b Bitfield) HasPiece(index int) bool { func (bf Bitfield) HasPiece(index int) bool {
byteIndex := index / 8 byteIndex := index / 8
offset := index % 8 offset := index % 8
return b[byteIndex]>>(7-offset)&1 > 0 return bf[byteIndex]>>(7-offset)&1 != 0
} }

View File

@@ -196,17 +196,17 @@ func TestString(t *testing.T) {
output string output string
}{ }{
{nil, "KeepAlive"}, {nil, "KeepAlive"},
{&Message{MsgChoke, []byte{1, 2, 3}}, "Choke\t[01 02 03]"}, {&Message{MsgChoke, []byte{1, 2, 3}}, "Choke"},
{&Message{MsgUnchoke, []byte{1, 2, 3}}, "Unchoke\t[01 02 03]"}, {&Message{MsgUnchoke, []byte{1, 2, 3}}, "Unchoke"},
{&Message{MsgInterested, []byte{1, 2, 3}}, "Interested\t[01 02 03]"}, {&Message{MsgInterested, []byte{1, 2, 3}}, "Interested"},
{&Message{MsgNotInterested, []byte{1, 2, 3}}, "NotInterested\t[01 02 03]"}, {&Message{MsgNotInterested, []byte{1, 2, 3}}, "NotInterested"},
{&Message{MsgHave, []byte{1, 2, 3}}, "Have\t[01 02 03]"}, {&Message{MsgHave, []byte{1, 2, 3}}, "Have"},
{&Message{MsgBitfield, []byte{1, 2, 3}}, "Bitfield\t[01 02 03]"}, {&Message{MsgBitfield, []byte{1, 2, 3}}, "Bitfield"},
{&Message{MsgRequest, []byte{1, 2, 3}}, "Request\t[01 02 03]"}, {&Message{MsgRequest, []byte{1, 2, 3}}, "Request"},
{&Message{MsgPiece, []byte{1, 2, 3}}, "Piece\t[01 02 03]"}, {&Message{MsgPiece, []byte{1, 2, 3}}, "Piece"},
{&Message{MsgCancel, []byte{1, 2, 3}}, "Cancel\t[01 02 03]"}, {&Message{MsgCancel, []byte{1, 2, 3}}, "Cancel"},
{&Message{MsgPort, []byte{1, 2, 3}}, "Port\t[01 02 03]"}, {&Message{MsgPort, []byte{1, 2, 3}}, "Port"},
{&Message{99, []byte{1, 2, 3}}, "Unknown#99\t[01 02 03]"}, {&Message{99, []byte{1, 2, 3}}, "Unknown#99"},
} }
for _, test := range tests { for _, test := range tests {