Skip to content

Simplify {Float,Double,Float80}.init(_: String) #28992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions stdlib/public/core/FloatingPointParsing.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,38 @@ extension ${Self}: LosslessStringConvertible {
/// is `nil`.
@inlinable // FIXME(sil-serialize-all)
public init?<S: StringProtocol>(_ text: S) {
let u8 = text.utf8

let (result, n): (${Self}, Int) = text.withCString { chars in
let result: ${Self}? = text.withCString { chars in
// TODO: We should change the ABI for
// _swift_stdlib_strtoX_clocale so that it returns
// a boolean `false` for leading whitespace, empty
// string, or invalid character. Then we could avoid
// inlining these checks into every single client.
switch chars[0] {
case 9, 10, 11, 12, 13, 32:
// Reject any input with leading whitespace.
return nil
case 0:
// Reject the empty string
return nil
default:
break
}
var result: ${Self} = 0
let endPtr = withUnsafeMutablePointer(to: &result) {
_swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(chars, $0)
}
return (result, endPtr == nil ? 0 : endPtr! - chars)
// Verify that all the characters were consumed.
if endPtr == nil || endPtr![0] != 0 {
return nil
}
return result
}

if n == 0 || n != u8.count
|| u8.contains(where: { codeUnit in
// Check if the code unit is either non-ASCII or if isspace(codeUnit)
// would return nonzero when the current locale is the C locale.
codeUnit > 127 || "\t\n\u{b}\u{c}\r ".utf8.contains(codeUnit)
}) {
if let result = result {
self = result
} else {
return nil
}
self = result
}
}

Expand Down