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
b750d103
Commit
b750d103
authored
Mar 25, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
48ada1da
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
6 deletions
+73
-6
t/neo/xcommon/xfmt/fmt.go
t/neo/xcommon/xfmt/fmt.go
+56
-4
t/neo/xcommon/xfmt/fmt_test.go
t/neo/xcommon/xfmt/fmt_test.go
+17
-2
No files found.
t/neo/xcommon/xfmt/fmt.go
View file @
b750d103
...
...
@@ -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
{
// X
b appends []byte formatted by %
x
func
(
b
*
Buffer
)
X
b
(
x
[]
byte
)
*
Buffer
{
*
b
=
AppendHex
(
*
b
,
x
)
return
b
}
// X
XX do we need it here ?
// X
s appends string formatted by %x
func
(
b
*
Buffer
)
Xs
(
x
string
)
*
Buffer
{
return
b
.
X
(
mem
.
Bytes
(
x
))
return
b
.
X
b
(
mem
.
Bytes
(
x
))
}
// TODO XX = %X
...
...
t/neo/xcommon/xfmt/fmt_test.go
View file @
b750d103
...
...
@@ -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
{}
...
...
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