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 ¶
- func CanEncode(src, x []byte) bool
- func CanEncodeToString(src []byte, x string) bool
- func DecodedLen(x int) int
- func DecodedLen64(x int64) int64
- func DumpTo(w io.Writer, src []byte, cfg *DumpConfig) (n int, err error)
- func DumpToString(src []byte, cfg *DumpConfig) string
- func Encode(dst, src []byte, upper bool) int
- func EncodeInt64(dst []byte, x int64, upper bool, digits int) int
- func EncodeInt64DstLen(digits int) int
- func EncodeInt64To(w io.Writer, x int64, upper bool, digits int) (written int, err error)
- func EncodeInt64ToString(x int64, upper bool, digits int) string
- func EncodeToString(src []byte, upper bool) string
- func EncodedLen(n int) int
- func EncodedLen64(n int64) int64
- func Format(dst, src []byte, cfg *FormatConfig) int
- func FormatTo(w io.Writer, src []byte, cfg *FormatConfig) (n int, err error)
- func FormatToString(src []byte, cfg *FormatConfig) string
- func FormattedLen(n int, cfg *FormatConfig) int
- func FormattedLen64(n int64, cfg *FormatConfig) int64
- func ParsedLen(x int, cfg *FormatConfig) int
- func ParsedLen64(x int64, cfg *FormatConfig) int64
- type DumpConfig
- type Dumper
- type Encoder
- type FormatConfig
- type Formatter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanEncode ¶ added in v0.2.0
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
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 ¶
DecodedLen returns the length of decoding of x source bytes, exactly x / 2.
func DecodedLen64 ¶
DecodedLen64 returns the length of decoding of x source bytes, exactly x / 2.
func DumpTo ¶
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 ¶
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
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
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
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
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 ¶
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 ¶
EncodedLen returns the length of encoding of n source bytes, exactly n * 2.
func EncodedLen64 ¶
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 ¶
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.
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.
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.