Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
48ada1da
Commit
48ada1da
authored
Mar 25, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
bbf27ce0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
3 deletions
+114
-3
t/neo/.gitignore
t/neo/.gitignore
+7
-0
t/neo/xcommon/xfmt/fmt.go
t/neo/xcommon/xfmt/fmt.go
+40
-3
t/neo/xcommon/xfmt/fmt_test.go
t/neo/xcommon/xfmt/fmt_test.go
+67
-0
No files found.
t/neo/.gitignore
0 → 100644
View file @
48ada1da
out.mem
out.cpu
mem.out
cpu.out
# files after go test -c
*.test
t/neo/xcommon/xfmt/fmt.go
View file @
48ada1da
// TODO copyright/license
// Package xfmt provide addons to std fmt and strconv package with focus on
// Package xfmt provide addons to std fmt and strconv package
s
with focus on
// formatting text without allocations.
package
xfmt
...
...
@@ -8,6 +8,8 @@ import (
"encoding/hex"
"../xslice"
"lab.nexedi.com/kirr/go123/mem"
)
const
(
...
...
@@ -21,6 +23,15 @@ type Stringer interface {
XFmtString
(
b
[]
byte
)
[]
byte
}
// Buffer provides syntactic sugar for formatting mimicking fmt.Printf style
// XXX combine with bytes.Buffer ?
type
Buffer
[]
byte
// Reset empties the buffer keeping underlying storage for future formattings
func
(
b
*
Buffer
)
Reset
()
{
*
b
=
(
*
b
)[
:
0
]
}
// Append appends to b formatted x
//
// NOTE sadly since x is interface it makes real value substituted to it
...
...
@@ -32,6 +43,13 @@ func Append(b []byte, x Stringer) []byte {
return
x
.
XFmtString
(
b
)
}
// V, similarly to %v, adds x formatted by default rules
// XXX -> v(interface {}) ?
func
(
b
*
Buffer
)
V
(
x
Stringer
)
*
Buffer
{
*
b
=
Append
(
*
b
,
x
)
return
b
}
// AppendHex appends to b hex representation of x
func
AppendHex
(
b
[]
byte
,
x
[]
byte
)
[]
byte
{
lx
:=
hex
.
EncodedLen
(
len
(
x
))
...
...
@@ -41,8 +59,21 @@ func AppendHex(b []byte, x []byte) []byte {
return
b
}
// AppendHex64 appends to b x formateed 16-character hex string
func
AppendHex64
(
b
[]
byte
,
x
uint64
)
[]
byte
{
// X, similarly to %x, adds hex representation of x
func
(
b
*
Buffer
)
X
(
x
[]
byte
)
*
Buffer
{
*
b
=
AppendHex
(
*
b
,
x
)
return
b
}
// XXX do we need it here ?
func
(
b
*
Buffer
)
Xs
(
x
string
)
*
Buffer
{
return
b
.
X
(
mem
.
Bytes
(
x
))
}
// TODO XX = %X
// AppendHex016 appends to b x formatted 16-character hex string
func
AppendHex016
(
b
[]
byte
,
x
uint64
)
[]
byte
{
// like sprintf("%016x") but faster and less allocations
l
:=
len
(
b
)
b
=
xslice
.
Grow
(
b
,
16
)
...
...
@@ -53,3 +84,9 @@ func AppendHex64(b []byte, x uint64) []byte {
}
return
b
}
// X016, similarly to %016x, adds hex representation of uint64 x
func
(
b
*
Buffer
)
X016
(
x
uint64
)
*
Buffer
{
*
b
=
AppendHex016
(
*
b
,
x
)
return
b
}
t/neo/xcommon/xfmt/fmt_test.go
0 → 100644
View file @
48ada1da
// TODO copyright/license
package
xfmt
import
(
"fmt"
"reflect"
"testing"
)
// 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"
},
{
"%016x"
,
"X016"
,
uint64
(
124
)},
}
buf
:=
&
Buffer
{}
xbuf
:=
reflect
.
ValueOf
(
buf
)
for
_
,
tt
:=
range
testv
{
// result via fmt
resFmt
:=
fmt
.
Sprintf
(
tt
.
format
,
tt
.
value
)
// result via xfmt (via reflect.Call)
buf
.
Reset
()
xmeth
:=
xbuf
.
MethodByName
(
tt
.
xformatMeth
)
if
!
xmeth
.
IsValid
()
{
t
.
Errorf
(
".%v: no such method"
,
tt
.
xformatMeth
)
continue
}
xargv
:=
[]
reflect
.
Value
{
reflect
.
ValueOf
(
tt
.
value
)}
xretv
:=
[]
reflect
.
Value
{}
callOk
:=
false
func
()
{
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
t
.
Errorf
(
"%v: panic: %v"
,
tt
,
r
)
}
}()
xretv
=
xmeth
.
Call
(
xargv
)
callOk
=
true
}()
if
!
callOk
{
continue
}
// check all formatters return pointer to the same buf
// (this way it is handy to do .S("hello ") .X016(123) .V(zzz) ...
if
!
(
len
(
xretv
)
==
1
&&
xretv
[
0
]
.
Interface
()
==
buf
)
{
t
.
Errorf
(
".%v: returned %#v ; want %#v"
,
tt
.
xformatMeth
,
xretv
[
0
]
.
Interface
(),
buf
)
continue
}
resXFmt
:=
string
(
*
buf
)
// results must be the same
if
resFmt
!=
resXFmt
{
t
.
Errorf
(
".%v(%v) -> %q != printf(%q) -> %q"
,
tt
.
xformatMeth
,
tt
.
value
,
resXFmt
,
tt
.
format
,
resFmt
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment