Version: v0.0.0-...-01e8697 Latest Latest

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

Go to latest
Published: Nov 24, 2021 License: GPL-3.0 Imports: 5 Imported by: 6



Package xfmt provides addons to std fmt and strconv packages with focus on formatting text without allocations.

For example if in fmt speak you have

s := fmt.Sprintf("hello %q %d %x", "world", 1, []byte("data"))

xfmt analog would be

buf := xfmt.Buffer{}
buf .S("hello ") .Q("world") .C(' ') .D(1) .C(' ') .Xb([]byte("data"))
s := buf.Bytes()

xfmt.Buffer can be reused several times via Buffer.Reset() .



This section is empty.


This section is empty.


func Append

func Append(b []byte, x Stringer) []byte

Append appends to b formatted x.

NOTE sadly since x is interface it makes real value substituted to it

escape to heap (not so a problem since usually they already are) but then also
if x has non-pointer receiver convT2I _allocates_ memory for the value copy.

-> always pass to append &object, even if object has non-pointer XFmtString receiver.

func AppendHex

func AppendHex(b []byte, x []byte) []byte

AppendHex appends to b hex representation of x.

func AppendHex016

func AppendHex016(b []byte, x uint64) []byte

AppendHex016 appends to b x formatted 16-character hex string.

func AppendQuotePy

func AppendQuotePy(buf []byte, s string) []byte

AppendQuotePy appends to buf Python quoting of s.

func AppendQuotePyBytes

func AppendQuotePyBytes(buf, b []byte) []byte

AppendQuotePyBytes appends to buf Python quoting of b.

func AppendRune

func AppendRune(b []byte, r rune) []byte

AppendRune appends to b UTF-8 encoding of r.


type Buffer

type Buffer []byte

Buffer provides syntactic sugar for formatting mimicking fmt.Printf style.

XXX combine with bytes.Buffer ?

func (Buffer) Bytes

func (b Buffer) Bytes() []byte

Bytes returns buffer storage as []byte.

func (*Buffer) C

func (b *Buffer) C(r rune) *Buffer

C appends rune formatted by %c.

func (*Buffer) Cb

func (b *Buffer) Cb(c byte) *Buffer

Cb appends byte formatted by %c.

func (*Buffer) D

func (b *Buffer) D(i int) *Buffer

D appends int formatted by %d.

func (*Buffer) D64

func (b *Buffer) D64(i int64) *Buffer

D64 appends int64 formatted by %d.

func (*Buffer) Q

func (b *Buffer) Q(s string) *Buffer

Q appends string formatted by %q.

func (*Buffer) Qb

func (b *Buffer) Qb(s []byte) *Buffer

Qb appends []byte formatted by %q.

func (*Buffer) Qc

func (b *Buffer) Qc(c rune) *Buffer

Qc appends rune formatted by %q.

func (*Buffer) Qcb

func (b *Buffer) Qcb(c byte) *Buffer

Qcb appends byte formatted by %q.

func (*Buffer) Qpy

func (b *Buffer) Qpy(s string) *Buffer

Qpy appends string quoted as Python would do.

func (*Buffer) Qpyb

func (b *Buffer) Qpyb(x []byte) *Buffer

Qpyb appends []byte quoted as Python would do.

func (*Buffer) Qpycb

func (b *Buffer) Qpycb(c byte) *Buffer

Qpycb appends byte quoted as Python would do for a single-character string.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset empties the buffer keeping underlying storage for future formattings.

func (*Buffer) S

func (b *Buffer) S(s string) *Buffer

S appends string formatted by %s.

func (*Buffer) Sb

func (b *Buffer) Sb(x []byte) *Buffer

Sb appends []byte formatted by %s.

func (*Buffer) V

func (b *Buffer) V(x Stringer) *Buffer

V, similarly to %v, adds x formatted by default rules.

func (*Buffer) X

func (b *Buffer) X(i int) *Buffer

X appends int formatted by %x.

func (*Buffer) X016

func (b *Buffer) X016(x uint64) *Buffer

X016, similarly to %016x, adds hex representation of uint64 x.

func (*Buffer) Xb

func (b *Buffer) Xb(x []byte) *Buffer

Xb appends []byte formatted by %x.

func (*Buffer) Xs

func (b *Buffer) Xs(x string) *Buffer

Xs appends string formatted by %x.

type Stringer

type Stringer interface {
	// XFmtString method is used to append formatted value to destination buffer
	// The grown buffer have to be returned
	XFmtString(b []byte) []byte

Stringer is interface for natively formatting a value representation via xfmt.

Source Files

Jump to

Keyboard shortcuts

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