Skip to content

Added code to detect UTF32 files for NSString contentsOfURL:usedEncoding #928

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

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] == 0xFF && bytePtr[1] == 0xFE && bytePtr[2] == 0x00 && bytePtr[3] == 0x00 {
enc?.pointee = String.Encoding.utf32LittleEndian.rawValue
}
else if readResult.length >= 2 && bytePtr[0] == 0xFE && bytePtr[1] == 0xFF {
enc?.pointee = String.Encoding.utf16BigEndian.rawValue
}
else if readResult.length >= 2 && bytePtr[0] == 255 && bytePtr[1] == 254 {
else if readResult.length >= 2 && bytePtr[0] == 0xFF && bytePtr[1] == 0xFE {
enc?.pointee = String.Encoding.utf16LittleEndian.rawValue
}
else if readResult.length >= 4 && bytePtr[0] == 0x00 && bytePtr[1] == 0x00 && bytePtr[2] == 0xFE && bytePtr[3] == 0xFF {
enc?.pointee = String.Encoding.utf32BigEndian.rawValue
}
else {
//Need to work on more conditions. This should be the default
enc?.pointee = String.Encoding.utf8.rawValue
Expand Down
Binary file added TestFoundation/Resources/NSString-UTF32-BE-data.txt
Binary file not shown.
Binary file added TestFoundation/Resources/NSString-UTF32-LE-data.txt
Binary file not shown.
34 changes: 34 additions & 0 deletions TestFoundation/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class TestNSString : XCTestCase {
("test_FromContentsOfURL",test_FromContentsOfURL),
("test_FromContentsOfURLUsedEncodingUTF16BE", test_FromContentsOfURLUsedEncodingUTF16BE),
("test_FromContentsOfURLUsedEncodingUTF16LE", test_FromContentsOfURLUsedEncodingUTF16LE),
("test_FromContentsOfURLUsedEncodingUTF32BE", test_FromContentsOfURLUsedEncodingUTF32BE),
("test_FromContentsOfURLUsedEncodingUTF32LE", test_FromContentsOfURLUsedEncodingUTF32LE),
("test_FromContentOfFile",test_FromContentOfFile),
("test_swiftStringUTF16", test_swiftStringUTF16),
// This test takes forever on build servers; it has been seen up to 1852.084 seconds
Expand Down Expand Up @@ -334,6 +336,38 @@ class TestNSString : XCTestCase {
}
}

func test_FromContentsOfURLUsedEncodingUTF32BE() {
guard let testFileURL = testBundle().url(forResource: "NSString-UTF32-BE-data", withExtension: "txt") else {
XCTFail("URL for NSString-UTF32-BE-data.txt is nil")
return
}

do {
var encoding: UInt = 0
let string = try NSString(contentsOf: testFileURL, usedEncoding: &encoding)
XCTAssertEqual(string, "NSString fromURL usedEncoding test with UTF32 BE file", "Wrong result when reading UTF32BE file")
XCTAssertEqual(encoding, String.Encoding.utf32BigEndian.rawValue, "Wrong encoding detected from UTF32BE file")
} catch {
XCTFail("Unable to init NSString from contentOf:usedEncoding:")
}
}

func test_FromContentsOfURLUsedEncodingUTF32LE() {
guard let testFileURL = testBundle().url(forResource: "NSString-UTF32-LE-data", withExtension: "txt") else {
XCTFail("URL for NSString-UTF32-LE-data.txt is nil")
return
}

do {
var encoding: UInt = 0
let string = try NSString(contentsOf: testFileURL, usedEncoding: &encoding)
XCTAssertEqual(string, "NSString fromURL usedEncoding test with UTF32 LE file", "Wrong result when reading UTF32LE file")
XCTAssertEqual(encoding, String.Encoding.utf32LittleEndian.rawValue, "Wrong encoding detected from UTF32LE file")
} catch {
XCTFail("Unable to init NSString from contentOf:usedEncoding:")
}
}

Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parkera @phausler I just tested this on MacOS... The tests are passing...

func test_FromContentOfFile() {
let testFilePath = testBundle().path(forResource: "NSStringTestData", ofType: "txt")
XCTAssertNotNil(testFilePath)
Expand Down
2 changes: 2 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Contributor

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

Copy link
Contributor Author

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

'TestFoundation/Resources/NSString-UTF32-LE-data.txt',
'TestFoundation/Resources/NSXMLDocumentTestData.xml',
'TestFoundation/Resources/PropertyList-1.0.dtd',
'TestFoundation/Resources/NSXMLDTDTestData.xml',
Expand Down