forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uid.go
125 lines (109 loc) · 2.56 KB
/
uid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package uid
import (
"fmt"
"strings"
)
type Block struct {
Start uint32
End uint32
}
var (
ErrBlockSlashBadFormat = fmt.Errorf("block not in the format \"<start>/<size>\"")
ErrBlockDashBadFormat = fmt.Errorf("block not in the format \"<start>-<end>\"")
)
func ParseBlock(in string) (Block, error) {
if strings.Contains(in, "/") {
var start, size uint32
n, err := fmt.Sscanf(in, "%d/%d", &start, &size)
if err != nil {
return Block{}, err
}
if n != 2 {
return Block{}, ErrBlockSlashBadFormat
}
return Block{Start: start, End: start + size - 1}, nil
}
var start, end uint32
n, err := fmt.Sscanf(in, "%d-%d", &start, &end)
if err != nil {
return Block{}, err
}
if n != 2 {
return Block{}, ErrBlockDashBadFormat
}
return Block{Start: start, End: end}, nil
}
func (b Block) String() string {
return fmt.Sprintf("%d/%d", b.Start, b.Size())
}
func (b Block) RangeString() string {
return fmt.Sprintf("%d-%d", b.Start, b.End)
}
func (b Block) Size() uint32 {
return b.End - b.Start + 1
}
type Range struct {
block Block
size uint32
}
func NewRange(start, end, size uint32) (*Range, error) {
if start > end {
return nil, fmt.Errorf("start %d must be less than end %d", start, end)
}
if size == 0 {
return nil, fmt.Errorf("block size must be a positive integer")
}
if (end - start) < size {
return nil, fmt.Errorf("block size must be less than or equal to the range")
}
return &Range{
block: Block{start, end},
size: size,
}, nil
}
func ParseRange(in string) (*Range, error) {
var start, end, block uint32
n, err := fmt.Sscanf(in, "%d-%d/%d", &start, &end, &block)
if err != nil {
return nil, err
}
if n != 3 {
return nil, fmt.Errorf("range not in the format \"<start>-<end>/<blockSize>\"")
}
return NewRange(start, end, block)
}
func (r *Range) Size() uint32 {
return r.block.Size() / r.size
}
func (r *Range) String() string {
return fmt.Sprintf("%s/%d", r.block.RangeString(), r.size)
}
func (r *Range) BlockAt(offset uint32) (Block, bool) {
if offset > r.Size() {
return Block{}, false
}
start := r.block.Start + offset*r.size
return Block{
Start: start,
End: start + r.size - 1,
}, true
}
func (r *Range) Contains(block Block) bool {
ok, _ := r.Offset(block)
return ok
}
func (r *Range) Offset(block Block) (bool, uint32) {
if block.Start < r.block.Start {
return false, 0
}
if block.End > r.block.End {
return false, 0
}
if block.End-block.Start+1 != r.size {
return false, 0
}
if (block.Start-r.block.Start)%r.size != 0 {
return false, 0
}
return true, (block.Start - r.block.Start) / r.size
}