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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
# frozen_string_literal: true
module SemverDialects
module IntervalType
UNKNOWN = 0
LEFT_OPEN = 1
LEFT_CLOSED = 2
RIGHT_OPEN = 4
RIGHT_CLOSED = 8
end
# Interval is an interval that starts with a lower boundary
# and ends with an upper boundary. The interval includes the boundaries
# or not depending on its type.
class Interval
# Returns an interval that only includes the given version.
def self.from_version(version)
boundary = Boundary.new(version)
Interval.new(IntervalType::LEFT_CLOSED | IntervalType::RIGHT_CLOSED, boundary, boundary)
end
attr_accessor :type, :start_cut, :end_cut
def initialize(type, start_cut, end_cut)
@type = type
@start_cut = start_cut
@end_cut = end_cut
end
def intersect(other_interval)
return EmptyInterval.new if empty?
# this look odd -- we have to use it here though, because it may be that placeholders are present inside
# the version for which > and < would yield true
return EmptyInterval.new if !(@start_cut <= other_interval.end_cut) || !(other_interval.start_cut <= @end_cut)
start_cut_new = max(@start_cut, other_interval.start_cut)
end_cut_new = min(@end_cut, other_interval.end_cut)
# compute the boundaries for the intersection
type = compute_intersection_boundary(self, other_interval, start_cut_new, end_cut_new)
interval = Interval.new(type, start_cut_new, end_cut_new)
half_open = !(interval.bit_set?(IntervalType::RIGHT_CLOSED) && interval.bit_set?(IntervalType::LEFT_CLOSED))
interval.singleton? && half_open ? EmptyInterval.new : interval
end
def special(cut)
cut.instance_of?(AboveAll) || cut.instance_of?(BelowAll)
end
def to_s
s = ''
s += bit_set?(IntervalType::LEFT_CLOSED) ? '[' : ''
s += bit_set?(IntervalType::LEFT_OPEN) ? '(' : ''
s += [@start_cut, @end_cut].join(',')
s += bit_set?(IntervalType::RIGHT_CLOSED) ? ']' : ''
s += bit_set?(IntervalType::RIGHT_OPEN) ? ')' : ''
s
end
# this function returns a human-readable descriptions of the version strings
def to_description_s
s = ''
if distinct?
s = "version #{@start_cut}"
elsif universal?
s = 'all versions '
else
s = 'all versions '
s += if start_cut.instance_of?(BelowAll)
''
elsif bit_set?(IntervalType::LEFT_OPEN)
"after #{@start_cut} "
else
bit_set?(IntervalType::LEFT_CLOSED) ? "starting from #{@start_cut} " : ''
end
s += if end_cut.instance_of?(AboveAll)
''
elsif bit_set?(IntervalType::RIGHT_OPEN)
"before #{@end_cut}"
else
bit_set?(IntervalType::RIGHT_CLOSED) ? "up to #{@end_cut}" : ''
end
end
s.strip
end
def to_nuget_s
to_maven_s
end
def to_maven_s
s = ''
# special case -- distinct version
if distinct?
s += "[#{@start_cut}]"
else
s += if start_cut.instance_of?(BelowAll)
'(,'
elsif bit_set?(IntervalType::LEFT_OPEN)
"[#{@start_cut},"
else
bit_set?(IntervalType::LEFT_CLOSED) ? "[#{@start_cut}," : ''
end
s += if end_cut.instance_of?(AboveAll)
')'
elsif bit_set?(IntervalType::RIGHT_OPEN)
"#{@end_cut})"
else
bit_set?(IntervalType::RIGHT_CLOSED) ? "#{@end_cut}]" : ''
end
end
s
end
def distinct?
bit_set?(IntervalType::LEFT_CLOSED) && bit_set?(IntervalType::RIGHT_CLOSED) && @start_cut == @end_cut
end
def subsumes?(other)
@start_cut <= other.start_cut && @end_cut >= other.end_cut
end
def universal?
(bit_set?(IntervalType::LEFT_OPEN) && bit_set?(IntervalType::RIGHT_OPEN) && @start_cut.instance_of?(BelowAll) && @end_cut.instance_of?(AboveAll)) || @start_cut.is_initial_version? && @end_cut.instance_of?(AboveAll)
end
def to_gem_s
get_canoncial_s
end
def to_ruby_s
get_canoncial_s
end
def to_npm_s
get_canoncial_s
end
def to_conan_s
get_canoncial_s
end
def to_go_s
get_canoncial_s
end
def to_pypi_s
get_canoncial_s(',', '==')
end
def to_packagist_s
get_canoncial_s(',')
end
def to_cargo_s
get_canoncial_s
end
def empty?
instance_of?(EmptyInterval)
end
def singleton?
@start_cut == @end_cut && @start_cut.semver == @end_cut.semver
end
def ==(other)
@start_cut == other.start_cut && @end_cut == other.end_cut && @type == other.type
end
def bit_set?(interval_type)
@type & interval_type != 0
end
protected
def compute_intersection_boundary(interval_a, interval_b, start_cut_new, end_cut_new)
compute_boundary(interval_a, interval_b, start_cut_new, end_cut_new, IntervalType::LEFT_OPEN,
IntervalType::RIGHT_OPEN)
end
def compute_boundary(interval_a, interval_b, start_cut_new, end_cut_new, left_check, right_check)
start_cut_a = interval_a.start_cut
end_cut_a = interval_a.end_cut
type_a = interval_a.type
start_cut_b = interval_b.start_cut
end_cut_b = interval_b.end_cut
type_b = interval_b.type
left_fill = left_check == IntervalType::LEFT_OPEN ? IntervalType::LEFT_CLOSED : IntervalType::LEFT_OPEN
right_fill = right_check == IntervalType::RIGHT_OPEN ? IntervalType::RIGHT_CLOSED : IntervalType::RIGHT_OPEN
# compute the boundaries for the union
if start_cut_b == start_cut_a
one_left_closed = left_type(type_a) == left_check || left_type(type_b) == left_check
left_type = one_left_closed ? left_check : left_fill
else
left_type = start_cut_new == start_cut_a ? left_type(type_a) : left_type(type_b)
end
if end_cut_b == end_cut_a
one_right_closed = right_type(type_a) == right_check || right_type(type_b) == right_check
right_type = one_right_closed ? right_check : right_fill
else
right_type = end_cut_new == end_cut_a ? right_type(type_a) : right_type(type_b)
end
left_type | right_type
end
def get_canoncial_s(delimiter = ' ', eq = '=')
if distinct?
"#{eq}#{@start_cut}"
else
first = if start_cut.instance_of?(BelowAll)
''
elsif bit_set?(IntervalType::LEFT_OPEN)
">#{@start_cut}"
else
bit_set?(IntervalType::LEFT_CLOSED) ? ">=#{@start_cut}" : ''
end
second = if end_cut.instance_of?(AboveAll)
''
elsif bit_set?(IntervalType::RIGHT_OPEN)
"<#{@end_cut}"
else
bit_set?(IntervalType::RIGHT_CLOSED) ? "<=#{@end_cut}" : ''
end
!first.empty? && !second.empty? ? "#{first}#{delimiter}#{second}" : first + second
end
end
def max(cut_a, cut_b)
cut_a > cut_b ? cut_a : cut_b
end
def min(cut_a, cut_b)
cut_a < cut_b ? cut_a : cut_b
end
def right_type(type)
(IntervalType::RIGHT_OPEN | IntervalType::RIGHT_CLOSED) & type
end
def left_type(type)
(IntervalType::LEFT_OPEN | IntervalType::LEFT_CLOSED) & type
end
end
class EmptyInterval < Interval
def initialize; end
def to_s
'empty'
end
end
end
|