Module: Libversion

Defined in:
lib/ruby_libversion.rb,
ext/libversion/libversion.c

Constant Summary collapse

VERSIONFLAG_P_IS_PATCH =
INT2NUM(VERSIONFLAG_P_IS_PATCH)
VERSIONFLAG_ANY_IS_PATCH =
INT2NUM(VERSIONFLAG_ANY_IS_PATCH)
VERSIONFLAG_LOWER_BOUND =
INT2NUM(VERSIONFLAG_LOWER_BOUND)
VERSIONFLAG_UPPER_BOUND =
INT2NUM(VERSIONFLAG_UPPER_BOUND)

Class Method Summary collapse

Class Method Details

.version_compare2(v1, v2) ⇒ Object



4
5
6
# File 'ext/libversion/libversion.c', line 4

def self.version_compare2(v1, v2)
  version_compare4(v1, v2, 0, 0)
end

.version_compare4(v1, v2, v1_flags, v2_flags) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'ext/libversion/libversion.c', line 8

def self.version_compare4(v1, v2, v1_flags, v2_flags)
  # Parse the raw version strings into an array of VersionComponents.
  v1_components = parse_version_string(v1, v1_flags)
  v2_components = parse_version_string(v2, v2_flags)

  # Pad each version so that they are the same length prior to zipping and comparison.
  # We add additional padding to support lower and upper bounds even when the versions have an equal number of components.
  v1_components.fill(VersionComponent.new([nil, nil, nil], v1_flags), v1_components.size, v2_components.size)
  v2_components.fill(VersionComponent.new([nil, nil, nil], v2_flags), v2_components.size, v1_components.size)

  # Zip the components together for ease of comparison.
  zipped_components = v1_components.zip(v2_components)

  # Iterate over the zipped components, comparing them and returning the first nonzero result we receive.
  (0...zipped_components.size).each do |i|
    comparison_result = zipped_components[i][0] <=> zipped_components[i][1]
    return comparison_result unless comparison_result.zero?
  end

  # If we got here, every component was equal, and thus the versions are equal.
  return 0
end