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