-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Added code to detect UTF32 files for NSString contentsOfURL:usedEncoding #928
Conversation
Please can you squash the history of this, so that we don't get noisy messages like "Resolved merge conflicts"? and instead of having "Merge remote-tracking branch X" messages, you can do a You can also configure the 'rebase' to be the default by doing |
@alblue usually any merges we accept are squash merged anyhow as a default rule so that isn't too bad. |
@@ -462,6 +462,8 @@ | |||
'TestFoundation/Resources/NSStringTestData.txt', | |||
'TestFoundation/Resources/NSString-UTF16-BE-data.txt', | |||
'TestFoundation/Resources/NSString-UTF16-LE-data.txt', | |||
'TestFoundation/Resources/NSString-UTF32-BE-data.txt', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these files missing from the commit? I don't see them currently on master
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're actually there. Because these files have BOM in them, Github is detecting them to be binary files. If you go ahead and click on "Files Changed" above, you would see them listed as binary files..
XCTFail("Unable to init NSString from contentOf:usedEncoding:") | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these pass on macOS using the system Foundation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohitathwani can you double check on this part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foundation/NSString.swift
Outdated
@@ -1301,12 +1301,18 @@ extension NSString { | |||
let readResult = try NSData(contentsOf: url, options:[]) | |||
|
|||
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity:readResult.length) | |||
if readResult.length >= 2 && bytePtr[0] == 254 && bytePtr[1] == 255 { | |||
if readResult.length >= 4 && bytePtr[0] == 255 && bytePtr[1] == 254 && bytePtr[2] == 0 && bytePtr[3] == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be good to use the hex values for these since most sources of the unicode standards refer to 0xFF and 0xFE etc. (this is just a style nit not a functionality commentary)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated my code with hex values
Foundation/NSString.swift
Outdated
enc?.pointee = String.Encoding.utf16BigEndian.rawValue | ||
} | ||
else if readResult.length >= 2 && bytePtr[0] == 255 && bytePtr[1] == 254 { | ||
enc?.pointee = String.Encoding.utf16LittleEndian.rawValue | ||
} | ||
else if readResult.length >= 4 && bytePtr[0] == 0 && bytePtr[1] == 0 && bytePtr[2] == 254 && bytePtr[3] == 255 { | ||
enc?.pointee = String.Encoding.utf32BigEndian.rawValue | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We seem to be using some Int
literals. Will it make sense to replace them with constants so that their contextual meaning is clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of naming conventions do you suggest for these literals?
f59f577
to
fcd4693
Compare
…L:usedEncoding: This pull request is in continuation to PR swiftlang#893
fcd4693
to
18cafb1
Compare
@swift-ci please test |
This looks good to me - @phausler WDYT? |
looks good to me @swift-ci please test and merge |
This pull request is in continuation to PR #893