Skip to content

[5.7] Write string external representation in String.write(to: URL) #4628

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

Open
wants to merge 1 commit into
base: release/5.7
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Sources/Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ extension NSString {
let length = self.length
var numBytes = 0
let theRange = NSRange(location: 0, length: length)
if !getBytes(nil, maxLength: Int.max - 1, usedLength: &numBytes, encoding: enc, options: [], range: theRange, remaining: nil) {
if !getBytes(nil, maxLength: Int.max - 1, usedLength: &numBytes, encoding: enc, options: [.externalRepresentation], range: theRange, remaining: nil) {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteInapplicableStringEncoding.rawValue, userInfo: [
NSURLErrorKey: dest,
])
Expand All @@ -1263,7 +1263,7 @@ extension NSString {
// The getBytes:... call should hopefully not fail, given it succeeded above, but check anyway (mutable string changing behind our back?)
var used = 0
try mData.withUnsafeMutableBytes { (mutableRawBuffer: UnsafeMutableRawBufferPointer) -> Void in
if !getBytes(mutableRawBuffer.baseAddress, maxLength: numBytes, usedLength: &used, encoding: enc, options: [], range: theRange, remaining: nil) {
if !getBytes(mutableRawBuffer.baseAddress, maxLength: numBytes, usedLength: &used, encoding: enc, options: [.externalRepresentation], range: theRange, remaining: nil) {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue, userInfo: [
NSURLErrorKey: dest,
])
Expand Down
30 changes: 30 additions & 0 deletions Tests/Foundation/Tests/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,35 @@ class TestNSString: LoopbackServerTest {
}
}

func test_writeToURLHasBOM_UTF32() throws {
var url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
url.appendPathComponent("swiftfoundation-\(#function)-tmp")

// Writing with String.Encoding.utf32 should include the BOM.
let string = NSString("hello, 🌍!")
do {
try string.write(to: url, atomically: false, encoding: String.Encoding.utf32.rawValue)
let data = try Data(contentsOf: url)
#if _endian(little)
XCTAssertTrue(data.starts(with: [0xFF, 0xFE, 0x00, 0x00]))
#else
XCTAssertTrue(data.starts(with: [0x00, 0x00, 0xFE, 0xFF]))
#endif
}
// Writing with String.Encoding.utf32{Big/Little}Endian does not include the BOM.
do {
try string.write(to: url, atomically: false, encoding: String.Encoding.utf32BigEndian.rawValue)
let data = try Data(contentsOf: url)
XCTAssertTrue(data.starts(with: [0x00, 0x00, 0x00, 0x68]))
}
do {
try string.write(to: url, atomically: false, encoding: String.Encoding.utf32LittleEndian.rawValue)
let data = try Data(contentsOf: url)
XCTAssertTrue(data.starts(with: [0x68, 0x00, 0x00, 0x00]))
}
}

func test_uppercaseString() {
XCTAssertEqual(NSString(stringLiteral: "abcd").uppercased, "ABCD")
XCTAssertEqual(NSString(stringLiteral: "abcd").uppercased, "ABCD") // full-width
Expand Down Expand Up @@ -1788,6 +1817,7 @@ class TestNSString: LoopbackServerTest {
("test_FromContentsOfURLUsedEncodingUTF32BE", test_FromContentsOfURLUsedEncodingUTF32BE),
("test_FromContentsOfURLUsedEncodingUTF32LE", test_FromContentsOfURLUsedEncodingUTF32LE),
("test_FromContentOfFile",test_FromContentOfFile),
("test_writeToURLHasBOM_UTF32", test_writeToURLHasBOM_UTF32),
("test_swiftStringUTF16", test_swiftStringUTF16),
("test_stringByTrimmingCharactersInSet", test_stringByTrimmingCharactersInSet),
("test_initializeWithFormat", test_initializeWithFormat),
Expand Down