-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.go
200 lines (171 loc) · 3.87 KB
/
interval.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package comver
// interval represents the intersection (logical AND) of two constraints.
type interval [2]*constraint
const (
ErrImpossibleInterval stringError = "impossible interval"
)
// NewInterval creates a new interval representing the intersection (logical AND) of two constraints.
//
// If either c1 or c2 is nil, it represents a boundless range.
// If both c1 and c2 are nil, it matches all versions (wildcard).
func NewInterval(c1, c2 *constraint) (interval, error) { //nolint:cyclop
cmp := c1.compare(c2)
// ensure c1 is the lower than c2
if cmp > 0 {
c1, c2 = c2, c1
}
switch {
case c1 == nil && c2 == nil:
return interval{}, nil
case c1 == nil:
return interval{c2}, nil
case c2 == nil:
return interval{c1}, nil
case cmp == 0: // exactly the same
return interval{c1}, nil
case c1.op.ceillingless() && c2.op.ceillingless():
// same direction
return interval{c2}, nil
case c1.op.floorless() && c2.op.floorless():
// same direction
return interval{c1}, nil
case c1.version.Compare(c2.version) == 0 && c1.Check(c1.version) && c2.Check(c2.version):
// same version & different directions & overlapping
return interval{c1, c2}, nil
case c1.Check(c2.version) && c2.Check(c1.version):
return interval{c1, c2}, nil
default:
// different directions & no overlap
return interval{}, ErrImpossibleInterval
}
}
// Check reports whether a [Version] satisfies the interval.
func (i interval) Check(v Version) bool {
for _, c := range i {
if c != nil && !c.Check(v) {
return false
}
}
return true
}
func (i interval) String() string {
switch {
case i[0] == nil && i[1] == nil:
return "*"
case i[0] == nil:
return i[1].String()
case i[1] == nil:
return i[0].String()
}
if i.exactVersionOnly() {
return i[0].version.Short()
}
cmp := i[0].compare(i[1])
switch {
case cmp < 0:
return i[0].String() + " " + i[1].String()
case cmp > 0:
return i[1].String() + " " + i[0].String()
default:
return i[0].String()
}
}
func (i interval) wildcard() bool {
return i[0] == nil && i[1] == nil
}
func (i interval) floorless() bool {
return i.floor() == nil
}
func (i interval) floor() *constraint { //nolint:cyclop
if i.wildcard() {
return nil
}
if i[0] != nil && i[1] == nil {
if i[0].lowerbounded() {
return i[0]
}
return nil
}
if i[0] == nil && i[1] != nil {
if i[1].lowerbounded() {
return i[1]
}
return nil
}
// both i[0] and i[1] are not nil
if !i[0].lowerbounded() && !i[1].lowerbounded() {
return nil
}
if i[0].lowerbounded() && i[1].lowerbounded() {
cmp := i[0].compare(i[1])
switch {
case cmp < 0:
return i[0]
case cmp > 0:
return i[1]
default:
return i[0]
}
}
// exactly one of them is lower bounded
if i[0].lowerbounded() {
return i[0]
}
return i[1]
}
func (i interval) ceilingless() bool {
return i.ceiling() == nil
}
func (i interval) ceiling() *constraint { //nolint:cyclop
if i.wildcard() {
return nil
}
if i[0] != nil && i[1] == nil {
if i[0].upperbounded() {
return i[0]
}
return nil
}
if i[0] == nil && i[1] != nil {
if i[1].upperbounded() {
return i[1]
}
return nil
}
// both i[0] and i[1] are not nil
if !i[0].upperbounded() && !i[1].upperbounded() {
return nil
}
if i[0].upperbounded() && i[1].upperbounded() {
cmp := i[0].compare(i[1])
switch {
case cmp < 0:
return i[1]
case cmp > 0:
return i[0]
default:
return i[0]
}
}
// exactly one of them is upper bounded
if i[0].upperbounded() {
return i[0]
}
return i[1]
}
func (i interval) exactVersionOnly() bool {
if i[0] == nil || i[1] == nil {
return false
}
if i[0].version.Compare(i[1].version) != 0 {
return false
}
return (i[0].lowerbounded() && i[1].upperbounded()) || (i[0].upperbounded() && i[1].lowerbounded())
}
func (i interval) compare(j interval) int {
cmp := i.floor().compare(j.floor())
if cmp != 0 {
return cmp
}
return j.ceiling().compare(j.ceiling())
}