Skip to content

Commit

Permalink
merged Version files
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-cat committed Oct 17, 2024
1 parent 52ce87c commit bc9db42
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 172 deletions.
34 changes: 0 additions & 34 deletions Sources/Version/Version+Codable.swift

This file was deleted.

65 changes: 0 additions & 65 deletions Sources/Version/Version+Comparable.swift

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/Version/Version+Foundation.swift

This file was deleted.

43 changes: 0 additions & 43 deletions Sources/Version/Version+Range.swift

This file was deleted.

65 changes: 65 additions & 0 deletions Sources/Version/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,68 @@ public extension Version {
#if swift(>=5.5)
extension Version: Sendable {}
#endif

extension Version: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(major)
hasher.combine(minor)
hasher.combine(patch)
hasher.combine(prereleaseIdentifiers)
}
}

extension Version: Equatable {
/// Compares the provided versions *without* comparing any build-metadata
public static func == (lhs: Version, rhs: Version) -> Bool {
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch && lhs.prereleaseIdentifiers == rhs.prereleaseIdentifiers
}
}

extension Version: Comparable {
func isEqualWithoutPrerelease(_ other: Version) -> Bool {
return major == other.major && minor == other.minor && patch == other.patch
}

/**
`1.0.0` is less than `1.0.1`, `1.0.1-alpha` is less than `1.0.1` but
greater than `1.0.0`.
- Returns: `true` if `lhs` is less than `rhs`
*/
public static func < (lhs: Version, rhs: Version) -> Bool {
let lhsComparators = [lhs.major, lhs.minor, lhs.patch]
let rhsComparators = [rhs.major, rhs.minor, rhs.patch]

if lhsComparators != rhsComparators {
return lhsComparators.lexicographicallyPrecedes(rhsComparators)
}

guard lhs.prereleaseIdentifiers.count > 0 else {
return false // Non-prerelease lhs >= potentially prerelease rhs
}

guard rhs.prereleaseIdentifiers.count > 0 else {
return true // Prerelease lhs < non-prerelease rhs
}

let zippedIdentifiers = zip(lhs.prereleaseIdentifiers, rhs.prereleaseIdentifiers)
for (lhsPrereleaseIdentifier, rhsPrereleaseIdentifier) in zippedIdentifiers {
if lhsPrereleaseIdentifier == rhsPrereleaseIdentifier {
continue
}

let typedLhsIdentifier: Any = Int(lhsPrereleaseIdentifier) ?? lhsPrereleaseIdentifier
let typedRhsIdentifier: Any = Int(rhsPrereleaseIdentifier) ?? rhsPrereleaseIdentifier

switch (typedLhsIdentifier, typedRhsIdentifier) {
case let (int1 as Int, int2 as Int): return int1 < int2
case let (string1 as String, string2 as String): return string1 < string2
case (is Int, is String): return true // Int prereleases < String prereleases
case (is String, is Int): return false
default:
fatalError("impossi-op")
}
}

return lhs.prereleaseIdentifiers.count < rhs.prereleaseIdentifiers.count
}
}
1 change: 1 addition & 0 deletions Sources/Version/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
https://github.com/mxcl/Version/releases/tag/2.1.0
Version+Comparable.swift merged into Version.swift.

0 comments on commit bc9db42

Please sign in to comment.