Skip to content

Commit d248649

Browse files
authored
Merge pull request #3158 from karwa/extrep
Write string external representation in String.write(to: URL)
2 parents e0cf003 + c4ce6c3 commit d248649

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Sources/Foundation/NSString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ extension NSString {
12541254
let length = self.length
12551255
var numBytes = 0
12561256
let theRange = NSRange(location: 0, length: length)
1257-
if !getBytes(nil, maxLength: Int.max - 1, usedLength: &numBytes, encoding: enc, options: [], range: theRange, remaining: nil) {
1257+
if !getBytes(nil, maxLength: Int.max - 1, usedLength: &numBytes, encoding: enc, options: [.externalRepresentation], range: theRange, remaining: nil) {
12581258
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteInapplicableStringEncoding.rawValue, userInfo: [
12591259
NSURLErrorKey: dest,
12601260
])
@@ -1263,7 +1263,7 @@ extension NSString {
12631263
// The getBytes:... call should hopefully not fail, given it succeeded above, but check anyway (mutable string changing behind our back?)
12641264
var used = 0
12651265
try mData.withUnsafeMutableBytes { (mutableRawBuffer: UnsafeMutableRawBufferPointer) -> Void in
1266-
if !getBytes(mutableRawBuffer.baseAddress, maxLength: numBytes, usedLength: &used, encoding: enc, options: [], range: theRange, remaining: nil) {
1266+
if !getBytes(mutableRawBuffer.baseAddress, maxLength: numBytes, usedLength: &used, encoding: enc, options: [.externalRepresentation], range: theRange, remaining: nil) {
12671267
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue, userInfo: [
12681268
NSURLErrorKey: dest,
12691269
])

Tests/Foundation/Tests/TestNSString.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,35 @@ class TestNSString: LoopbackServerTest {
460460
}
461461
}
462462

463+
func test_writeToURLHasBOM_UTF32() throws {
464+
var url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
465+
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
466+
url.appendPathComponent("swiftfoundation-\(#function)-tmp")
467+
468+
// Writing with String.Encoding.utf32 should include the BOM.
469+
let string = NSString("hello, 🌍!")
470+
do {
471+
try string.write(to: url, atomically: false, encoding: String.Encoding.utf32.rawValue)
472+
let data = try Data(contentsOf: url)
473+
#if _endian(little)
474+
XCTAssertTrue(data.starts(with: [0xFF, 0xFE, 0x00, 0x00]))
475+
#else
476+
XCTAssertTrue(data.starts(with: [0x00, 0x00, 0xFE, 0xFF]))
477+
#endif
478+
}
479+
// Writing with String.Encoding.utf32{Big/Little}Endian does not include the BOM.
480+
do {
481+
try string.write(to: url, atomically: false, encoding: String.Encoding.utf32BigEndian.rawValue)
482+
let data = try Data(contentsOf: url)
483+
XCTAssertTrue(data.starts(with: [0x00, 0x00, 0x00, 0x68]))
484+
}
485+
do {
486+
try string.write(to: url, atomically: false, encoding: String.Encoding.utf32LittleEndian.rawValue)
487+
let data = try Data(contentsOf: url)
488+
XCTAssertTrue(data.starts(with: [0x68, 0x00, 0x00, 0x00]))
489+
}
490+
}
491+
463492
func test_uppercaseString() {
464493
XCTAssertEqual(NSString(stringLiteral: "abcd").uppercased, "ABCD")
465494
XCTAssertEqual(NSString(stringLiteral: "abcd").uppercased, "ABCD") // full-width
@@ -1788,6 +1817,7 @@ class TestNSString: LoopbackServerTest {
17881817
("test_FromContentsOfURLUsedEncodingUTF32BE", test_FromContentsOfURLUsedEncodingUTF32BE),
17891818
("test_FromContentsOfURLUsedEncodingUTF32LE", test_FromContentsOfURLUsedEncodingUTF32LE),
17901819
("test_FromContentOfFile",test_FromContentOfFile),
1820+
("test_writeToURLHasBOM_UTF32", test_writeToURLHasBOM_UTF32),
17911821
("test_swiftStringUTF16", test_swiftStringUTF16),
17921822
("test_stringByTrimmingCharactersInSet", test_stringByTrimmingCharactersInSet),
17931823
("test_initializeWithFormat", test_initializeWithFormat),

0 commit comments

Comments
 (0)