@@ -1005,10 +1005,11 @@ extension StringProtocol where Index == String.Index {
1005
1005
/// Returns a new string in which the characters in a
1006
1006
/// specified range of the `String` are replaced by a given string.
1007
1007
public func replacingCharacters<
1008
- T : StringProtocol
1009
- > ( in range: Range < Index > , with replacement: T ) -> String {
1008
+ T : StringProtocol , R : RangeExpression
1009
+ > ( in range: R , with replacement: T ) -> String where R . Bound == Index {
1010
1010
return _ns. replacingCharacters (
1011
- in: _toNSRange ( range) , with: replacement. _ephemeralString)
1011
+ in: _toNSRange ( range. relative ( to: self ) ) ,
1012
+ with: replacement. _ephemeralString)
1012
1013
}
1013
1014
1014
1015
// - (NSString *)
@@ -1053,7 +1054,7 @@ extension StringProtocol where Index == String.Index {
1053
1054
/// Returns a new string made by replacing in the `String`
1054
1055
/// all percent escapes with the matching characters as determined
1055
1056
/// by a given encoding.
1056
- @available ( swift, deprecated: 3.2 , obsoleted: 4.0 ,
1057
+ @available ( swift, deprecated: 3.0 , obsoleted: 4.0 ,
1057
1058
message: " Use removingPercentEncoding instead, which always uses the recommended UTF-8 encoding. " )
1058
1059
public func replacingPercentEscapes(
1059
1060
using encoding: String . Encoding
@@ -1151,15 +1152,16 @@ extension StringProtocol where Index == String.Index {
1151
1152
/// enumerating the specific range of the string, providing the
1152
1153
/// Block with the located tags.
1153
1154
public func enumerateLinguisticTags<
1154
- T : StringProtocol
1155
+ T : StringProtocol , R : RangeExpression
1155
1156
> (
1156
- in range: Range < Index > ,
1157
+ in range: R ,
1157
1158
scheme tagScheme: T ,
1158
1159
options opts: NSLinguisticTagger . Options = [ ] ,
1159
1160
orthography: NSOrthography ? = nil ,
1160
1161
invoking body:
1161
1162
( String , Range < Index > , Range < Index > , inout Bool ) -> Void
1162
- ) {
1163
+ ) where R. Bound == Index {
1164
+ let range = range. relative ( to: self )
1163
1165
_ns. enumerateLinguisticTags (
1164
1166
in: _toNSRange ( range) ,
1165
1167
scheme: tagScheme. _ephemeralString,
@@ -1214,15 +1216,18 @@ extension StringProtocol where Index == String.Index {
1214
1216
/// enumerated range is included in one and only one enclosing range.
1215
1217
/// - An `inout` Boolean value that the closure can use to stop the
1216
1218
/// enumeration by setting `stop = true`.
1217
- public func enumerateSubstrings(
1218
- in range: Range < Index > ,
1219
+ public func enumerateSubstrings<
1220
+ R : RangeExpression
1221
+ > (
1222
+ in range: R ,
1219
1223
options opts: String . EnumerationOptions = [ ] ,
1220
1224
_ body: @escaping (
1221
1225
_ substring: String ? , _ substringRange: Range < Index > ,
1222
1226
_ enclosingRange: Range < Index > , inout Bool
1223
1227
) -> Void
1224
- ) {
1225
- _ns. enumerateSubstrings ( in: _toNSRange ( range) , options: opts) {
1228
+ ) where R. Bound == Index {
1229
+ _ns. enumerateSubstrings (
1230
+ in: _toNSRange ( range. relative ( to: self ) ) , options: opts) {
1226
1231
var stop_ = false
1227
1232
1228
1233
body ( $0,
@@ -1277,23 +1282,25 @@ extension StringProtocol where Index == String.Index {
1277
1282
/// conversion isn't possible due to the chosen encoding.
1278
1283
///
1279
1284
/// - Note: will get a maximum of `min(buffer.count, maxLength)` bytes.
1280
- public func getBytes(
1285
+ public func getBytes<
1286
+ R : RangeExpression
1287
+ > (
1281
1288
_ buffer: inout [ UInt8 ] ,
1282
1289
maxLength maxBufferCount: Int ,
1283
1290
usedLength usedBufferCount: UnsafeMutablePointer < Int > ,
1284
1291
encoding: String . Encoding ,
1285
1292
options: String . EncodingConversionOptions = [ ] ,
1286
- range: Range < Index > ,
1293
+ range: R ,
1287
1294
remaining leftover: UnsafeMutablePointer < Range < Index > >
1288
- ) -> Bool {
1295
+ ) -> Bool where R . Bound == Index {
1289
1296
return _withOptionalOutParameter ( leftover) {
1290
1297
self . _ns. getBytes (
1291
1298
& buffer,
1292
1299
maxLength: Swift . min ( buffer. count, maxBufferCount) ,
1293
1300
usedLength: usedBufferCount,
1294
1301
encoding: encoding. rawValue,
1295
1302
options: options,
1296
- range: _toNSRange ( range) ,
1303
+ range: _toNSRange ( range. relative ( to : self ) ) ,
1297
1304
remaining: $0)
1298
1305
}
1299
1306
}
@@ -1306,19 +1313,21 @@ extension StringProtocol where Index == String.Index {
1306
1313
1307
1314
/// Returns by reference the beginning of the first line and
1308
1315
/// the end of the last line touched by the given range.
1309
- public func getLineStart(
1316
+ public func getLineStart<
1317
+ R : RangeExpression
1318
+ > (
1310
1319
_ start: UnsafeMutablePointer < Index > ,
1311
1320
end: UnsafeMutablePointer < Index > ,
1312
1321
contentsEnd: UnsafeMutablePointer < Index > ,
1313
- for range: Range < Index >
1314
- ) {
1322
+ for range: R
1323
+ ) where R . Bound == Index {
1315
1324
_withOptionalOutParameter ( start) {
1316
1325
start in self . _withOptionalOutParameter ( end) {
1317
1326
end in self . _withOptionalOutParameter ( contentsEnd) {
1318
1327
contentsEnd in self . _ns. getLineStart (
1319
1328
start, end: end,
1320
1329
contentsEnd: contentsEnd,
1321
- for: _toNSRange ( range) )
1330
+ for: _toNSRange ( range. relative ( to : self ) ) )
1322
1331
}
1323
1332
}
1324
1333
}
@@ -1332,19 +1341,21 @@ extension StringProtocol where Index == String.Index {
1332
1341
1333
1342
/// Returns by reference the beginning of the first paragraph
1334
1343
/// and the end of the last paragraph touched by the given range.
1335
- public func getParagraphStart(
1344
+ public func getParagraphStart<
1345
+ R : RangeExpression
1346
+ > (
1336
1347
_ start: UnsafeMutablePointer < Index > ,
1337
1348
end: UnsafeMutablePointer < Index > ,
1338
1349
contentsEnd: UnsafeMutablePointer < Index > ,
1339
- for range: Range < Index >
1340
- ) {
1350
+ for range: R
1351
+ ) where R . Bound == Index {
1341
1352
_withOptionalOutParameter ( start) {
1342
1353
start in self . _withOptionalOutParameter ( end) {
1343
1354
end in self . _withOptionalOutParameter ( contentsEnd) {
1344
1355
contentsEnd in self . _ns. getParagraphStart (
1345
1356
start, end: end,
1346
1357
contentsEnd: contentsEnd,
1347
- for: _toNSRange ( range) )
1358
+ for: _toNSRange ( range. relative ( to : self ) ) )
1348
1359
}
1349
1360
}
1350
1361
}
@@ -1368,8 +1379,10 @@ extension StringProtocol where Index == String.Index {
1368
1379
1369
1380
/// Returns the range of characters representing the line or lines
1370
1381
/// containing a given range.
1371
- public func lineRange( for aRange: Range < Index > ) -> Range < Index > {
1372
- return _range ( _ns. lineRange ( for: _toNSRange ( aRange) ) )
1382
+ public func lineRange<
1383
+ R : RangeExpression
1384
+ > ( for aRange: R ) -> Range < Index > where R. Bound == Index {
1385
+ return _range ( _ns. lineRange ( for: _toNSRange ( aRange. relative ( to: self ) ) ) )
1373
1386
}
1374
1387
1375
1388
// - (NSArray *)
@@ -1382,18 +1395,18 @@ extension StringProtocol where Index == String.Index {
1382
1395
/// Returns an array of linguistic tags for the specified
1383
1396
/// range and requested tags within the receiving string.
1384
1397
public func linguisticTags<
1385
- T : StringProtocol
1398
+ T : StringProtocol , R : RangeExpression
1386
1399
> (
1387
- in range: Range < Index > ,
1400
+ in range: R ,
1388
1401
scheme tagScheme: T ,
1389
1402
options opts: NSLinguisticTagger . Options = [ ] ,
1390
1403
orthography: NSOrthography ? = nil ,
1391
1404
tokenRanges: UnsafeMutablePointer < [ Range < Index > ] > ? = nil // FIXME:Can this be nil?
1392
- ) -> [ String ] {
1405
+ ) -> [ String ] where R . Bound == Index {
1393
1406
var nsTokenRanges : NSArray ?
1394
1407
let result = tokenRanges. _withNilOrAddress ( of: & nsTokenRanges) {
1395
1408
self . _ns. linguisticTags (
1396
- in: _toNSRange ( range) ,
1409
+ in: _toNSRange ( range. relative ( to : self ) ) ,
1397
1410
scheme: tagScheme. _ephemeralString,
1398
1411
options: opts,
1399
1412
orthography: orthography,
@@ -1413,8 +1426,11 @@ extension StringProtocol where Index == String.Index {
1413
1426
1414
1427
/// Returns the range of characters representing the
1415
1428
/// paragraph or paragraphs containing a given range.
1416
- public func paragraphRange( for aRange: Range < Index > ) -> Range < Index > {
1417
- return _range ( _ns. paragraphRange ( for: _toNSRange ( aRange) ) )
1429
+ public func paragraphRange<
1430
+ R : RangeExpression
1431
+ > ( for aRange: R ) -> Range < Index > where R. Bound == Index {
1432
+ return _range (
1433
+ _ns. paragraphRange ( for: _toNSRange ( aRange. relative ( to: self ) ) ) )
1418
1434
}
1419
1435
1420
1436
// - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet
@@ -1461,14 +1477,17 @@ extension StringProtocol where Index == String.Index {
1461
1477
1462
1478
/// Returns the range in the string of the composed character
1463
1479
/// sequences for a given range.
1464
- public func rangeOfComposedCharacterSequences(
1465
- for range: Range < Index >
1466
- ) -> Range < Index > {
1480
+ public func rangeOfComposedCharacterSequences<
1481
+ R : RangeExpression
1482
+ > (
1483
+ for range: R
1484
+ ) -> Range < Index > where R. Bound == Index {
1467
1485
// Theoretically, this will be the identity function. In practice
1468
1486
// I think users will be able to observe differences in the input
1469
1487
// and output ranges due (if nothing else) to locale changes
1470
1488
return _range (
1471
- _ns. rangeOfComposedCharacterSequences ( for: _toNSRange ( range) ) )
1489
+ _ns. rangeOfComposedCharacterSequences (
1490
+ for: _toNSRange ( range. relative ( to: self ) ) ) )
1472
1491
}
1473
1492
1474
1493
// - (NSRange)rangeOfString:(NSString *)aString
@@ -1540,7 +1559,8 @@ extension StringProtocol where Index == String.Index {
1540
1559
/// Returns a representation of the `String` using a given
1541
1560
/// encoding to determine the percent escapes necessary to convert
1542
1561
/// the `String` into a legal URL string.
1543
- @available ( * , deprecated, message: " Use addingPercentEncoding(withAllowedCharacters:) instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid. " )
1562
+ @available ( swift, deprecated: 3.0 , obsoleted: 4.0 ,
1563
+ message: " Use addingPercentEncoding(withAllowedCharacters:) instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid. " )
1544
1564
public func addingPercentEscapes(
1545
1565
using encoding: String . Encoding
1546
1566
) -> String ? {
@@ -1556,10 +1576,9 @@ extension StringProtocol where Index == String.Index {
1556
1576
///
1557
1577
/// Equivalent to `self.rangeOfString(other) != nil`
1558
1578
public func contains< T : StringProtocol > ( _ other: T ) -> Bool {
1559
- let other = other. _ephemeralString
1560
1579
let r = self . range ( of: other) != nil
1561
1580
if #available( OSX 10 . 10 , iOS 8 . 0 , * ) {
1562
- _sanityCheck ( r == _ns. contains ( other) )
1581
+ _sanityCheck ( r == _ns. contains ( other. _ephemeralString ) )
1563
1582
}
1564
1583
return r
1565
1584
}
@@ -1578,12 +1597,12 @@ extension StringProtocol where Index == String.Index {
1578
1597
public func localizedCaseInsensitiveContains<
1579
1598
T : StringProtocol
1580
1599
> ( _ other: T ) -> Bool {
1581
- let other = other. _ephemeralString
1582
1600
let r = self . range (
1583
1601
of: other, options: . caseInsensitive, locale: Locale . current
1584
1602
) != nil
1585
1603
if #available( OSX 10 . 10 , iOS 8 . 0 , * ) {
1586
- _sanityCheck ( r == _ns. localizedCaseInsensitiveContains ( other) )
1604
+ _sanityCheck ( r ==
1605
+ _ns. localizedCaseInsensitiveContains ( other. _ephemeralString) )
1587
1606
}
1588
1607
return r
1589
1608
}
0 commit comments