bencode

package module
v0.0.0-...-3c4df4f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 9 Imported by: 0

README

bencode

a Go implementation of Bencode.

bep_0003.rst | bep_0003.md(中文)

Usage

import "code.gopub.tech/bencode"

var str = bencode.String("spam")
var data = bencode.Encode(str)   // []byte(`4:spam`)
val, err := bencode.Decode(data) // val==str, err==nil

var i = bencode.Integer(42)
var list = bencode.List{
    bencode.String("item1"),
    bencode.Integer(2),
    bencode.List{},
    bencode.Dict{},
}
var dict = bencode.Dict{
    bencode.String("key-must-be-string"): bencode.String("value can be any bencode.Value"),
}

Implement

Encode
func Encode(v Value) []byte {
	return v.Encode()
}

type Value interface {
	Encode() []byte
}

type String string
type Integer int64
type List []Value
type Dict map[String]Value

Decode
func Decode(input []byte) (Value, error) {
    ...
}

Documentation

Index

Constants

View Source
const (
	StrLenEnd = ':' // 字节串 ${len}:${str} // len 是字节长度
	IntStart  = 'i' // 整数 i${value}e. 不能有前导 0.  -0 非法.
	ListStart = 'l' // 列表 l[${item}]*e
	DictStart = 'd' // 字典 d[${strKey}${value}]*e
	End       = 'e' // 整数, 列表和字典的结尾标记
)

Variables

This section is empty.

Functions

func AsInt

func AsInt(v Value) int64

func AsStr

func AsStr(v Value) string

func Encode

func Encode(v Value) []byte

Encode 对值进行 bencode 编码

func IsUTF8

func IsUTF8(s String) bool

Types

type Dict

type Dict map[String]Value

Dict 字典

func AsDict

func AsDict(v Value) Dict

func (Dict) Encode

func (d Dict) Encode() []byte

func (Dict) Range

func (d Dict) Range(fn func(index int, key String, value Value))

func (Dict) String

func (d Dict) String() string

type Integer

type Integer int64

Integer 整数

func AsInteger

func AsInteger(v Value) Integer

func (Integer) Encode

func (i Integer) Encode() []byte

func (Integer) String

func (i Integer) String() string

type List

type List []Value

List 列表

func AsList

func AsList(v Value) List

func (List) Encode

func (l List) Encode() []byte

func (List) String

func (l List) String() string

type String

type String string

String 字节串

func AsString

func AsString(v Value) String

func (String) Encode

func (s String) Encode() []byte

func (String) String

func (s String) String() string

type Value

type Value interface {
	fmt.Stringer // for print purpose
	Encode() []byte
}

Value 接口表示支持 bencode 编码的值

func Decode

func Decode(input []byte) (Value, error)

Decode 从字节数组中解析出表示的值

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL