Module: Vers::ALPMVersion

Extended by:
ALPMVersion
Included in:
ALPMVersion
Defined in:
lib/vers/distribution_version.rb

Instance Method Summary collapse

Instance Method Details

#compare(left, right) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/vers/distribution_version.rb', line 267

def compare(left, right)
  left_epoch, left_version, left_release, left_has_release = split(left)
  right_epoch, right_version, right_release, right_has_release = split(right)

  comparison = compare_part(left_epoch, right_epoch)
  return comparison unless comparison.zero?

  comparison = compare_part(left_version, right_version)
  return comparison unless comparison.zero?

  return compare_part(left_release, right_release) if left_has_release && right_has_release

  0
end

#compare_part(left, right) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/vers/distribution_version.rb', line 308

def compare_part(left, right)
  left_segments = segments(left)
  right_segments = segments(right)

  [left_segments.length, right_segments.length].max.times do |index|
    left_segment = left_segments[index]
    right_segment = right_segments[index]
    return 0 unless left_segment || right_segment
    return right_segment[0] == :alpha ? 1 : -1 unless left_segment
    return left_segment[0] == :alpha ? -1 : 1 unless right_segment

    left_kind, left_value = left_segment
    right_kind, right_value = right_segment
    if left_kind != right_kind
      return 1 if left_kind == :digit
      return -1 if right_kind == :digit
      return 1 if left_kind == :other
      return -1
    end

    comparison = case left_kind
                 when :digit
                   VersionComparison.compare_numbers(left_value, right_value)
                 when :alpha
                   left_value <=> right_value
                 else
                   left_value.length <=> right_value.length
                 end
    return comparison unless comparison.zero?
  end

  0
end

#normalize(value) ⇒ Object



287
288
289
# File 'lib/vers/distribution_version.rb', line 287

def normalize(value)
  value.to_s.strip
end

#prerelease?(_value) ⇒ Boolean

Returns:

  • (Boolean)


291
292
293
# File 'lib/vers/distribution_version.rb', line 291

def prerelease?(_value)
  false
end

#segment_kind(byte) ⇒ Object



355
356
357
358
359
360
# File 'lib/vers/distribution_version.rb', line 355

def segment_kind(byte)
  return :digit if byte.between?(48, 57)
  return :alpha if byte.between?(65, 90) || byte.between?(97, 122)

  :other
end

#segments(value) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/vers/distribution_version.rb', line 342

def segments(value)
  result = []
  index = 0
  while index < value.length
    kind = segment_kind(value.getbyte(index))
    ending = index + 1
    ending += 1 while ending < value.length && segment_kind(value.getbyte(ending)) == kind
    result << [kind, value[index...ending]]
    index = ending
  end
  result
end

#split(value) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/vers/distribution_version.rb', line 295

def split(value)
  epoch, version = value.to_s.split(":", 2)
  unless version
    version = epoch
    epoch = "0"
  end

  separator = version.rindex("-")
  return [epoch, version, "", false] unless separator

  [epoch, version[0...separator], version[(separator + 1)..], true]
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


282
283
284
285
# File 'lib/vers/distribution_version.rb', line 282

def valid?(value)
  string = value.to_s.strip
  !string.empty? && !string.match?(/[[:space:]]/)
end