1
- //===--------------- Triple.swift - Swift Target Triples ------------------===//
1
+ //===---------------------------------------------------- ------------------===//
2
2
//
3
- // This source file is part of the Swift.org open source project
3
+ // This source file is part of the Swift open source project
4
4
//
5
- // Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
- // See https ://swift.org/LICENSE.txt for license information
9
- // See https ://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8
+ // See http ://swift.org/LICENSE.txt for license information
9
+ // See http ://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
@@ -72,14 +72,16 @@ public struct Triple {
72
72
73
73
/// Represents a version that may be present in the target triple.
74
74
public struct Version : Equatable , Comparable , CustomStringConvertible {
75
- public static let zero = Version ( 0 , 0 , 0 )
76
-
77
75
public var major : Int
78
76
public var minor : Int
79
77
public var micro : Int
80
78
81
- public init < S: StringProtocol > ( parse string: S ) {
82
- let components = string. split ( separator: " . " , maxSplits: 3 ) . map { Int ( $0) ?? 0 }
79
+ public init ? ( parse string: some StringProtocol ) {
80
+ guard !string. isEmpty else {
81
+ return nil
82
+ }
83
+
84
+ let components = string. split ( separator: " . " , maxSplits: 3 ) . map { Int ( $0) ?? 0 }
83
85
self . major = components. count > 0 ? components [ 0 ] : 0
84
86
self . minor = components. count > 1 ? components [ 1 ] : 0
85
87
self . micro = components. count > 2 ? components [ 2 ] : 0
@@ -162,10 +164,9 @@ public struct Triple {
162
164
163
165
// Now that we've parsed everything, we construct a normalized form of the
164
166
// triple string.
165
- triple = parser. components. map ( { $0. isEmpty ? " unknown " : $0 } ) . joined ( separator: " - " )
166
- }
167
- else {
168
- triple = string
167
+ self . triple = parser. components. map { $0. isEmpty ? " unknown " : $0 } . joined ( separator: " - " )
168
+ } else {
169
+ self . triple = string
169
170
}
170
171
171
172
// Unpack the parsed data into the fields. If no environment info was found,
@@ -1526,7 +1527,7 @@ extension Triple {
1526
1527
/// `darwin` OS version number is not adjusted to match the equivalent
1527
1528
/// `macosx` version number. It's usually better to use `version(for:)`
1528
1529
/// to get Darwin versions.
1529
- public var osVersion : Version {
1530
+ public var osVersion : Version ? {
1530
1531
var osName = self . osName [ ... ]
1531
1532
1532
1533
// Assume that the OS portion of the triple starts with the canonical name.
@@ -1569,7 +1570,9 @@ extension Triple {
1569
1570
/// This accessor is semi-private; it's typically better to use `version(for:)` or
1570
1571
/// `Triple.FeatureAvailability`.
1571
1572
public var _macOSVersion : Version ? {
1572
- var version = osVersion
1573
+ guard var version = osVersion else {
1574
+ return nil
1575
+ }
1573
1576
1574
1577
switch os {
1575
1578
case . darwin:
@@ -1623,7 +1626,7 @@ extension Triple {
1623
1626
///
1624
1627
/// This accessor is semi-private; it's typically better to use `version(for:)` or
1625
1628
/// `Triple.FeatureAvailability`.
1626
- public var _iOSVersion : Version {
1629
+ public var _iOSVersion : Version ? {
1627
1630
switch os {
1628
1631
case . darwin, . macosx:
1629
1632
// Ignore the version from the triple. This is only handled because the
@@ -1632,7 +1635,7 @@ extension Triple {
1632
1635
// OS X.
1633
1636
return Version ( 5 , 0 , 0 )
1634
1637
case . ios, . tvos:
1635
- var version = self . osVersion
1638
+ guard var version = self . osVersion else { return nil }
1636
1639
// Default to 5.0 (or 7.0 for arm64).
1637
1640
if version. major == 0 {
1638
1641
version. major = arch == . aarch64 ? 7 : 5
@@ -1650,7 +1653,7 @@ extension Triple {
1650
1653
///
1651
1654
/// This accessor is semi-private; it's typically better to use `version(for:)` or
1652
1655
/// `Triple.FeatureAvailability`.
1653
- public var _watchOSVersion : Version {
1656
+ public var _watchOSVersion : Version ? {
1654
1657
switch os {
1655
1658
case . darwin, . macosx:
1656
1659
// Ignore the version from the triple. This is only handled because the
@@ -1659,7 +1662,7 @@ extension Triple {
1659
1662
// OS X.
1660
1663
return Version ( 2 , 0 , 0 )
1661
1664
case . watchos:
1662
- var version = self . osVersion
1665
+ guard var version = self . osVersion else { return nil }
1663
1666
if version. major == 0 {
1664
1667
version. major = 2
1665
1668
}
0 commit comments