-
Notifications
You must be signed in to change notification settings - Fork 10.6k
New FP Parsing: Use relevant part of a substring #86018
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -256,15 +256,21 @@ extension ${Self}: LosslessStringConvertible { | |
| // Use the all-Swift `parse_float${bits}()` implementation for Float16/32/64 | ||
| @available(SwiftStdlib 5.3, *) | ||
| public init?(_ text: Substring) { | ||
| // TODO: Someday, this whole function should simplify down to just: | ||
| // ${Self}(text.utf8.span) | ||
| #if _pointerBitWidth(_16) | ||
| // Always fail on 16-bit targets | ||
| return nil | ||
| #else | ||
| // Work around span availability limits | ||
| let parsed = unsafe text.base._guts.withFastUTF8 { chars -> ${Self}? in | ||
| unsafe parse_float${bits}(chars.span) | ||
| let parsed: ${Self}? | ||
| if text.base._guts.isFastUTF8 { | ||
| parsed = unsafe text.base._guts.withFastUTF8 { chars -> ${Self}? in | ||
| unsafe parse_float${bits}(chars.span.extracting(text._offsetRange)) | ||
| } | ||
| } else { | ||
| // Otherwise, make a copy so we have contiguous UTF8... | ||
| let string = String(text) | ||
| parsed = unsafe string._guts.withFastUTF8 { chars -> ${Self}? in | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In particular, can I reliably assume that a copied string will always support
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use https://developer.apple.com/documentation/swift/string/makecontiguousutf8() ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation does always produce a string that supports |
||
| unsafe parse_float${bits}(chars.span) | ||
| } | ||
| } | ||
|
|
||
| if let parsed { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -328,6 +328,14 @@ tests.test("Decimal Floats") { | |
| expectParse("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.999999999999999999999999999", Float64.infinity) | ||
| } | ||
|
|
||
| tests.test("Substring") { | ||
| let s1 = "1.02.03.0" | ||
| let s1sub = s1[s1.firstIndex(of: "2")!..<s1.firstIndex(of: "3")!] | ||
| let parsed = Float64(s1sub) | ||
| expectNotNil(parsed) | ||
| expectEqual(parsed!.bitPattern, (2.0).bitPattern) | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to test bridged strings specially here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not going to turn down free test coverage of my code if it's offered, but I don't think it's at all necessary. |
||
| @_extern(c, "_swift_stdlib_strtod_clocale") | ||
| func _swift_stdlib_strtod_clocale( | ||
| _: Optional<UnsafePointer<CChar>>, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we spell it that way for the platforms that don't have availability issues?