Ferrite-backup/Ferrite/Extensions/String.swift
kingbri 39c4a10a72 Ferrite: format
Signed-off-by: kingbri <bdashore3@proton.me>
2023-01-07 18:47:27 -05:00

51 lines
1.8 KiB
Swift

//
// String.swift
// Ferrite
//
// Created by Brian Dashore on 8/31/22.
//
//
import Foundation
extension String {
// From https://www.hackingwithswift.com/example-code/strings/how-to-capitalize-the-first-letter-of-a-string
func capitalizingFirstLetter() -> String {
prefix(1).capitalized + dropFirst()
}
mutating func capitalizeFirstLetter() {
self = capitalizingFirstLetter()
}
// From https://stackoverflow.com/a/59307884
private func compare(toVersion targetVersion: String) -> ComparisonResult {
let versionDelimiter = "."
var result: ComparisonResult = .orderedSame
var versionComponents = components(separatedBy: versionDelimiter)
var targetComponents = targetVersion.components(separatedBy: versionDelimiter)
while versionComponents.count < targetComponents.count {
versionComponents.append("0")
}
while targetComponents.count < versionComponents.count {
targetComponents.append("0")
}
for (version, target) in zip(versionComponents, targetComponents) {
result = version.compare(target, options: .numeric)
if result != .orderedSame {
break
}
}
return result
}
static func == (lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedSame }
static func < (lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedAscending }
static func <= (lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) != .orderedDescending }
static func > (lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) == .orderedDescending }
static func >= (lhs: String, rhs: String) -> Bool { lhs.compare(toVersion: rhs) != .orderedAscending }
}