Skip to content

Commit

Permalink
Implement version comparison in Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefizso committed Oct 19, 2023
1 parent 09fee5f commit 2c0f5e9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 5 deletions.
93 changes: 89 additions & 4 deletions Sparkle/SUStandardVersionComparator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,96 @@ public class SUStandardVersionComparator: NSObject, SUVersionComparison {
@return A comparison result between @c versionA and @c versionB
*/
public func compareVersion(_ versionA: String, toVersion versionB: String) -> ComparisonResult {
var splitPartsA = self.splitVersion(string: versionA)
var splitPartsB = self.splitVersion(string: versionB)
var partsA = self.splitVersion(string: versionA)
var partsB = self.splitVersion(string: versionB)

self.balanceVersionParts(partsA: &splitPartsA, partsB: &splitPartsB)

self.balanceVersionParts(partsA: &partsA, partsB: &partsB)

let count = min(partsA.count, partsB.count)

for i in 0 ..< count {
let partA = partsA[i]
let partB = partsB[i]

let typeA = self.typeOfCharacter(partA)
let typeB = self.typeOfCharacter(partB)

// Compare types
if self.isEqualCharacterTypeClassForTypeA(typeA: typeA, typeB: typeB) {
// Same type; we can compare
if typeA == .numberType {
let valueA = Int64(partA) ?? 0
let valueB = Int64(partB) ?? 0

if valueA > valueB {
return .orderedDescending
}
else if valueA < valueB {
return .orderedAscending
}
}
else if typeA == .stringType {
let result = partA.compare(partB)
if result != .orderedSame {
return result
}
}
}
else {
// Not the same type? Now we have to do some validity checking
if typeA != .stringType && typeB == .stringType {
// typeA wins
return .orderedDescending
}
else if typeA == .stringType && typeB != .stringType {
// typeB wins
return .orderedAscending
}
else {
// One is a number and the other is a period. The period is invalid
if typeA == .numberType {
return .orderedDescending
}
else {
return .orderedAscending
}
}
}
}

// The versions are equal up to the point where they both still have parts
// Lets check to see if one is larger than the other
if partsA.count != partsB.count {
// Yep. Lets get the next part of the larger
// n holds the index of the part we want.
var missingPart: String
var shorterResult: ComparisonResult
var largerResult: ComparisonResult

if partsA.count > partsB.count {
missingPart = partsA[count]
shorterResult = .orderedAscending
largerResult = .orderedDescending
}
else {
missingPart = partsB[count]
shorterResult = .orderedDescending
largerResult = .orderedAscending
}

let missingType = self.typeOfCharacter(missingPart)
// Check the type
if missingType == .stringType {
// It's a string. Shorter version wins
return shorterResult
}
else {
// It's a number/period. Larger version wins
return largerResult
}
}

// The 2 strings are identical
return .orderedSame
}
}
23 changes: 22 additions & 1 deletion SparkleTestsObjc/SUStandardVersionComparatorTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ - (void)test_defaultComparator_comparatorIsAccessibleFromObjC
XCTAssertNotNil(comparator);
}


- (void)test_compareVersion_sameNumericVersionsAreEqual
{
SUStandardVersionComparator *comparator = [[SUStandardVersionComparator alloc] init];
Expand All @@ -46,5 +45,27 @@ - (void)test_compareVersion_sameNumericVersionsAreEqual
XCTAssertEqual(actualResult, NSOrderedSame);
}

- (void)test_compareVersion_newerVersionIsAscending
{
SUStandardVersionComparator *comparator = [[SUStandardVersionComparator alloc] init];

// Act
NSComparisonResult actualResult = [comparator compareVersion:@"1.0" toVersion:@"1.1"];

// Assert
XCTAssertEqual(actualResult, NSOrderedAscending);
}

- (void)test_compareVersion_olderVersionIsDescending
{
SUStandardVersionComparator *comparator = [[SUStandardVersionComparator alloc] init];

// Act
NSComparisonResult actualResult = [comparator compareVersion:@"12.5" toVersion:@"12.4"];

// Assert
XCTAssertEqual(actualResult, NSOrderedDescending);
}


@end

0 comments on commit 2c0f5e9

Please sign in to comment.