Commit b750d103 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 48ada1da
......@@ -6,6 +6,8 @@ package xfmt
import (
"encoding/hex"
"strconv"
"unicode/utf8"
"../xslice"
......@@ -32,6 +34,11 @@ func (b *Buffer) Reset() {
*b = (*b)[:0]
}
// Bytes returns buffer storage as []byte
func (b Buffer) Bytes() []byte {
return []byte(b)
}
// Append appends to b formatted x
//
// NOTE sadly since x is interface it makes real value substituted to it
......@@ -50,6 +57,51 @@ func (b *Buffer) V(x Stringer) *Buffer {
return b
}
// S appends string formatted by %s
func (b *Buffer) S(s string) *Buffer {
*b = append(*b, s...)
return b
}
// Sb appends []byte formatted by %s
func (b *Buffer) Sb(x []byte) *Buffer {
*b = append(*b, x...)
return b
}
// Cb appends byte formated by %c
func (b *Buffer) Cb(c byte) *Buffer {
*b = append(*b, c)
return b
}
// AppendRune appends to be UTF-8 encoding of r
func AppendRune(b []byte, r rune) []byte {
l := len(b)
b = xslice.Grow(b, utf8.UTFMax)
n := utf8.EncodeRune(b[l:], r)
return b[:l+n]
}
// C appends rune formatted by %c
func (b *Buffer) C(r rune) *Buffer {
*b = AppendRune(*b, r)
return b
}
// D appends int formatted by %d
func (b *Buffer) D(i int) *Buffer {
*b = strconv.AppendInt(*b, int64(i), 10)
return b
}
// X appends int formatted by %x
func (b *Buffer) X(i int) *Buffer {
*b = strconv.AppendInt(*b, int64(i), 16)
return b
}
// AppendHex appends to b hex representation of x
func AppendHex(b []byte, x []byte) []byte {
lx := hex.EncodedLen(len(x))
......@@ -59,15 +111,15 @@ func AppendHex(b []byte, x []byte) []byte {
return b
}
// X, similarly to %x, adds hex representation of x
func (b *Buffer) X(x []byte) *Buffer {
// Xb appends []byte formatted by %x
func (b *Buffer) Xb(x []byte) *Buffer {
*b = AppendHex(*b, x)
return b
}
// XXX do we need it here ?
// Xs appends string formatted by %x
func (b *Buffer) Xs(x string) *Buffer {
return b.X(mem.Bytes(x))
return b.Xb(mem.Bytes(x))
}
// TODO XX = %X
......
......@@ -11,9 +11,24 @@ import (
// verify formatting result is the same in between std fmt and xfmt
func TestXFmt(t *testing.T) {
testv := []struct {format, xformatMeth string; value interface{}} {
{"%x", "X", []byte("hello")},
{"%x", "Xs", "world"},
{"%c", "Cb", byte('A')},
{"%c", "C", rune(-1)},
{"%c", "C", 'B'}, // 1-byte encoded
{"%c", "C", 'и'}, // 2-bytes encoded
{"%c", "C", '\u20ac'}, // 3-bytes encoded
{"%c", "C", '\U00010001'}, // 4-bytes encoded
// TODO %q qb qr qs qcb qc
{"%s", "S", "hello"},
{"%s", "Sb", []byte("world")},
{"%x", "Xb", []byte("hexstring")},
{"%x", "Xs", "stringhex"},
{"%d", "D", 12765},
{"%x", "X", 12789},
{"%016x", "X016", uint64(124)},
// TODO .V
}
buf := &Buffer{}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment