Commit db60a77f authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 302e5645
// 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])
}
*/
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:\nhave: %v %q\nwant: %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:\nhave: %v %v %q\nwant: %v %v %q", tt.in,
tmin, tmax, estr, tt.tidMin, tt.tidMax, tt.estr)
}
}
}
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