Documentation ¶
Overview ¶
Package util provides common utility functions.
Index ¶
- Variables
- func Bytes2Hex(d []byte) string
- func BytesToUint32(i []byte) uint32
- func BytesToUint32BE(buf []byte) uint32
- func BytesToUint64(i []byte) uint64
- func CopyBytes(b []byte) (copiedBytes []byte)
- func Decode(input string) ([]byte, error)
- func DecodeBig(input string) (*big.Int, error)
- func DecodeUint64(input string) (uint64, error)
- func Encode(b []byte) string
- func EncodeBig(bigint *big.Int) string
- func EncodeUint64(i uint64) string
- func FromHex(s string) []byte
- func Hex2Bytes(str string) []byte
- func LeftPadBytes(slice []byte, l int) []byte
- func Min(a, b int) int
- func Min32(a, b uint32) uint32
- func Min64(a, b uint64) uint64
- func MustDecode(input string) []byte
- func MustDecodeBig(input string) *big.Int
- func MustDecodeUint64(input string) uint64
- func Uint32ToBytes(i uint32) []byte
- func Uint32ToBytesBE(i uint32) []byte
- func Uint64ToBytes(i uint64) []byte
- func Uint64ToBytesBigEndian(i uint64) []byte
- func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error
- func UnmarshalFixedText(typname string, input, out []byte) error
- func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error
- type Big
- type Bytes
- type Closer
- type Uint
- type Uint64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyString = &decError{"empty hex string"} ErrSyntax = &decError{"invalid hex string"} ErrMissingPrefix = &decError{"hex string without 0x prefix"} ErrOddLength = &decError{"hex string of odd length"} ErrEmptyNumber = &decError{"hex string 0x"} ErrLeadingZero = &decError{"hex number with leading zero digits"} ErrUint64Range = &decError{"hex number > 64 bits"} ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)} ErrBig256Range = &decError{"hex number > 256 bits"} )
Errors.
Functions ¶
func BytesToUint32 ¶
BytesToUint32 interprets a byte slice as uint32, using little endian encoding.
func BytesToUint32BE ¶
BytesToUint32BE decode buf as uint32. Panics if length is not 4 bytes.
func BytesToUint64 ¶
BytesToUint64 interprets a byte slice as uint64, using little endian encoding.
func DecodeBig ¶
DecodeBig decodes a hex string with 0x prefix as a quantity. Numbers larger than 256 bits are not accepted.
func DecodeUint64 ¶
DecodeUint64 decodes a hex string with 0x prefix as a quantity.
func EncodeBig ¶
EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.
func EncodeUint64 ¶
EncodeUint64 encodes i as a hex string with 0x prefix.
func FromHex ¶
FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".
func Hex2Bytes ¶
Hex2Bytes returns the bytes represented by the hexadecimal string str. Note that str should not be "0x" prefixed. To decode a "0x" prefixed string use FromHex.
func LeftPadBytes ¶
LeftPadBytes zero-pads slice to the left up to length l.
func MustDecode ¶
MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
func MustDecodeBig ¶
MustDecodeBig decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func MustDecodeUint64 ¶
MustDecodeUint64 decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func Uint32ToBytes ¶
Uint32ToBytes returns the byte representation of a uint32, using little endian encoding.
func Uint32ToBytesBE ¶
Uint32ToBytesBE marshal uint32 to bytes in big endian format.
func Uint64ToBytes ¶
Uint64ToBytes returns the byte representation of a uint64, using little endian encoding.
func Uint64ToBytesBigEndian ¶
Uint64ToBytesBigEndian returns the byte representation of a uint64, using big endian encoding (which is not the default for spacemesh).
func UnmarshalFixedJSON ¶
UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalJSON method for fixed-size types.
func UnmarshalFixedText ¶
UnmarshalFixedText decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
Example ¶
package main import ( "encoding/json" "fmt" ) type MyType [5]byte func (v *MyType) UnmarshalText(input []byte) error { return UnmarshalFixedText("MyType", input, v[:]) } func (v MyType) String() string { return Bytes(v[:]).String() } func main() { var v1, v2 MyType fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1)) fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2)) fmt.Println("v2:", v2) }
Output: v1 error: hex string has length 2, want 10 for MyType v2 error: <nil> v2: 0x0101010101
func UnmarshalFixedUnprefixedText ¶
UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
Types ¶
type Big ¶
Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.
func (Big) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Big) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Big) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Bytes ¶
type Bytes []byte
Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".
func (Bytes) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Bytes) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Closer ¶
type Closer struct {
// contains filtered or unexported fields
}
Closer adds the ability to close objects.
func (*Closer) Close ¶
func (closer *Closer) Close()
Close signals all listening instances to close. Note: should be called only once.
func (*Closer) CloseChannel ¶
func (closer *Closer) CloseChannel() chan struct{}
CloseChannel returns the channel to wait on for close signal.
type Uint ¶
type Uint uint
Uint marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Uint64 ¶
type Uint64 uint64
Uint64 marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint64) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint64) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.