Compare two Versionable objects

Returns true if first is greater than second, false if not

@param  Versionable $v1
@param  Versionable $v2
@return bool
541399
private static function versionableGreaterThan(Versionable $v1, Versionable $v2)
    {
        if ($v1->getMajor() > $v2->getMajor()) {
            return true;
        }
        if ($v1->getMajor() < $v2->getMajor()) {
            return false;
        }

        if ($v1->getMinor() > $v2->getMinor()) {
            return true;
        }
        if ($v1->getMinor() < $v2->getMinor()) {
            return false;
        }

        if ($v1->getPatch() > $v2->getPatch()) {
            return true;
        }
        if ($v1->getPatch() < $v2->getPatch()) {
            return false;
        }

        return false;
    }
645632
public static function softerVersionCompare($version1, $version2, $operand) {
        $v1Parts = explode('.', $version1);
        $version1 .= str_repeat('.0', 3-count($v1Parts));
        $v2Parts = explode('.', $version2);
        $version2 .= str_repeat('.0', 3-count($v2Parts));
        $version1 = str_replace('.x', '.1000', $version1);
        $version2 = str_replace('.x', '.1000', $version2);       
        return version_compare($version1, $version2, $operand);
    }
651918
public static function compare($n1, $n2)
    {
        $n1 = self::round($n1, 2);
        $n2 = self::round($n2, 2);

        return (bccomp($n1, $n2, self::$scale) === 0);
    }
1302383
public static function compare(Version $version1, Version $version2)
    {
        if ($version1->toString() == $version2->toString()) {
            return 0;
        }

        $v1 = $version1->getVersionArray();
        $v2 = $version2->getVersionArray();

        if (count($v1) > count($v2)) {
            $v2 = array_pad($v2, count($v1), 0);
        } elseif (count($v1) < count($v2)) {
            $v1 = array_pad($v1, count($v2), 0);
        }

        // version comparison
        foreach ($v1 as $k => $v) {
            if ($v == $v2[$k]) {
                continue;
            }
            if ($v < $v2[$k]) {
                return -1;
            } else {
                return 1;
            }
        }

        // stability comparison
        $s1 = $version1->getStabilityVersion();
        $s2 = $version2->getStabilityVersion();
        if (count($s1) > count($s2)) {
            $s2 = array_pad($s2, count($s1), '');
        } elseif (count($s1) < count($s2)) {
            $s1 = array_pad($s1, count($s2), '');
        }

        foreach ($s1 as $k => $v) {
            if ($v == '*' || $s2[$k] == '*') {
                return 0;
            }
            if ($v === $s2[$k]) {
                continue;
            }
            if ($v === '') {
                if (!is_numeric($s2[$k])) {
                    return 1;
                } else {
                    $v = '0';
                }
            } elseif ($s2[$k] === '') {
                if (!is_numeric($v)) {
                    return -1;
                } else {
                    $s2[$k] = '0';
                }
            }
            if (is_numeric($v)) {
                if (is_numeric($s2[$k])) {
                    $v1 = intval($v);
                    $v2 = intval($s2[$k]);
                    if ($v1 == $v2) {
                        continue;
                    }
                    if ($v1 < $v2) {
                        return -1;
                    }
                    return 1;
                } else {
                    return -1;
                }
            } elseif (is_numeric($s2[$k])) {
                return -1;
            } else {
                if ($v == 'dev' || $v == 'pre') {
                    $v = 'aaaaaaaaaa';
                }
                $v2 = $s2[$k];
                if ($v2 == 'dev' || $v2 == 'pre') {
                    $v2 = 'aaaaaaaaaa';
                }
                $r = strcmp($v, $v2);
                if ($r > 0) {
                    return 1;
                } elseif ($r < 0) {
                    return -1;
                }
            }
        }

        $sv1 = $version1->getSecondaryVersion();
        $sv2 = $version2->getSecondaryVersion();
        if (($sv1 instanceof Version) || ($sv2 instanceof Version)) {
            if (($sv1 instanceof Version) && ($sv2 instanceof Version)) {
                return self::compare($sv1, $sv2);
            }
            else if ($sv1 instanceof Version) {
                return 1;
            }
            else if ($sv2 instanceof Version) {
                return -1;
            }
        }
        return 0;
    }
