Skip to content
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

Cannot instantiate String with Data with encoding nonLossyASCII #929

Closed
Frizlab opened this issue Sep 19, 2024 · 4 comments · Fixed by #932
Closed

Cannot instantiate String with Data with encoding nonLossyASCII #929

Frizlab opened this issue Sep 19, 2024 · 4 comments · Fixed by #932
Assignees

Comments

@Frizlab
Copy link

Frizlab commented Sep 19, 2024

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

jmschonfeld commented Sep 20, 2024

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

Frizlab commented Sep 24, 2024

@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
Copy link
Contributor

@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
Copy link
Author

Frizlab commented Sep 24, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants