1- //===--------------- Triple.swift - Swift Target Triples ------------------===//
1+ //===---------------------------------------------------- ------------------===//
22//
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
44//
5- // Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+ // Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
66// Licensed under Apache License v2.0 with Runtime Library Exception
77//
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
1010//
1111//===----------------------------------------------------------------------===//
1212
@@ -72,14 +72,16 @@ public struct Triple {
7272
7373 /// Represents a version that may be present in the target triple.
7474 public struct Version : Equatable , Comparable , CustomStringConvertible {
75- public static let zero = Version ( 0 , 0 , 0 )
76-
7775 public var major : Int
7876 public var minor : Int
7977 public var micro : Int
8078
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 }
8385 self . major = components. count > 0 ? components [ 0 ] : 0
8486 self . minor = components. count > 1 ? components [ 1 ] : 0
8587 self . micro = components. count > 2 ? components [ 2 ] : 0
@@ -162,10 +164,9 @@ public struct Triple {
162164
163165 // Now that we've parsed everything, we construct a normalized form of the
164166 // 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
169170 }
170171
171172 // Unpack the parsed data into the fields. If no environment info was found,
@@ -1526,7 +1527,7 @@ extension Triple {
15261527 /// `darwin` OS version number is not adjusted to match the equivalent
15271528 /// `macosx` version number. It's usually better to use `version(for:)`
15281529 /// to get Darwin versions.
1529- public var osVersion : Version {
1530+ public var osVersion : Version ? {
15301531 var osName = self . osName [ ... ]
15311532
15321533 // Assume that the OS portion of the triple starts with the canonical name.
@@ -1569,7 +1570,9 @@ extension Triple {
15691570 /// This accessor is semi-private; it's typically better to use `version(for:)` or
15701571 /// `Triple.FeatureAvailability`.
15711572 public var _macOSVersion : Version ? {
1572- var version = osVersion
1573+ guard var version = osVersion else {
1574+ return nil
1575+ }
15731576
15741577 switch os {
15751578 case . darwin:
@@ -1623,7 +1626,7 @@ extension Triple {
16231626 ///
16241627 /// This accessor is semi-private; it's typically better to use `version(for:)` or
16251628 /// `Triple.FeatureAvailability`.
1626- public var _iOSVersion : Version {
1629+ public var _iOSVersion : Version ? {
16271630 switch os {
16281631 case . darwin, . macosx:
16291632 // Ignore the version from the triple. This is only handled because the
@@ -1632,7 +1635,7 @@ extension Triple {
16321635 // OS X.
16331636 return Version ( 5 , 0 , 0 )
16341637 case . ios, . tvos:
1635- var version = self . osVersion
1638+ guard var version = self . osVersion else { return nil }
16361639 // Default to 5.0 (or 7.0 for arm64).
16371640 if version. major == 0 {
16381641 version. major = arch == . aarch64 ? 7 : 5
@@ -1650,7 +1653,7 @@ extension Triple {
16501653 ///
16511654 /// This accessor is semi-private; it's typically better to use `version(for:)` or
16521655 /// `Triple.FeatureAvailability`.
1653- public var _watchOSVersion : Version {
1656+ public var _watchOSVersion : Version ? {
16541657 switch os {
16551658 case . darwin, . macosx:
16561659 // Ignore the version from the triple. This is only handled because the
@@ -1659,7 +1662,7 @@ extension Triple {
16591662 // OS X.
16601663 return Version ( 2 , 0 , 0 )
16611664 case . watchos:
1662- var version = self . osVersion
1665+ guard var version = self . osVersion else { return nil }
16631666 if version. major == 0 {
16641667 version. major = 2
16651668 }
0 commit comments