1011541
public static function isNewestVersionAvailable()
    {
        $latestVersion = self::getLatestVersion();
        if (!empty($latestVersion)
            && version_compare(Version::VERSION, $latestVersion) == -1
        ) {
            return $latestVersion;
        }
        return false;
    }
1142856
def match_version(desired, available, cmp_func=None, ignore_epoch=False):
    '''
    
    '''
    oper, version = split_comparison(desired)
    if not oper:
        oper = '=='
    for candidate in available:
        if salt.utils.versions.compare(ver1=candidate,
                                       oper=oper,
                                       ver2=version,
                                       cmp_func=cmp_func,
                                       ignore_epoch=ignore_epoch):
            return candidate
    return None
382609
public static function greaterThan(Version $v1, Version $v2)
    {
        // If they are equal, they can not be greater/smaller than each other
        if (self::equals($v1,  $v2)) {
            return false;
        }

        // Compare on the major/minor/patch level if we can
        if (!self::versionableEquals($v1, $v2)) {
            return self::versionableGreaterThan($v1, $v2);
        }

        // v1 has a pre-release, but v2 does not, v2 is bigger
        if ($v1->hasPreRelease() && !$v2->hasPreRelease()) {
            return false;
        }

        // v1 does not have a pre-release, but v2 does, v1 is bigger
        if (!$v1->hasPreRelease() && $v2->hasPreRelease()) {
            return true;
        }

        // Compare on the re-release level if possible
        if ($v1->hasPreRelease() && $v2->hasPreRelease()) {
            if (!self::preReleaseEquals($v1->getPreRelease(), $v2->getPreRelease())) {

                // If v1 has a larger pre-release than v2, it's bigger
                if (self::preReleaseGreaterThan($v1->getPreRelease(), $v2->getPreRelease())) {
                    return true;
                }

                // v2 has a larger pre-release than v1, v2 is bigger.
                return false;
            }
        }

        // Both have the same pre-release version, but only one has a build
        // Version with a build is bigger
        if ($v1->hasBuild() && !$v2->hasBuild()) {
            return true;
        }
        if (!$v1->hasBuild() && $v2->hasBuild()) {
            return false;
        }

        // Compare the build version
        if ($v1->hasBuild() && $v2->hasBuild()) {
            return self::buildGreaterThan($v1->getBuild(), $v2->getBuild());
        }

        return true;
    }
699254
public function match(Version $version)
    {
        $versionStabilityIndex = array_search($version->getStability(), Version::$stabilities);
        $constraintStabilityIndex = array_search($this->stability, Version::$stabilities);

        if ($this->compare($versionStabilityIndex, $constraintStabilityIndex) !== 0) {
            return false;
        }

        $numericValues = $version->numeric();
        $comparisons = array();

        foreach ($this->version as $i => $value) {
            $comparisons[$i] = $this->compare($numericValues[$i], $value);
        }

        switch ($this->operator) {
            case '=': case '==':
                // all parts must be equal:
                return array_filter($comparisons) === array();
                break;
            case '!': case '!=':
                // any part must be unequal:
                return array_filter($comparisons) !== array();
                break;
            case '>=':
            case '<=':
            case '>':
            case '<':
                // the first mismatch is the tie breaker
                $mismatches = array_filter($comparisons);
                if (!count($mismatches)) {
                    // if no mismatches, only ok if equality operator
                    return in_array($this->operator, array('<=', '>='));
                } else {
                    $first = array_shift($mismatches);
                    return
                           ($this->operator === '<'  && $first < 0)
                        || ($this->operator === '<=' && $first < 0)
                        || ($this->operator === '>'  && $first > 0)
                        || ($this->operator === '>=' && $first > 0)
                    ;
                }
        }

        throw new \UnexpectedValueException("An unknown edge case was encountered. Is the constraint valid?");
    }
