Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 11 additions & 5 deletions stdlib/public/core/FloatingPointParsing.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

@glessard glessard Dec 17, 2025

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?

#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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 withFastUTF8?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation does always produce a string that supports withFastUTF8, but we might as well be sure and call makeContiguousUTF8(). I would suggest withUTF8, since it is a wrapper for makeContiguousUTF8() followed by _guts.withFastUTF8

unsafe parse_float${bits}(chars.span)
}
}

if let parsed {
Expand Down
8 changes: 8 additions & 0 deletions test/stdlib/ParseFloat64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to test bridged strings specially here?

Copy link
Contributor

Choose a reason for hiding this comment

The 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>>,
Expand Down