hex

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: AGPL-3.0 Imports: 7 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 CanEncode added in v0.2.0

func CanEncode(src, x []byte) bool

CanEncode reports whether src can be encoded to the hexadecimal representation x (in type []byte).

It performs better than the comparison after encoding.

func CanEncodeToString added in v0.2.0

func CanEncodeToString(src []byte, x string) bool

CanEncodeToString reports whether src can be encoded to the hexadecimal representation x (in type string).

It performs better than the comparison after encoding.

func DecodedLen

func DecodedLen(x int) int

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

func DecodedLen64

func DecodedLen64(x int64) int64

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

func DumpTo

func DumpTo(w io.Writer, src []byte, cfg *DumpConfig) (n int, err error)

DumpTo dumps src with cfg to w.

It returns the number of bytes dumped from src, and any write error encountered.

func DumpToString added in v0.2.0

func DumpToString(src []byte, cfg *DumpConfig) string

DumpToString returns a string including a hexadecimal dump of src with cfg.

func Encode

func Encode(dst, src []byte, 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 to use uppercase in hexadecimal representation.

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

Encode(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 to use uppercase in hexadecimal representation. digits specifies 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 will be 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 to use uppercase in hexadecimal representation. digits specifies 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 will be 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 to use uppercase in hexadecimal representation. digits specifies 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 will be applied.

func EncodeToString

func EncodeToString(src []byte, upper bool) string

EncodeToString returns hexadecimal encoding of src.

upper indicates to use uppercase in hexadecimal representation.

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

func EncodedLen

func EncodedLen(n int) int

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

func EncodedLen64

func EncodedLen64(n int64) int64

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

func Format

func Format(dst, src []byte, cfg *FormatConfig) int

Format outputs hexadecimal representation of src in the format specified by cfg to dst.

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

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

Format(dst, src, nil) is equivalent to Encode(dst, src) in official package encoding/hex.

func FormatTo

func FormatTo(w io.Writer, src []byte, cfg *FormatConfig) (n int, err error)

FormatTo outputs hexadecimal representation of src in the format specified by cfg to w.

It returns the number of bytes formatted from src, and any write error encountered.

func FormatToString

func FormatToString(src []byte, cfg *FormatConfig) string

FormatToString returns hexadecimal representation of src in the format specified by cfg.

FormatToString(src, nil) is equivalent to EncodeToString(src) in official package encoding/hex.

func FormattedLen

func FormattedLen(n int, cfg *FormatConfig) int

FormattedLen returns the length of formatting n source bytes with cfg.

func FormattedLen64

func FormattedLen64(n int64, cfg *FormatConfig) int64

FormattedLen64 returns the length of formatting n source bytes with cfg.

func ParsedLen

func ParsedLen(x int, cfg *FormatConfig) int

ParsedLen returns the length of parsing with cfg of x source bytes.

func ParsedLen64

func ParsedLen64(x int64, cfg *FormatConfig) int64

ParsedLen64 returns the length of parsing with cfg of x source bytes.

Types

type DumpConfig

type DumpConfig struct {
	FormatConfig
	LineSep       string // Line separator.
	BlocksPerLine int    // The number of blocks per line, non-positive values for all blocks in one line.

	// Function to generate a prefix of one line.
	// Only valid when BlockLen > 0 and BlocksPerLine > 0.
	PrefixFn func() []byte
	// Function to generate a suffix of one line.
	// The input is the line before hexadecimal encoding.
	// Only valid when BlockLen > 0 and BlocksPerLine > 0.
	SuffixFn func(line []byte) []byte
}

DumpConfig is a configuration for hexadecimal dumping.

type Dumper

type Dumper interface {
	io.Writer
	io.ByteWriter
	io.ReaderFrom
	io.Closer
	inout.Flusher

	// DumpDst returns the destination writer of this dumper.
	// It returns nil if the dumper is closed successfully.
	DumpDst() io.Writer

	// DumpCfg returns a copy of the configuration for hexadecimal dumping
	// used by this dumper.
	DumpCfg() *DumpConfig
}

Dumper is a device to dump input data, with specified configuration, to the destination writer.

It combines io.Writer, io.ByteWriter, io.ReaderFrom. All methods dump input data to the destination writer.

It contains the method Close and the method Flush. The client should flush the dumper after use, and close it when it is no longer needed.

func NewDumper

func NewDumper(w io.Writer, cfg *DumpConfig) Dumper

NewDumper creates a dumper to dump hexadecimal characters to w. The format is specified by cfg.

It panics if w is nil.

Note that the created dumper will keep a copy of cfg, so you can't change its config after create it.

type Encoder

type Encoder interface {
	io.Writer
	io.ByteWriter
	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, 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 to use uppercase in hexadecimal representation. It panics if w is nil.

type FormatConfig

type FormatConfig struct {
	Upper bool   // Use uppercase or not.
	Sep   string // Separator.

	// Block size, i.e., the number of bytes (before hexadecimal encoding)
	// between every two separators.
	// Non-positive values for no separators.
	BlockLen int
}

FormatConfig is a configuration for hexadecimal formatting.

type Formatter

type Formatter interface {
	io.Writer
	io.ByteWriter
	io.ReaderFrom
	io.Closer
	inout.Flusher

	// FormatDst returns the destination writer of this formatter.
	// It returns nil if the formatter is closed successfully.
	FormatDst() io.Writer

	// FormatCfg returns a copy of the configuration for hexadecimal formatting
	// used by this formatter.
	FormatCfg() *FormatConfig
}

Formatter is a device to write hexadecimal representation of input data, in the specified format, to the destination writer.

It combines io.Writer, io.ByteWriter, io.ReaderFrom. All the methods format input data and output to the destination writer.

It contains the method Close and the method Flush. The client should flush the formatter after use, and close it when it is no longer needed.

func NewFormatter

func NewFormatter(w io.Writer, cfg *FormatConfig) Formatter

NewFormatter creates a formatter to write hexadecimal characters with separators to w. The format is specified by cfg.

It panics if w is nil.

Note that the created formatter will keep a copy of cfg, so you can't change its config after creating it.

Directories

Path Synopsis
Package helper provides some helper functions for github.com/donyori/gogo/encoding/hex.
Package helper provides some helper functions for github.com/donyori/gogo/encoding/hex.

Jump to

Keyboard shortcuts

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