439037
public static function isEqualTo(Version $left, Version $right)
    {
        return (self::EQUAL_TO === self::compareTo($left, $right));
    }
713707
public static function areEqual(&$ref1, &$ref2) : bool
    {
        if ($ref1 !== $ref2) {
            return false;
        }

        if (!self::$secret) {
            self::$secret = new \stdClass();
        }

        $tempStorage = $ref1;

        $ref1     = self::$secret;
        $areEqual = $ref1 === $ref2;

        $ref1 = $tempStorage;

        return $areEqual;
    }
1097
protected function compareVersion()
    {
        $manifest = $this->getManifest();
        $extensionEntity = $this->getExtensionEntity();

        if (isset($extensionEntity)) {
            $comparedVersion = version_compare($manifest['version'], $extensionEntity->get('version'), '>=');
            if ($comparedVersion <= 0) {
                $this->throwErrorAndRemovePackage('You cannot install an older version of this extension.');
            }
        }
    }
1354714
private static function preReleaseGreaterThan(PreRelease $v1, PreRelease $v2)
    {
        // Pre-releases can be denoted as versionable (X.Y.Z) or greek (alpha.5)
        // We let versionable take precedence
        if (!self::versionableEquals($v1, $v2)) {
            // If v1 is bigger on the versionable level, it's bigger.
            return self::versionableGreaterThan($v1, $v2);
        }

        // "Greek" part is "bigger" for v1
        if ($v1->getGreek() !== $v2->getGreek()) {
            return self::greekLargerThan($v1->getGreek(), $v2->getGreek());
        }

        // Release number for v1 is bigger than v2's
        if ($v1->getReleaseNumber() > $v2->getReleaseNumber()) {
            return true;
        }

        return false;
    }
684199
def compare(self, v1, v2):
        
        try:
            if len(v1) != len(v2): return 0
            for x, y in zip(v1, v2):
                if abs( x - y ) > self.tolerance:
                    return 0
            return 1
        except:
            # some patterns may not be vectors
            try:
                if abs( v1 - v2 ) > self.tolerance:
                    return 0
                else:
                    return 1
            except:
                return 0
203992
public function revisionCompare($revision1, $revision2)
    {
        $revision1 = (int) $revision1;
        $revision2 = (int) $revision2;

        if ($revision1 > $revision2) {
            return 1;
        } elseif ($revision1 < $revision2) {
            return -1;
        } else {
            return 0;
        }
    }
704218
public function compare($object1, $object2)
    {
        if (! self::isCastableToString($object1)) {
            throw new ComparatorException('$object1 (type: ' . gettype($object1) . ') is not castable to string.');
        }
        if (! in_array(gettype($object2), ['string', 'integer', 'double'])) {
            throw new ComparatorException('$object2 (type: ' . gettype($object2) . ') is not castable to string.');
        }
        return call_user_func("\\{$this->compareMode}", $object1, $object2);
    }
235157
public static function haveTheSameValue($value1, $value2)
    {
        if (\is_array($value1)) {
            if (!self::areRecursivelyEquals($value1, $value2)) {
                return false;
            }
        } else {
            if ($value1 !== $value2) {
                return false;
            }
        }

        return true;
    }
491092
def semver_newer(v1, v2):
    
    v1_parts = v1.split('.')
    v2_parts = v2.split('.')
    if len(v1_parts) < 3 or len(v2_parts) < 3:
        # one isn't a semantic version
        return False

    v1_major, v1_minor, v1_patch = get_version_parts(v1_parts, int)
    v2_major, v2_minor, v2_patch = get_version_parts(v2_parts, int)

    if v1_major > v2_major:
        return False

    if v1_major == v2_major and v1_minor >= v2_minor:
        return False

    return True