Skip to content

Commit 512be2c

Browse files
authored
Merge pull request swiftlang#36945 from tbkka/tbkka/rdar76728925
2 parents b65777e + 4b2f487 commit 512be2c

File tree

1 file changed

+56
-26
lines changed

1 file changed

+56
-26
lines changed

stdlib/public/core/FloatingPointParsing.swift.gyb

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,63 +53,65 @@ extension ${Self}: LosslessStringConvertible {
5353
/// Creates a new instance from the given string.
5454
///
5555
/// 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`.
5859
///
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:
6263
///
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:
6566
///
6667
/// let c = ${Self}("-1.0")
6768
/// // c == -1.0
6869
///
6970
/// let d = ${Self}("28.375")
7071
/// // d == 28.375
7172
///
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
7576
/// character, `e` or `E`, and consists of an optional plus or minus sign
7677
/// character and a sequence of decimal digits.
7778
///
7879
/// let e = ${Self}("2837.5e-2")
7980
/// // e == 28.375
8081
///
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
8384
/// include a decimal point.
8485
///
8586
/// let f = ${Self}("0x1c.6")
8687
/// // f == 28.375
8788
///
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
9091
/// be multiplied. If included, the exponent is separated by a single
9192
/// character, `p` or `P`, and consists of an optional plus or minus sign
9293
/// character and a sequence of decimal digits.
9394
///
9495
/// let g = ${Self}("0x1.c6p4")
9596
/// // g == 28.375
9697
///
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:
99100
///
100101
/// let i = ${Self}("inf")
101102
/// // i == ${Self}.infinity
102103
///
103104
/// let j = ${Self}("-Infinity")
104105
/// // j == -${Self}.infinity
105106
///
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:
107109
///
108110
/// let n = ${Self}("-nan")
109111
/// // n?.isNaN == true
110112
/// // n?.sign == .minus
111113
///
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
113115
/// `"nan"` keyword. The payload consists of a sequence of decimal digits,
114116
/// or the characters `0X` or `0x` followed by a sequence of hexadecimal
115117
/// digits. If the payload contains any other characters, it is ignored.
@@ -120,16 +122,44 @@ extension ${Self}: LosslessStringConvertible {
120122
/// // p?.isNaN == true
121123
/// // String(p!) == "nan(0x10)"
122124
///
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`.
125160
///
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.
129162
///
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`.
133163
@inlinable
134164
public init?<S: StringProtocol>(_ text: S) {
135165
%if bits == 16:

0 commit comments

Comments
 (0)