A small menubar app that allows you to switch between R versions quickly (if you have multiple versions of R framework installed).
https://rud.is/rswitch
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.9 KiB
42 lines
1.9 KiB
//
|
|
// String+Version.swift
|
|
// Pods
|
|
//
|
|
// Created by DragonCherry on 5/11/17.
|
|
//
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension String {
|
|
|
|
/// Inner comparison utility to handle same versions with different length. (Ex: "1.0.0" & "1.0")
|
|
private func compare(toVersion targetVersion: String) -> ComparisonResult {
|
|
|
|
let versionDelimiter = "."
|
|
var result: ComparisonResult = .orderedSame
|
|
var versionComponents = components(separatedBy: versionDelimiter)
|
|
var targetComponents = targetVersion.components(separatedBy: versionDelimiter)
|
|
let spareCount = versionComponents.count - targetComponents.count
|
|
|
|
if spareCount == 0 {
|
|
result = compare(targetVersion, options: .numeric)
|
|
} else {
|
|
let spareZeros = repeatElement("0", count: abs(spareCount))
|
|
if spareCount > 0 {
|
|
targetComponents.append(contentsOf: spareZeros)
|
|
} else {
|
|
versionComponents.append(contentsOf: spareZeros)
|
|
}
|
|
result = versionComponents.joined(separator: versionDelimiter)
|
|
.compare(targetComponents.joined(separator: versionDelimiter), options: .numeric)
|
|
}
|
|
return result
|
|
}
|
|
|
|
public func isVersion(equalTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedSame }
|
|
public func isVersion(greaterThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedDescending }
|
|
public func isVersion(greaterThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedAscending }
|
|
public func isVersion(lessThan targetVersion: String) -> Bool { return compare(toVersion: targetVersion) == .orderedAscending }
|
|
public func isVersion(lessThanOrEqualTo targetVersion: String) -> Bool { return compare(toVersion: targetVersion) != .orderedDescending }
|
|
}
|
|
|