diff --git a/message/message.go b/message/message.go index 3f1078e..ffeccc4 100644 --- a/message/message.go +++ b/message/message.go @@ -116,38 +116,35 @@ func (m *Message) String() string { return "KeepAlive" } - var idName string switch m.ID { case MsgChoke: - idName = "Choke" + return "Choke" case MsgUnchoke: - idName = "Unchoke" + return "Unchoke" case MsgInterested: - idName = "Interested" + return "Interested" case MsgNotInterested: - idName = "NotInterested" + return "NotInterested" case MsgHave: - idName = "Have" + return "Have" case MsgBitfield: - idName = "Bitfield" + return "Bitfield" case MsgRequest: - idName = "Request" + return "Request" case MsgPiece: - idName = "Piece" + return "Piece" case MsgCancel: - idName = "Cancel" + return "Cancel" case MsgPort: - idName = "Port" + return "Port" 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 -func (b Bitfield) HasPiece(index int) bool { +func (bf Bitfield) HasPiece(index int) bool { byteIndex := index / 8 offset := index % 8 - return b[byteIndex]>>(7-offset)&1 > 0 + return bf[byteIndex]>>(7-offset)&1 != 0 } diff --git a/message/message_test.go b/message/message_test.go index f7ae0e8..4760a16 100644 --- a/message/message_test.go +++ b/message/message_test.go @@ -196,17 +196,17 @@ func TestString(t *testing.T) { output string }{ {nil, "KeepAlive"}, - {&Message{MsgChoke, []byte{1, 2, 3}}, "Choke\t[01 02 03]"}, - {&Message{MsgUnchoke, []byte{1, 2, 3}}, "Unchoke\t[01 02 03]"}, - {&Message{MsgInterested, []byte{1, 2, 3}}, "Interested\t[01 02 03]"}, - {&Message{MsgNotInterested, []byte{1, 2, 3}}, "NotInterested\t[01 02 03]"}, - {&Message{MsgHave, []byte{1, 2, 3}}, "Have\t[01 02 03]"}, - {&Message{MsgBitfield, []byte{1, 2, 3}}, "Bitfield\t[01 02 03]"}, - {&Message{MsgRequest, []byte{1, 2, 3}}, "Request\t[01 02 03]"}, - {&Message{MsgPiece, []byte{1, 2, 3}}, "Piece\t[01 02 03]"}, - {&Message{MsgCancel, []byte{1, 2, 3}}, "Cancel\t[01 02 03]"}, - {&Message{MsgPort, []byte{1, 2, 3}}, "Port\t[01 02 03]"}, - {&Message{99, []byte{1, 2, 3}}, "Unknown#99\t[01 02 03]"}, + {&Message{MsgChoke, []byte{1, 2, 3}}, "Choke"}, + {&Message{MsgUnchoke, []byte{1, 2, 3}}, "Unchoke"}, + {&Message{MsgInterested, []byte{1, 2, 3}}, "Interested"}, + {&Message{MsgNotInterested, []byte{1, 2, 3}}, "NotInterested"}, + {&Message{MsgHave, []byte{1, 2, 3}}, "Have"}, + {&Message{MsgBitfield, []byte{1, 2, 3}}, "Bitfield"}, + {&Message{MsgRequest, []byte{1, 2, 3}}, "Request"}, + {&Message{MsgPiece, []byte{1, 2, 3}}, "Piece"}, + {&Message{MsgCancel, []byte{1, 2, 3}}, "Cancel"}, + {&Message{MsgPort, []byte{1, 2, 3}}, "Port"}, + {&Message{99, []byte{1, 2, 3}}, "Unknown#99"}, } for _, test := range tests {