From b5ecdc7fae97d1f4c9b562fefc313bd5232c675e Mon Sep 17 00:00:00 2001 From: Chris Eplett Date: Fri, 3 Nov 2023 10:40:51 -0400 Subject: [PATCH] Add support for modern Swift regex, when available --- Sources/SemanticVersion/SemanticVersion.swift | 51 ++++++++++++ .../SemanticVersionTests.swift | 79 +++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/Sources/SemanticVersion/SemanticVersion.swift b/Sources/SemanticVersion/SemanticVersion.swift index f772b81..8f87cff 100644 --- a/Sources/SemanticVersion/SemanticVersion.swift +++ b/Sources/SemanticVersion/SemanticVersion.swift @@ -56,6 +56,27 @@ extension SemanticVersion: LosslessStringConvertible { /// Initialize a version from a string. Returns `nil` if the string is not a semantic version. /// - Parameter string: Version string. public init?(_ string: String) { +#if swift(>=5.7) + if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) { + guard let match = string.wholeMatch(of: semVerPattern), + let major = Int(match.output.major), + let minor = Int(match.output.minor), + let patch = Int(match.output.patch) + else { return nil } + let preRelease = match.output.prerelease.flatMap({String($0)}) ?? "" + let build = match.output.buildmetadata.flatMap({String($0)}) ?? "" + self = .init(major, minor, patch, preRelease, build) + } else { + let groups = semVerRegex.matchGroups(string) + guard + groups.count == semVerRegex.numberOfCaptureGroups, + let major = Int(groups[0]), + let minor = Int(groups[1]), + let patch = Int(groups[2]) + else { return nil } + self = .init(major, minor, patch, groups[3], groups[4]) + } +#else let groups = semVerRegex.matchGroups(string) guard groups.count == semVerRegex.numberOfCaptureGroups, @@ -64,6 +85,7 @@ extension SemanticVersion: LosslessStringConvertible { let patch = Int(groups[2]) else { return nil } self = .init(major, minor, patch, groups[3], groups[4]) +#endif } public var description: String { @@ -163,8 +185,37 @@ extension SemanticVersion: Sendable {} // Source: https://regex101.com/r/Ly7O1x/3/ // Linked from https://semver.org + + #if swift(>=5) +#if swift(>=5.7) +@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) +let semVerPattern = ##/ +^ +v? # SPI extension: allow leading 'v' +(?0|[1-9]\d*) +\. +(?0|[1-9]\d*) +\. +(?0|[1-9]\d*) +(?:- + (? + (?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*) + (?:\. + (?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*) + ) + *) +)? +(?:\+ + (?[0-9a-zA-Z-]+ + (?:\.[0-9a-zA-Z-]+) + *) +)? +$ +/## +#endif + let semVerRegex = NSRegularExpression(#""" ^ v? # SPI extension: allow leading 'v' diff --git a/Tests/SemanticVersionTests/SemanticVersionTests.swift b/Tests/SemanticVersionTests/SemanticVersionTests.swift index a3ad6c6..f364ba3 100644 --- a/Tests/SemanticVersionTests/SemanticVersionTests.swift +++ b/Tests/SemanticVersionTests/SemanticVersionTests.swift @@ -17,6 +17,7 @@ import XCTest final class SemanticVersionTests: XCTestCase { + func test_semVerRegex_valid() throws { XCTAssert(semVerRegex.matches("0.0.4")) XCTAssert(semVerRegex.matches("1.2.3")) @@ -51,8 +52,44 @@ final class SemanticVersionTests: XCTestCase { XCTAssert(semVerRegex.matches("1.0.0-0A.is.legal")) } + func test_semVerPattern_valid() throws { + XCTAssert("0.0.4".contains(semVerPattern)) + XCTAssert("0.0.4".contains(semVerPattern)) + XCTAssert("1.2.3".contains(semVerPattern)) + XCTAssert("10.20.30".contains(semVerPattern)) + XCTAssert("1.1.2-prerelease+meta".contains(semVerPattern)) + XCTAssert("1.1.2+meta".contains(semVerPattern)) + XCTAssert("1.1.2+meta-valid".contains(semVerPattern)) + XCTAssert("1.0.0-alpha".contains(semVerPattern)) + XCTAssert("1.0.0-beta".contains(semVerPattern)) + XCTAssert("1.0.0-alpha.beta".contains(semVerPattern)) + XCTAssert("1.0.0-alpha.beta.1".contains(semVerPattern)) + XCTAssert("1.0.0-alpha.1".contains(semVerPattern)) + XCTAssert("1.0.0-alpha0.valid".contains(semVerPattern)) + XCTAssert("1.0.0-alpha.0valid".contains(semVerPattern)) + XCTAssert("1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay".contains(semVerPattern)) + XCTAssert("1.0.0-rc.1+build.1".contains(semVerPattern)) + XCTAssert("2.0.0-rc.1+build.123".contains(semVerPattern)) + XCTAssert("1.2.3-beta".contains(semVerPattern)) + XCTAssert("10.2.3-DEV-SNAPSHOT".contains(semVerPattern)) + XCTAssert("1.2.3-SNAPSHOT-123".contains(semVerPattern)) + XCTAssert("1.0.0".contains(semVerPattern)) + XCTAssert("2.0.0".contains(semVerPattern)) + XCTAssert("1.1.7".contains(semVerPattern)) + XCTAssert("2.0.0+build.1848".contains(semVerPattern)) + XCTAssert("2.0.1-alpha.1227".contains(semVerPattern)) + XCTAssert("1.0.0-alpha+beta".contains(semVerPattern)) + XCTAssert("1.2.3----RC-SNAPSHOT.12.9.1--.12+788".contains(semVerPattern)) + XCTAssert("1.2.3----R-S.12.9.1--.12+meta".contains(semVerPattern)) + XCTAssert("1.2.3----RC-SNAPSHOT.12.9.1--.12".contains(semVerPattern)) + XCTAssert("1.0.0+0.build.1-rc.10000aaa-kk-0.1".contains(semVerPattern)) + XCTAssert("99999999999999999999999.999999999999999999.99999999999999999".contains(semVerPattern)) + XCTAssert("1.0.0-0A.is.legal".contains(semVerPattern)) + } + func test_allow_leading_v() throws { XCTAssert(semVerRegex.matches("v0.0.4")) + XCTAssert("v0.0.4".contains(semVerPattern)) } func test_semVerRegex_invalid() throws { @@ -97,6 +134,48 @@ final class SemanticVersionTests: XCTestCase { XCTAssertFalse(semVerRegex.matches("9.8.7-whatever+meta+meta")) } + func test_semVerPattern_invalid() throws { + XCTAssertFalse("1".contains(semVerPattern)) + XCTAssertFalse("1.2".contains(semVerPattern)) + XCTAssertFalse("1.2.3-0123".contains(semVerPattern)) + XCTAssertFalse("1.2.3-0123.0123".contains(semVerPattern)) + XCTAssertFalse("1.1.2+.123".contains(semVerPattern)) + XCTAssertFalse("+invalid".contains(semVerPattern)) + XCTAssertFalse("-invalid".contains(semVerPattern)) + XCTAssertFalse("-invalid+invalid".contains(semVerPattern)) + XCTAssertFalse("-invalid.01".contains(semVerPattern)) + XCTAssertFalse("alpha".contains(semVerPattern)) + XCTAssertFalse("alpha.beta".contains(semVerPattern)) + XCTAssertFalse("alpha.beta.1".contains(semVerPattern)) + XCTAssertFalse("alpha.1".contains(semVerPattern)) + XCTAssertFalse("alpha+beta".contains(semVerPattern)) + XCTAssertFalse("alpha_beta".contains(semVerPattern)) + XCTAssertFalse("alpha.".contains(semVerPattern)) + XCTAssertFalse("alpha..".contains(semVerPattern)) + XCTAssertFalse("beta".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha_beta".contains(semVerPattern)) + XCTAssertFalse("-alpha.".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha..".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha..1".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha...1".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha....1".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha.....1".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha......1".contains(semVerPattern)) + XCTAssertFalse("1.0.0-alpha.......1".contains(semVerPattern)) + XCTAssertFalse("01.1.1".contains(semVerPattern)) + XCTAssertFalse("1.01.1".contains(semVerPattern)) + XCTAssertFalse("1.1.01".contains(semVerPattern)) + XCTAssertFalse("1.2".contains(semVerPattern)) + XCTAssertFalse("1.2.3.DEV".contains(semVerPattern)) + XCTAssertFalse("1.2-SNAPSHOT".contains(semVerPattern)) + XCTAssertFalse("1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788".contains(semVerPattern)) + XCTAssertFalse("1.2-RC-SNAPSHOT".contains(semVerPattern)) + XCTAssertFalse("-1.0.3-gamma+b7718".contains(semVerPattern)) + XCTAssertFalse("+justmeta".contains(semVerPattern)) + XCTAssertFalse("9.8.7+meta+meta".contains(semVerPattern)) + XCTAssertFalse("9.8.7-whatever+meta+meta".contains(semVerPattern)) + } + func test_init() throws { XCTAssertEqual(SemanticVersion("1.2.3"), SemanticVersion(1, 2, 3)) XCTAssertEqual(SemanticVersion("v1.2.3"), SemanticVersion(1, 2, 3))