swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.36k stars 150 forks source link

Cannot instantiate String with Data with encoding nonLossyASCII #929

Closed Frizlab closed 8 hours ago

Frizlab commented 1 day ago

The following code returns nil with Swift 6.0 and Swift 6.0-snapshot-2024-09-17 on Linux, but returns the proper String on macOS: String(data: Data(#"yolo"#.utf8), encoding: .nonLossyASCII). I believe macOS is in the right.

jmschonfeld commented 7 hours ago

I've merged the fix to main. As a workaround for now on Linux, you can use .ascii instead of .nonLossyASCII - decoding bytes with ASCII is always considered lossy, nonLossyASCII only has different effects when converting a string to bytes