Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
db60a77f
Commit
db60a77f
authored
Mar 06, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
302e5645
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
9 deletions
+114
-9
t/neo/zodb/str.go
t/neo/zodb/str.go
+64
-9
t/neo/zodb/str_test.go
t/neo/zodb/str_test.go
+50
-0
No files found.
t/neo/zodb/str.go
View file @
db60a77f
// TODO copyright / license
// formatting and
scanning
for basic zodb types
// formatting and
parsing
for basic zodb types
package
zodb
import
(
"fmt"
//"encoding/hex"
"encoding/hex"
"encoding/binary"
"lab.nexedi.com/kirr/go123/xstrings"
)
func
(
tid
Tid
)
String
()
string
{
...
...
@@ -39,16 +42,68 @@ func (xid Xid) String() string {
}
/
*
func
ParseTid(s string) (Tid
, error) {
//
-> scanf("%016x")
/
/ parseHex64 decode 16-character-wide hex-encoded string into uint64
func
parseHex64
(
subj
,
s
string
)
(
uint64
,
error
)
{
//
XXX like scanf("%016x") but scanf implicitly skips spaces without giving control to caller and is slower
var
b
[
8
]
byte
if
hex.DecodedLdn(len(s)) != 8
{
return 0, fmt.Errorf("
hex64parse: %q invalid len", s) // XXX
if
len
(
s
)
!=
16
{
return
0
,
fmt
.
Errorf
(
"
%s %q invalid"
,
subj
,
s
)
}
_, err = hex.Decode(b[:], mem.Bytes(s))
_
,
err
:=
hex
.
Decode
(
b
[
:
],
[]
byte
(
s
))
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"%s %q invalid"
,
subj
,
s
)
}
return
binary
.
BigEndian
.
Uint64
(
b
[
:
]),
nil
}
func
ParseTid
(
s
string
)
(
Tid
,
error
)
{
x
,
err
:=
parseHex64
(
"tid"
,
s
)
return
Tid
(
x
),
err
}
func
ParseOid
(
s
string
)
(
Oid
,
error
)
{
x
,
err
:=
parseHex64
(
"oid"
,
s
)
return
Oid
(
x
),
err
}
// ParseTidRange parses string of form "<tidmin>..<tidmax>" into tidMin, tidMax pair
// both <tidmin> and <tidmax> can be empty, in which case defaults 0 and TidMax are returned
func
ParseTidRange
(
s
string
)
(
tidMin
,
tidMax
Tid
,
err
error
)
{
s1
,
s2
,
err
:=
xstrings
.
Split2
(
s
,
".."
)
if
err
!=
nil
{
...
goto
Error
}
tidMin
=
0
tidMax
=
TidMax
if
s1
!=
""
{
tidMin
,
err
=
ParseTid
(
s1
)
if
err
!=
nil
{
goto
Error
}
}
if
s2
!=
""
{
tidMax
,
err
=
ParseTid
(
s2
)
if
err
!=
nil
{
goto
Error
}
}
return
tidMin
,
tidMax
,
nil
Error
:
return
0
,
0
,
fmt
.
Errorf
(
"tid range %q invalid"
,
s
)
}
/*
func (tid Tid) String2() string {
var b [8+16]byte
binary.BigEndian.PutUint64(b[:], uint64(tid))
hex.Encode(b[8:], b[:8])
//return mem.String(b[:8])
return string(b[:8])
}
*/
t/neo/zodb/str_test.go
0 → 100644
View file @
db60a77f
package
zodb
import
(
"testing"
)
func
TestParseHex64
(
t
*
testing
.
T
)
{
var
testv
=
[]
struct
{
in
string
;
out
uint64
;
estr
string
}
{
{
""
,
0
,
`tid "" invalid`
},
{
"0123456789abcde"
,
0
,
`tid "0123456789abcde" invalid`
},
{
"0123456789abcdeq"
,
0
,
`tid "0123456789abcdeq" invalid`
},
{
"0123456789abcdef"
,
0x0123456789abcdef
,
""
},
}
for
_
,
tt
:=
range
testv
{
x
,
err
:=
parseHex64
(
"tid"
,
tt
.
in
)
estr
:=
""
if
err
!=
nil
{
estr
=
err
.
Error
()
}
if
!
(
x
==
tt
.
out
&&
estr
==
tt
.
estr
)
{
t
.
Errorf
(
"parsehex64: %v: test error:
\n
have: %v %q
\n
want: %v %q"
,
tt
.
in
,
x
,
estr
,
tt
.
out
,
tt
.
estr
)
}
}
}
func
TestParseTidRange
(
t
*
testing
.
T
)
{
var
testv
=
[]
struct
{
in
string
;
tidMin
,
tidMax
Tid
;
estr
string
}
{
{
""
,
0
,
0
,
`tid range "" invalid`
},
{
"."
,
0
,
0
,
`tid range "." invalid`
},
{
".."
,
0
,
TidMax
,
""
},
{
"0123456789abcdef.."
,
0x0123456789abcdef
,
TidMax
,
""
},
{
"..0123456789abcdef"
,
0
,
0x0123456789abcdef
,
""
},
}
for
_
,
tt
:=
range
testv
{
tmin
,
tmax
,
err
:=
ParseTidRange
(
tt
.
in
)
estr
:=
""
if
err
!=
nil
{
estr
=
err
.
Error
()
}
if
!
(
tmin
==
tt
.
tidMin
&&
tmax
==
tt
.
tidMax
&&
estr
==
tt
.
estr
)
{
t
.
Errorf
(
"parseTidRange: %v: test error:
\n
have: %v %v %q
\n
want: %v %v %q"
,
tt
.
in
,
tmin
,
tmax
,
estr
,
tt
.
tidMin
,
tt
.
tidMax
,
tt
.
estr
)
}
}
}
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