Skip to content

Commit 55d7e65

Browse files
spevansianpartridge
authored andcommitted
Make NSString(contentsOf:usedEncoding:) tests work on macOS (#1149)
- Add the NSString-UTF32-{LE,BE}-data.txt tests to the copy resources. - Skip BOM header when passing data to CFStringCreateWithBytes().
1 parent e61b4c3 commit 55d7e65

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@
321321
B90C57BC1EEEEA5A005208AE /* TestThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5835F31C20C9B500C81317 /* TestThread.swift */; };
322322
B910957A1EEF237800A71930 /* NSString-UTF16-LE-data.txt in Resources */ = {isa = PBXBuildFile; fileRef = B91095781EEF237800A71930 /* NSString-UTF16-LE-data.txt */; };
323323
B910957B1EEF237800A71930 /* NSString-UTF16-BE-data.txt in Resources */ = {isa = PBXBuildFile; fileRef = B91095791EEF237800A71930 /* NSString-UTF16-BE-data.txt */; };
324+
B933A79E1F3055F700FE6846 /* NSString-UTF32-BE-data.txt in Resources */ = {isa = PBXBuildFile; fileRef = B933A79C1F3055F600FE6846 /* NSString-UTF32-BE-data.txt */; };
325+
B933A79F1F3055F700FE6846 /* NSString-UTF32-LE-data.txt in Resources */ = {isa = PBXBuildFile; fileRef = B933A79D1F3055F600FE6846 /* NSString-UTF32-LE-data.txt */; };
324326
B9974B961EDF4A22007F15B8 /* TransferState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9974B8F1EDF4A22007F15B8 /* TransferState.swift */; };
325327
B9974B971EDF4A22007F15B8 /* MultiHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9974B901EDF4A22007F15B8 /* MultiHandle.swift */; };
326328
B9974B981EDF4A22007F15B8 /* libcurlHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9974B911EDF4A22007F15B8 /* libcurlHelpers.swift */; };
@@ -785,6 +787,8 @@
785787
B167A6641ED7303F0040B09A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
786788
B91095781EEF237800A71930 /* NSString-UTF16-LE-data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "NSString-UTF16-LE-data.txt"; sourceTree = "<group>"; };
787789
B91095791EEF237800A71930 /* NSString-UTF16-BE-data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "NSString-UTF16-BE-data.txt"; sourceTree = "<group>"; };
790+
B933A79C1F3055F600FE6846 /* NSString-UTF32-BE-data.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "NSString-UTF32-BE-data.txt"; sourceTree = "<group>"; };
791+
B933A79D1F3055F600FE6846 /* NSString-UTF32-LE-data.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "NSString-UTF32-LE-data.txt"; sourceTree = "<group>"; };
788792
B9974B8F1EDF4A22007F15B8 /* TransferState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TransferState.swift; path = http/TransferState.swift; sourceTree = "<group>"; };
789793
B9974B901EDF4A22007F15B8 /* MultiHandle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MultiHandle.swift; path = http/MultiHandle.swift; sourceTree = "<group>"; };
790794
B9974B911EDF4A22007F15B8 /* libcurlHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = libcurlHelpers.swift; path = http/libcurlHelpers.swift; sourceTree = "<group>"; };
@@ -1402,6 +1406,8 @@
14021406
CE19A88B1C23AA2300B4CB6A /* NSStringTestData.txt */,
14031407
B91095781EEF237800A71930 /* NSString-UTF16-LE-data.txt */,
14041408
B91095791EEF237800A71930 /* NSString-UTF16-BE-data.txt */,
1409+
B933A79C1F3055F600FE6846 /* NSString-UTF32-BE-data.txt */,
1410+
B933A79D1F3055F600FE6846 /* NSString-UTF32-LE-data.txt */,
14051411
528776181BF27D9500CB0090 /* Test.plist */,
14061412
EA66F63B1BF1619600136161 /* NSURLTestData.plist */,
14071413
E1A3726E1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml */,
@@ -2076,6 +2082,8 @@
20762082
isa = PBXResourcesBuildPhase;
20772083
buildActionMask = 2147483647;
20782084
files = (
2085+
B933A79E1F3055F700FE6846 /* NSString-UTF32-BE-data.txt in Resources */,
2086+
B933A79F1F3055F700FE6846 /* NSString-UTF32-LE-data.txt in Resources */,
20792087
D3A597F41C34142600295652 /* NSKeyedUnarchiver-NotificationTest.plist in Resources */,
20802088
528776191BF27D9500CB0090 /* Test.plist in Resources */,
20812089
EA66F6481BF1619600136161 /* NSURLTestData.plist in Resources */,

Foundation/NSString.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,25 +1260,33 @@ extension NSString {
12601260
public convenience init(contentsOf url: URL, usedEncoding enc: UnsafeMutablePointer<UInt>?) throws {
12611261
let readResult = try NSData(contentsOf: url, options:[])
12621262

1263+
var offset = 0
12631264
let bytePtr = readResult.bytes.bindMemory(to: UInt8.self, capacity:readResult.length)
12641265
if readResult.length >= 4 && bytePtr[0] == 0xFF && bytePtr[1] == 0xFE && bytePtr[2] == 0x00 && bytePtr[3] == 0x00 {
1265-
enc?.pointee = String.Encoding.utf32LittleEndian.rawValue
1266+
enc?.pointee = String.Encoding.utf32LittleEndian.rawValue
1267+
offset = 4
12661268
}
12671269
else if readResult.length >= 2 && bytePtr[0] == 0xFE && bytePtr[1] == 0xFF {
1268-
enc?.pointee = String.Encoding.utf16BigEndian.rawValue
1270+
enc?.pointee = String.Encoding.utf16BigEndian.rawValue
1271+
offset = 2
12691272
}
12701273
else if readResult.length >= 2 && bytePtr[0] == 0xFF && bytePtr[1] == 0xFE {
1271-
enc?.pointee = String.Encoding.utf16LittleEndian.rawValue
1274+
enc?.pointee = String.Encoding.utf16LittleEndian.rawValue
1275+
offset = 2
12721276
}
12731277
else if readResult.length >= 4 && bytePtr[0] == 0x00 && bytePtr[1] == 0x00 && bytePtr[2] == 0xFE && bytePtr[3] == 0xFF {
1274-
enc?.pointee = String.Encoding.utf32BigEndian.rawValue
1278+
enc?.pointee = String.Encoding.utf32BigEndian.rawValue
1279+
offset = 4
12751280
}
12761281
else {
1277-
//Need to work on more conditions. This should be the default
1278-
enc?.pointee = String.Encoding.utf8.rawValue
1282+
//Need to work on more conditions. This should be the default
1283+
enc?.pointee = String.Encoding.utf8.rawValue
12791284
}
12801285

1281-
guard let enc = enc, let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr, readResult.length, CFStringConvertNSStringEncodingToEncoding(enc.pointee), true) else {
1286+
// Since the encoding being passed includes the byte order the BOM wont be checked or skipped, so pass offset to
1287+
// manually skip the BOM header.
1288+
guard let enc = enc, let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr + offset, readResult.length - offset,
1289+
CFStringConvertNSStringEncodingToEncoding(enc.pointee), true) else {
12821290
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadInapplicableStringEncoding.rawValue, userInfo: [
12831291
"NSDebugDescription" : "Unable to create a string using the specified encoding."
12841292
])

0 commit comments

Comments
 (0)