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

[SR-7620] String(contentsOf: url) not working on Linux #3707

Closed
swift-ci opened this issue May 7, 2018 · 5 comments
Closed

[SR-7620] String(contentsOf: url) not working on Linux #3707

swift-ci opened this issue May 7, 2018 · 5 comments
Assignees

Comments

@swift-ci
Copy link
Contributor

swift-ci commented May 7, 2018

Previous ID SR-7620
Radar None
Original Reporter sdidanov (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Ubuntu 16.04(with last updates), Swift 4.1.1

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @spevans
Priority Medium

md5: 3d20f9724259d3d924f97a21c1420271

Issue Description:

The following code:

import Foundation
do {
 let url = URL(string: "https://www.google.com")!
 let htmlString = try String(contentsOf: url, encoding: .ascii)
 print("Start", htmlString, "Finish")
}
catch {
 print(error)
}

On swift3 returns google page. On Swift 4 returns:

$ swift test.swift 
Start Finish
@belkadan
Copy link

belkadan commented May 7, 2018

@millenomi, @phausler, any idea what would have changed?

@spevans
Copy link
Collaborator

spevans commented May 28, 2018

I just tested this with swift-DEVELOPMENT-SNAPSHOT-2018-05-26-a on Ubuntu 16.04 and it works fine. Note that https://www.google.com is actually served with charset=ISO-8859-1 and it does contain non-ASCII bytes in the data.

However String(contentsOf: url) doesnt work because it tries to determine the encoding by looking for a BOM (Byte Order Mark) in the first few bytes for UTF-16/UTF-32, falling back to UTF-8.
This page won't decode as UTF-8, which is correct, so I have written a PR that fixes NSString(contentsOf:usedEncoding: )
(the underlying call) to use the Content-Type header if available to find the encoding to use.

#1565

@swift-ci
Copy link
Contributor Author

Comment by Sergey Didanov (JIRA)

Confirm. It works correct on last development snapshot.

@swift-ci
Copy link
Contributor Author

swift-ci commented Jun 7, 2018

Comment by Sergey Didanov (JIRA)

Hi,

In what release this fix will be?

@spevans
Copy link
Collaborator

spevans commented Aug 3, 2018

This has been merged into the 4.2 branch in PR #1615

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants