Skip to content

Commit

Permalink
Add support for modern Swift regex, when available
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseplettsonos committed Nov 3, 2023
1 parent 0f1351b commit e811c0e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Sources/SemanticVersion/SemanticVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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'
(?<major>0|[1-9]\d*)
\.
(?<minor>0|[1-9]\d*)
\.
(?<patch>0|[1-9]\d*)
(?:-
(?<prerelease>
(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
(?:\.
(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
)
*)
)?
(?:\+
(?<buildmetadata>[0-9a-zA-Z-]+
(?:\.[0-9a-zA-Z-]+)
*)
)?
$
/##
#endif

let semVerRegex = NSRegularExpression(#"""
^
v? # SPI extension: allow leading 'v'
Expand Down
79 changes: 79 additions & 0 deletions Tests/SemanticVersionTests/SemanticVersionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit e811c0e

Please sign in to comment.