@@ -460,6 +460,35 @@ class TestNSString: LoopbackServerTest {
460
460
}
461
461
}
462
462
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
+
463
492
func test_uppercaseString( ) {
464
493
XCTAssertEqual ( NSString ( stringLiteral: " abcd " ) . uppercased, " ABCD " )
465
494
XCTAssertEqual ( NSString ( stringLiteral: " abcd " ) . uppercased, " ABCD " ) // full-width
@@ -1788,6 +1817,7 @@ class TestNSString: LoopbackServerTest {
1788
1817
( " test_FromContentsOfURLUsedEncodingUTF32BE " , test_FromContentsOfURLUsedEncodingUTF32BE) ,
1789
1818
( " test_FromContentsOfURLUsedEncodingUTF32LE " , test_FromContentsOfURLUsedEncodingUTF32LE) ,
1790
1819
( " test_FromContentOfFile " , test_FromContentOfFile) ,
1820
+ ( " test_writeToURLHasBOM_UTF32 " , test_writeToURLHasBOM_UTF32) ,
1791
1821
( " test_swiftStringUTF16 " , test_swiftStringUTF16) ,
1792
1822
( " test_stringByTrimmingCharactersInSet " , test_stringByTrimmingCharactersInSet) ,
1793
1823
( " test_initializeWithFormat " , test_initializeWithFormat) ,
0 commit comments