Skip to content

Commit

Permalink
Switch fully to Swift regex
Browse files Browse the repository at this point in the history
* Set minimum swift tools to 5.7
* Set appropriate min platform versions
* Remove all NSRegularExpression code
* Eliminate condtional compilation and @available checks, using Swift regex at all times
  • Loading branch information
chriseplettsonos committed Nov 17, 2023
1 parent cd49a16 commit 618c4a2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 299 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run-tests:
strategy:
matrix:
swift-version: [5.6, 5.7, 5.8, 5.9]
swift-version: [5.7, 5.8, 5.9]
runs-on: ubuntu-20.04
steps:
- uses: fwal/setup-swift@v1
Expand Down
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.6
// swift-tools-version:5.7

// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
Expand All @@ -18,6 +18,12 @@ import PackageDescription

let package = Package(
name: "SemanticVersion",
platforms: [
.iOS(.v16),
.macOS(.v13),
.watchOS(.v9),
.tvOS(.v16),
],
products: [
.library(
name: "SemanticVersion",
Expand Down
48 changes: 0 additions & 48 deletions Sources/SemanticVersion/NSRegularExpression+ext.swift

This file was deleted.

93 changes: 7 additions & 86 deletions Sources/SemanticVersion/SemanticVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,14 @@ 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,
let major = Int(groups[0]),
let minor = Int(groups[1]),
let patch = Int(groups[2])
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 }
self = .init(major, minor, patch, groups[3], groups[4])
#endif
let preRelease = match.output.prerelease.flatMap({String($0)}) ?? ""
let build = match.output.buildmetadata.flatMap({String($0)}) ?? ""
self = .init(major, minor, patch, preRelease, build)
}

public var description: String {
Expand Down Expand Up @@ -186,10 +164,6 @@ extension SemanticVersion: Sendable {}
// 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'
Expand All @@ -213,56 +187,3 @@ v? # SPI extension: allow leading 'v'
)?
$
/##
#endif

let semVerRegex = NSRegularExpression(#"""
^
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-]+)
*)
)?
$
"""#, options: [.allowCommentsAndWhitespace])

#else

let semVerRegex = NSRegularExpression("""
^
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-]+)
*)
)?
$
""", options: [.allowCommentsAndWhitespace])

#endif
Loading

0 comments on commit 618c4a2

Please sign in to comment.