@@ -88,6 +88,7 @@ class TestNSString: LoopbackServerTest {
88
88
( " test_emptyStringPrefixAndSuffix " , test_emptyStringPrefixAndSuffix) ,
89
89
( " test_reflection " , { _ in test_reflection } ) ,
90
90
( " test_replacingOccurrences " , test_replacingOccurrences) ,
91
+ ( " test_replacingOccurrencesInSubclass " , test_replacingOccurrencesInSubclass) ,
91
92
( " test_getLineStart " , test_getLineStart) ,
92
93
( " test_substringWithRange " , test_substringWithRange) ,
93
94
( " test_createCopy " , test_createCopy) ,
@@ -1250,4 +1251,68 @@ extension TestNSString {
1250
1251
let replaceSuffixWithMultibyte = testString. replacingOccurrences ( of: testSuffix, with: testReplacementEmoji)
1251
1252
XCTAssertEqual ( replaceSuffixWithMultibyte, testPrefix + testEmoji + testReplacementEmoji)
1252
1253
}
1254
+
1255
+ func test_replacingOccurrencesInSubclass( ) {
1256
+ class TestMutableString : NSMutableString {
1257
+ internal var _storage : String
1258
+ private var _replaceCharactersCount : Int = 0
1259
+ var replaceCharactersCount : Int {
1260
+ return _replaceCharactersCount
1261
+ }
1262
+
1263
+ override var length : Int {
1264
+ return _storage. utf16. count
1265
+ }
1266
+
1267
+ override func character( at index: Int ) -> unichar {
1268
+ let start = _storage. utf16. startIndex
1269
+ return _storage. utf16 [ start. advanced ( by: index) ]
1270
+ }
1271
+
1272
+ override func replaceCharacters( in range: NSRange , with aString: String ) {
1273
+ _replaceCharactersCount = _replaceCharactersCount + 1
1274
+ let start = _storage. utf16. startIndex
1275
+ let min = _storage. utf16. index ( start, offsetBy: range. location) . samePosition ( in: _storage) !
1276
+ let max = _storage. utf16. index ( start, offsetBy: range. location + range. length) . samePosition ( in: _storage) !
1277
+ _storage. replaceSubrange ( min..< max, with: aString)
1278
+ }
1279
+
1280
+ override func mutableCopy( with zone: NSZone ? = nil ) -> Any {
1281
+ return self
1282
+ }
1283
+
1284
+ required init ( stringLiteral value: StaticString ) {
1285
+ _storage = String ( describing: value)
1286
+ super. init ( stringLiteral: value)
1287
+ }
1288
+
1289
+ required init ( capacity: Int ) {
1290
+ fatalError ( " init(capacity:) has not been implemented " )
1291
+ }
1292
+
1293
+ required init ( string aString: String ) {
1294
+ fatalError ( " init(string:) has not been implemented " )
1295
+ }
1296
+
1297
+ required convenience init ? ( coder aDecoder: NSCoder ) {
1298
+ fatalError ( " init(coder:) has not been implemented " )
1299
+ }
1300
+
1301
+ required init ( characters: UnsafePointer < unichar > , length: Int ) {
1302
+ fatalError ( " init(characters:length:) has not been implemented " )
1303
+ }
1304
+
1305
+ required convenience init ( extendedGraphemeClusterLiteral value: StaticString ) {
1306
+ fatalError ( " init(extendedGraphemeClusterLiteral:) has not been implemented " )
1307
+ }
1308
+
1309
+ required convenience init ( unicodeScalarLiteral value: StaticString ) {
1310
+ fatalError ( " init(unicodeScalarLiteral:) has not been implemented " )
1311
+ }
1312
+ }
1313
+ let testString = TestMutableString ( stringLiteral: " ababab " )
1314
+ XCTAssertEqual ( testString. replacingOccurrences ( of: " ab " , with: " xx " ) , " xxxxxx " )
1315
+ XCTAssertEqual ( testString. replaceCharactersCount, 3 )
1316
+ }
1317
+
1253
1318
}
0 commit comments