hex

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: AGPL-3.0 Imports: 8 Imported by: 1

Documentation

Overview

Package hex provides hexadecimal encoding and decoding functions.

For better performance, all functions in this package are unsafe for concurrency unless otherwise specified.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendEncode added in v0.12.2

func AppendEncode[Bytes constraints.ByteString](
	dst []byte,
	src Bytes,
	upper bool,
) []byte

AppendEncode appends the hexadecimal representation of src to dst and returns the extended byte slice.

upper indicates whether to use uppercase in hexadecimal representation.

AppendEncode[[]byte](dst, src, false) is equivalent to AppendEncode(dst, src) in official package encoding/hex.

func CanEncodeTo added in v0.6.0

func CanEncodeTo[Bytes1, Bytes2 constraints.ByteString](
	src Bytes1,
	x Bytes2,
) bool

CanEncodeTo reports whether src can be encoded to the hexadecimal representation x.

It performs better than the comparison after encoding.

func CanEncodeToPrefix added in v0.6.0

func CanEncodeToPrefix[Bytes1, Bytes2 constraints.ByteString](
	src Bytes1,
	prefix Bytes2,
) bool

CanEncodeToPrefix reports whether src can be encoded to the hexadecimal representation that has the specified prefix.

It performs better than the comparison after encoding.

func DecodedLen

func DecodedLen[Int constraints.Integer](x Int) Int

DecodedLen returns the length of decoding of x source bytes, exactly x / 2.

func Encode

func Encode[Bytes constraints.ByteString](
	dst []byte,
	src Bytes,
	upper bool,
) int

Encode encodes src in hexadecimal representation to dst.

It panics if dst doesn't have enough space to hold the encoding result. The client should guarantee that len(dst) >= EncodedLen(len(src)).

upper indicates whether to use uppercase in hexadecimal representation.

It returns the number of bytes written into dst, exactly EncodedLen(len(src)).

Encode[[]byte](dst, src, false) is equivalent to Encode(dst, src) in official package encoding/hex.

func EncodeInt64 added in v0.2.0

func EncodeInt64(dst []byte, x int64, upper bool, digits int) int

EncodeInt64 encodes x in hexadecimal representation to dst.

upper indicates whether to use uppercase in hexadecimal representation. digits specify the minimum length of the output content (excluding the negative sign "-"). It pads with leading zeros after the sign (if any) if the length is not enough. If digits is non-positive, no padding is applied.

It returns the number of bytes written into dst.

It panics if dst is too small to keep the result. To ensure that dst has enough space to keep the result, its length should be at least EncodeInt64DstLen(digits).

func EncodeInt64DstLen added in v0.2.0

func EncodeInt64DstLen(digits int) int

EncodeInt64DstLen returns a safe length of dst used in EncodeInt64 to ensure that dst has enough space to keep the encoding result.

digits is the parameter used in EncodeInt64 to specify the minimum length of the output content (excluding the negative sign "-").

func EncodeInt64To added in v0.2.0

func EncodeInt64To(w io.Writer, x int64, upper bool, digits int) (
	written int, err error)

EncodeInt64To encodes x in hexadecimal representation to w.

upper indicates whether to use uppercase in hexadecimal representation. digits specify the minimum length of the output content (excluding the negative sign "-"). It pads with leading zeros after the sign (if any) if the length is not enough. If digits is non-positive, no padding is applied.

It returns the number of bytes written to w, and any write error encountered.

func EncodeInt64ToString added in v0.2.0

func EncodeInt64ToString(x int64, upper bool, digits int) string

EncodeInt64ToString returns hexadecimal representation of integer x.

upper indicates whether to use uppercase in hexadecimal representation. digits specify the minimum length of the return string (excluding the negative sign "-"). It pads with leading zeros after the sign (if any) if the length is not enough. If digits is non-positive, no padding is applied.

func EncodeToString

func EncodeToString[Bytes constraints.ByteString](
	src Bytes,
	upper bool,
) string

EncodeToString returns hexadecimal encoding of src.

upper indicates whether to use uppercase in hexadecimal representation.

EncodeToString[[]byte](src, false) is equivalent to EncodeToString(src) in official package encoding/hex.

func EncodedLen

func EncodedLen[Int constraints.Integer](n Int) Int

EncodedLen returns the length of encoding of n source bytes, exactly n * 2.

Types

type Encoder

type Encoder interface {
	io.Writer
	io.ByteWriter
	io.StringWriter
	io.ReaderFrom

	// EncodeDst returns the destination writer of this encoder.
	EncodeDst() io.Writer
}

Encoder is a device to write hexadecimal encoding of input data to the destination writer.

It combines io.Writer, io.ByteWriter, io.StringWriter, and io.ReaderFrom. All the methods write hexadecimal encoding of input data to the destination writer.

func NewEncoder

func NewEncoder(w io.Writer, upper bool) Encoder

NewEncoder creates an encoder to write hexadecimal characters to w.

upper indicates whether to use uppercase in hexadecimal representation.

It panics if w is nil.

Jump to

Keyboard shortcuts

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