SPB Git

Repository Public MIT

SemanticVersion.swift 4.2 KB 96 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassRegistry/SemanticVersion.swift8// Module           : SPGlassRegistry9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// `MAJOR.MINOR.PATCH` with an optional pre-release (`1.2.0-beta.1`). Build metadata is not accepted.44public struct SemanticVersion: Hashable, Comparable, Sendable, CustomStringConvertible {45    public let major: Int46    public let minor: Int47    public let patch: Int48    public let prerelease: [String]4950    public init(major: Int, minor: Int, patch: Int, prerelease: [String] = []) {51        self.major = major; self.minor = minor; self.patch = patch; self.prerelease = prerelease52    }5354    public init(_ text: String) throws(RegistryError) {55        guard text.utf8.count <= 64, text.allSatisfy(\.isASCII) else { throw .invalidVersion(text) }56        let halves = text.split(separator: "-", maxSplits: 1, omittingEmptySubsequences: false)57        let numbers = halves[0].split(separator: ".", omittingEmptySubsequences: false)58        guard numbers.count == 3 else { throw .invalidVersion(text) }59        var values: [Int] = []60        for number in numbers {61            guard !number.isEmpty, number.count <= 9, number.allSatisfy(\.isNumber),62                  number == "0" || !number.hasPrefix("0"), let value = Int(number) else { throw .invalidVersion(text) }63            values.append(value)64        }65        var identifiers: [String] = []66        if halves.count == 2 {67            identifiers = halves[1].split(separator: ".", omittingEmptySubsequences: false).map(String.init)68            guard !identifiers.isEmpty, identifiers.allSatisfy({ !$0.isEmpty && $0.allSatisfy { $0.isLetter || $0.isNumber } }) else {69                throw .invalidVersion(text)70            }71        }72        self.init(major: values[0], minor: values[1], patch: values[2], prerelease: identifiers)73    }7475    public var description: String {76        "\(major).\(minor).\(patch)" + (prerelease.isEmpty ? "" : "-" + prerelease.joined(separator: "."))77    }7879    public static func < (lhs: SemanticVersion, rhs: SemanticVersion) -> Bool {80        if (lhs.major, lhs.minor, lhs.patch) != (rhs.major, rhs.minor, rhs.patch) {81            return (lhs.major, lhs.minor, lhs.patch) < (rhs.major, rhs.minor, rhs.patch)82        }83        // A version without pre-release is greater than the same version with one.84        if lhs.prerelease.isEmpty != rhs.prerelease.isEmpty { return !lhs.prerelease.isEmpty }85        for (a, b) in zip(lhs.prerelease, rhs.prerelease) where a != b {86            switch (Int(a), Int(b)) {87            case (let x?, let y?): return x < y88            case (_?, nil): return true89            case (nil, _?): return false90            default: return a < b91            }92        }93        return lhs.prerelease.count < rhs.prerelease.count94    }95}96
Files main
Menu

Theme