@@ -53,63 +53,65 @@ extension ${Self}: LosslessStringConvertible {
53
53
/// Creates a new instance from the given string.
54
54
///
55
55
/// The string passed as `text` can represent a real number in decimal or
56
- /// hexadecimal format or special floating-point values for infinity and NaN
57
- /// ("not a number").
56
+ /// hexadecimal format or can be in a special format representing infinity
57
+ /// or NaN ("not a number"). If `text` is not in a recognized format,
58
+ /// the optional initializer will fail and return `nil`.
58
59
///
59
- /// The given string may begin with a plus or minus sign character (`+` or
60
- /// `-`). The allowed formats for each of these representations is then as
61
- /// follows :
60
+ /// The `text` string consists of an optional
61
+ /// plus or minus sign character (`+` or `-`)
62
+ /// followed by one of the following :
62
63
///
63
- /// - A *decimal value * contains the significand, a sequence of decimal
64
- /// digits that may include a decimal point.
64
+ /// - A *decimal string * contains a significand consisting of one
65
+ /// or more decimal digits that may include a decimal point:
65
66
///
66
67
/// let c = ${Self}("-1.0")
67
68
/// // c == -1.0
68
69
///
69
70
/// let d = ${Self}("28.375")
70
71
/// // d == 28.375
71
72
///
72
- /// A decimal value may also include an exponent following the significand,
73
- /// indicating the power of 10 by which the significand should be
74
- /// multiplied. If included, the exponent is separated by a single
73
+ /// A decimal string may also include an exponent following the
74
+ /// significand, indicating the power of 10 by which the significand should
75
+ /// be multiplied. If included, the exponent is separated by a single
75
76
/// character, `e` or `E`, and consists of an optional plus or minus sign
76
77
/// character and a sequence of decimal digits.
77
78
///
78
79
/// let e = ${Self}("2837.5e-2")
79
80
/// // e == 28.375
80
81
///
81
- /// - A *hexadecimal value * contains the significand, either `0X` or `0x`,
82
- /// followed by a sequence of hexadecimal digits. The significand may
82
+ /// - A *hexadecimal string * contains a significand consisting of
83
+ /// `0X` or `0x` followed by one or more hexadecimal digits that may
83
84
/// include a decimal point.
84
85
///
85
86
/// let f = ${Self}("0x1c.6")
86
87
/// // f == 28.375
87
88
///
88
- /// A hexadecimal value may also include an exponent following the
89
- /// significand, indicating the power of 2 by which the significand should
89
+ /// A hexadecimal string may also include an exponent
90
+ /// indicating the power of 2 by which the significand should
90
91
/// be multiplied. If included, the exponent is separated by a single
91
92
/// character, `p` or `P`, and consists of an optional plus or minus sign
92
93
/// character and a sequence of decimal digits.
93
94
///
94
95
/// let g = ${Self}("0x1.c6p4")
95
96
/// // g == 28.375
96
97
///
97
- /// - A value of *infinity* contains one of the strings `"inf"` or
98
- /// `"infinity"`, case insensitive.
98
+ /// - The input strings `"inf"` or `"infinity"` (case insensitive)
99
+ /// are converted to an infinite result:
99
100
///
100
101
/// let i = ${Self}("inf")
101
102
/// // i == ${Self}.infinity
102
103
///
103
104
/// let j = ${Self}("-Infinity")
104
105
/// // j == -${Self}.infinity
105
106
///
106
- /// - A value of *NaN* contains the string `"nan"`, case insensitive.
107
+ /// - An input string of `"nan"` (case insensitive) is converted
108
+ /// into a *NaN* value:
107
109
///
108
110
/// let n = ${Self}("-nan")
109
111
/// // n?.isNaN == true
110
112
/// // n?.sign == .minus
111
113
///
112
- /// A NaN value may also include a payload in parentheses following the
114
+ /// A NaN string may also include a payload in parentheses following the
113
115
/// `"nan"` keyword. The payload consists of a sequence of decimal digits,
114
116
/// or the characters `0X` or `0x` followed by a sequence of hexadecimal
115
117
/// digits. If the payload contains any other characters, it is ignored.
@@ -120,16 +122,44 @@ extension ${Self}: LosslessStringConvertible {
120
122
/// // p?.isNaN == true
121
123
/// // String(p!) == "nan(0x10)"
122
124
///
123
- /// Passing any other format or any additional characters as `text` results
124
- /// in `nil`. For example, the following conversions result in `nil`:
125
+ /// A string in any other format than those described above
126
+ /// or containing additional characters
127
+ /// results in a `nil` value. For example, the following conversions
128
+ /// result in `nil`:
129
+ ///
130
+ /// ${Self}(" 5.0") // Includes whitespace
131
+ /// ${Self}("±2.0") // Invalid character
132
+ /// ${Self}("0x1.25e4") // Incorrect exponent format
133
+ ///
134
+ /// A decimal or hexadecimal string is converted to a `${Self}`
135
+ /// instance using the IEEE 754 roundTiesToEven (default) rounding
136
+ /// attribute.
137
+ /// Values with absolute value smaller than `${Self}.leastNonzeroMagnitude`
138
+ /// are rounded to plus or minus zero.
139
+ /// Values with absolute value larger than `${Self}.greatestFiniteMagnitude`
140
+ /// are rounded to plus or minus infinity.
141
+ ///
142
+ /// let y = ${Self}("1.23e-9999")
143
+ /// // y == 0.0
144
+ /// // y?.sign == .plus
145
+ ///
146
+ /// let z = ${Self}("-7.89e-7206")
147
+ /// // z == -0.0
148
+ /// // z?.sign == .minus
149
+ ///
150
+ /// let r = ${Self}("1.23e17802")
151
+ /// // r == ${Self}.infinity
152
+ ///
153
+ /// let s = ${Self}("-7.89e7206")
154
+ /// // s == ${Self}.-infinity
155
+ ///
156
+ /// - Note: Prior to Swift 5.4, a decimal or
157
+ /// hexadecimal input string whose value was too large to represent
158
+ /// as a finite `${Self}` instance returned `nil` instead of
159
+ /// `${Self}.infinity`.
125
160
///
126
- /// ${Self}(" 5.0") // Includes whitespace
127
- /// ${Self}("±2.0") // Invalid character
128
- /// ${Self}("0x1.25e4") // Incorrect exponent format
161
+ /// - Parameter text: An input string to convert to a `${Self}?` instance.
129
162
///
130
- /// - Parameter text: The input string to convert to a `${Self}` instance. If
131
- /// `text` has invalid characters or is in an invalid format, the result
132
- /// is `nil`.
133
163
@inlinable
134
164
public init?<S: StringProtocol>(_ text: S) {
135
165
%if bits == 16:
0 commit comments