1 // Stores a Block Locator Digest compactly. Can be used as a map key.
11 var LocatorPattern = regexp.MustCompile(
12 "^[0-9a-fA-F]{32}\\+[0-9]+(\\+[A-Z][A-Za-z0-9@_-]*)*$")
14 // Stores a Block Locator Digest compactly, up to 128 bits.
15 // Can be used as a map key.
16 type BlockDigest struct {
21 type DigestWithSize struct {
26 type BlockLocator struct {
32 func (d BlockDigest) String() string {
33 return fmt.Sprintf("%016x%016x", d.H, d.L)
36 func (w DigestWithSize) String() string {
37 return fmt.Sprintf("%s+%d", w.Digest.String(), w.Size)
40 // Will create a new BlockDigest unless an error is encountered.
41 func FromString(s string) (dig BlockDigest, err error) {
43 err = fmt.Errorf("Block digest should be exactly 32 characters but this one is %d: %s", len(s), s)
48 d.H, err = strconv.ParseUint(s[:16], 16, 64)
52 d.L, err = strconv.ParseUint(s[16:], 16, 64)
60 func IsBlockLocator(s string) bool {
61 return LocatorPattern.MatchString(s)
64 func ParseBlockLocator(s string) (b BlockLocator, err error) {
65 if !LocatorPattern.MatchString(s) {
66 err = fmt.Errorf("String \"%s\" does not match BlockLocator pattern "+
69 LocatorPattern.String())
71 tokens := strings.Split(s, "+")
73 var blockDigest BlockDigest
74 // We expect both of the following to succeed since LocatorPattern
75 // restricts the strings appropriately.
76 blockDigest, err = FromString(tokens[0])
80 blockSize, err = strconv.ParseInt(tokens[1], 10, 0)
84 b.Digest = blockDigest
85 b.Size = int(blockSize)