Skip to content

Commit f817803

Browse files
committed
Conform fixed-width integer types to LosslessStringConvertible
1 parent 7509e9b commit f817803

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

stdlib/public/core/IntegerParsing.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension FixedWidthInteger {
102102
///
103103
/// The string passed as `text` may begin with a plus or minus sign character
104104
/// (`+` or `-`), followed by one or more numeric digits (`0-9`) or letters
105-
/// (`a-z` or `A-Z`). The string is case insensitive.
105+
/// (`a-z` or `A-Z`). The string is case-insensitive.
106106
///
107107
/// let x = Int("123")
108108
/// // x == 123
@@ -155,4 +155,27 @@ extension FixedWidthInteger {
155155
guard _fastPath(result != nil) else { return nil }
156156
self = result!
157157
}
158+
159+
/// Creates a new integer value from the given string.
160+
///
161+
/// The string passed as `description` may begin with a plus or minus sign
162+
/// character (`+` or `-`), followed by one or more numeric digits (`0-9`).
163+
///
164+
/// let x = Int("123")
165+
/// // x == 123
166+
///
167+
/// If `description` is in an invalid format, or if the value it denotes in
168+
/// base 10 is not representable, the result is `nil`. For example, the
169+
/// following conversions result in `nil`:
170+
///
171+
/// Int(" 100") // Includes whitespace
172+
/// Int("21-50") // Invalid format
173+
/// Int("ff6600") // Characters out of bounds
174+
///
175+
/// - Parameter description: The ASCII representation of a number in base 10.
176+
@_semantics("optimize.sil.specialize.generic.partial.never")
177+
@inline(__always)
178+
public init?(_ description: String) {
179+
self.init(description, radix: 10)
180+
}
158181
}

stdlib/public/core/Integers.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,8 @@ public enum ArithmeticOverflow {
19691969
/// customization points for arithmetic operations. When you provide just those
19701970
/// methods, the standard library provides default implementations for all
19711971
/// other arithmetic methods and operators.
1972-
public protocol FixedWidthInteger : BinaryInteger, _BitwiseOperations
1972+
public protocol FixedWidthInteger :
1973+
BinaryInteger, LosslessStringConvertible, _BitwiseOperations
19731974
where Magnitude : FixedWidthInteger
19741975
{
19751976
/// The number of bits used for the underlying binary representation of

0 commit comments

Comments
 (0)