Skip to content

Commit 6e4bf20

Browse files
author
Mohit Athwani
committed
Ensuring file size is atleasy 2 bytes and added test case for UTF 16 LE
1 parent e97ad39 commit 6e4bf20

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

Foundation/NSString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,10 +1301,10 @@ extension NSString {
13011301
let readResult = try NSData(contentsOf: url, options:[])
13021302

13031303
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity:readResult.length)
1304-
if bytePtr[0] == 254 && bytePtr[1] == 255 {
1304+
if readResult.length >= 2 && bytePtr[0] == 254 && bytePtr[1] == 255 {
13051305
enc?.pointee = String.Encoding.utf16BigEndian.rawValue
13061306
}
1307-
else if bytePtr[0] == 255 && bytePtr[1] == 254 {
1307+
else if readResult.length >= 2 && bytePtr[0] == 255 && bytePtr[1] == 254 {
13081308
enc?.pointee = String.Encoding.utf16LittleEndian.rawValue
13091309
}
13101310
else {
108 Bytes
Binary file not shown.

TestFoundation/TestNSString.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class TestNSString : XCTestCase {
6767
("test_CFStringCreateMutableCopy", test_CFStringCreateMutableCopy),
6868
("test_FromContentsOfURL",test_FromContentsOfURL),
6969
("test_FromContentsOfURLUsedEncodingUTF16BE", test_FromContentsOfURLUsedEncodingUTF16BE),
70+
("test_FromContentsOfURLUsedEncodingUTF16LE", test_FromContentsOfURLUsedEncodingUTF16LE),
7071
("test_FromContentOfFile",test_FromContentOfFile),
7172
("test_swiftStringUTF16", test_swiftStringUTF16),
7273
// This test takes forever on build servers; it has been seen up to 1852.084 seconds
@@ -313,7 +314,23 @@ class TestNSString : XCTestCase {
313314
XCTAssertEqual(string, "NSString fromURL usedEncoding test with UTF16 BE file", "Wrong result when reading UTF16BE file")
314315
XCTAssertEqual(encoding, String.Encoding.utf16BigEndian.rawValue, "Wrong encoding detected from UTF16BE file")
315316
} catch {
316-
XCTFail("Unable to init NSString from contentsOf:encoding:")
317+
XCTFail("Unable to init NSString from contentsOf:usedEncoding:")
318+
}
319+
}
320+
321+
func test_FromContentsOfURLUsedEncodingUTF16LE() {
322+
guard let testFileURL = testBundle().url(forResource: "NSString-UTF16-LE-data", withExtension: "txt") else {
323+
XCTFail("URL for NSString-UTF16-LE-data.txt is nil")
324+
return
325+
}
326+
327+
do {
328+
var encoding: UInt = 0
329+
let string = try NSString(contentsOf: testFileURL, usedEncoding: &encoding)
330+
XCTAssertEqual(string, "NSString fromURL usedEncoding test with UTF16 LE file", "Wrong result when reading UTF16LE file")
331+
XCTAssertEqual(encoding, String.Encoding.utf16LittleEndian.rawValue, "Wrong encoding detected from UTF16LE file")
332+
} catch {
333+
XCTFail("Unable to init NSString from contentOf:usedEncoding:")
317334
}
318335
}
319336

build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@
461461
'TestFoundation/Resources/Test.plist',
462462
'TestFoundation/Resources/NSStringTestData.txt',
463463
'TestFoundation/Resources/NSString-UTF16-BE-data.txt',
464+
'TestFoundation/Resources/NSString-UTF16-LE-data.txt',
464465
'TestFoundation/Resources/NSXMLDocumentTestData.xml',
465466
'TestFoundation/Resources/PropertyList-1.0.dtd',
466467
'TestFoundation/Resources/NSXMLDTDTestData.xml',

0 commit comments

Comments
 (0)