2016-08-26 12:51:09 +08:00
|
|
|
package bencode
|
|
|
|
|
2021-11-02 14:28:05 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
2022-03-09 17:57:23 +08:00
|
|
|
"fmt"
|
2021-11-02 14:28:05 +08:00
|
|
|
)
|
|
|
|
|
2016-08-26 12:51:09 +08:00
|
|
|
type Bytes []byte
|
|
|
|
|
|
|
|
var (
|
2021-09-18 10:36:25 +08:00
|
|
|
_ Unmarshaler = (*Bytes)(nil)
|
|
|
|
_ Marshaler = (*Bytes)(nil)
|
2016-08-26 18:29:29 +08:00
|
|
|
_ Marshaler = Bytes{}
|
2016-08-26 12:51:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (me *Bytes) UnmarshalBencode(b []byte) error {
|
|
|
|
*me = append([]byte(nil), b...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:29:29 +08:00
|
|
|
func (me Bytes) MarshalBencode() ([]byte, error) {
|
2021-11-02 14:28:05 +08:00
|
|
|
if len(me) == 0 {
|
|
|
|
return nil, errors.New("marshalled Bytes should not be zero-length")
|
|
|
|
}
|
2016-08-26 18:29:29 +08:00
|
|
|
return me, nil
|
2016-08-26 12:51:09 +08:00
|
|
|
}
|
2022-03-09 17:57:23 +08:00
|
|
|
|
|
|
|
func (me Bytes) GoString() string {
|
|
|
|
return fmt.Sprintf("bencode.Bytes(%q)", []byte(me))
|
|
|
|
}
|