swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.4k stars 160 forks source link

Cannot instantiate String with Data with encoding nonLossyASCII #929

Closed Frizlab closed 1 month ago

Frizlab commented 1 month 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 1 month 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

Frizlab commented 1 month ago

@jmschonfeld I don’t think that’s true:

print(String(data: Data("yolo\\012".utf8), encoding: .nonLossyASCII))
print(String(data: Data("yolo\\012".utf8), encoding: .ascii))

First version prints yolo\n the other prints yolo\\012 on macOS.

jmschonfeld commented 1 month ago

@Frizlab which macOS version are you testing this on? There were some slight behavioral updates to this function in macOS 15 when the function was re-implemented in Swift rather than bridging to NSString where this behavior is perhaps coming from. However, that behavior does seem quite strange - I'm not sure where the newline character would be coming from

Frizlab commented 1 month ago

@jmschonfeld

macOS 15
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0

Also on macOS 14.7 w/ Xcode 15.2.

12 in octal is a newline in ascii.

Interestingly, "yélo".data(using: .nonLossyASCII) works on macOS 14 (returns the Data for the literal string y\351lo) but on macOS 15 it returns nil.