Skip to content

Commit 832c5d6

Browse files
author
Max Moiseev
committed
[overlay] Use RangeExpression whenever possible instead of Range
(cherry picked from commit 1a0436e)
1 parent c2476f4 commit 832c5d6

File tree

1 file changed

+59
-40
lines changed

1 file changed

+59
-40
lines changed

stdlib/public/SDK/Foundation/NSStringAPI.swift

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,10 +1005,11 @@ extension StringProtocol where Index == String.Index {
10051005
/// Returns a new string in which the characters in a
10061006
/// specified range of the `String` are replaced by a given string.
10071007
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 {
10101010
return _ns.replacingCharacters(
1011-
in: _toNSRange(range), with: replacement._ephemeralString)
1011+
in: _toNSRange(range.relative(to: self)),
1012+
with: replacement._ephemeralString)
10121013
}
10131014

10141015
// - (NSString *)
@@ -1053,7 +1054,7 @@ extension StringProtocol where Index == String.Index {
10531054
/// Returns a new string made by replacing in the `String`
10541055
/// all percent escapes with the matching characters as determined
10551056
/// by a given encoding.
1056-
@available(swift, deprecated: 3.2, obsoleted: 4.0,
1057+
@available(swift, deprecated: 3.0, obsoleted: 4.0,
10571058
message: "Use removingPercentEncoding instead, which always uses the recommended UTF-8 encoding.")
10581059
public func replacingPercentEscapes(
10591060
using encoding: String.Encoding
@@ -1151,15 +1152,16 @@ extension StringProtocol where Index == String.Index {
11511152
/// enumerating the specific range of the string, providing the
11521153
/// Block with the located tags.
11531154
public func enumerateLinguisticTags<
1154-
T : StringProtocol
1155+
T : StringProtocol, R : RangeExpression
11551156
>(
1156-
in range: Range<Index>,
1157+
in range: R,
11571158
scheme tagScheme: T,
11581159
options opts: NSLinguisticTagger.Options = [],
11591160
orthography: NSOrthography? = nil,
11601161
invoking body:
11611162
(String, Range<Index>, Range<Index>, inout Bool) -> Void
1162-
) {
1163+
) where R.Bound == Index {
1164+
let range = range.relative(to: self)
11631165
_ns.enumerateLinguisticTags(
11641166
in: _toNSRange(range),
11651167
scheme: tagScheme._ephemeralString,
@@ -1214,15 +1216,18 @@ extension StringProtocol where Index == String.Index {
12141216
/// enumerated range is included in one and only one enclosing range.
12151217
/// - An `inout` Boolean value that the closure can use to stop the
12161218
/// 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,
12191223
options opts: String.EnumerationOptions = [],
12201224
_ body: @escaping (
12211225
_ substring: String?, _ substringRange: Range<Index>,
12221226
_ enclosingRange: Range<Index>, inout Bool
12231227
) -> 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) {
12261231
var stop_ = false
12271232

12281233
body($0,
@@ -1277,23 +1282,25 @@ extension StringProtocol where Index == String.Index {
12771282
/// conversion isn't possible due to the chosen encoding.
12781283
///
12791284
/// - Note: will get a maximum of `min(buffer.count, maxLength)` bytes.
1280-
public func getBytes(
1285+
public func getBytes<
1286+
R : RangeExpression
1287+
>(
12811288
_ buffer: inout [UInt8],
12821289
maxLength maxBufferCount: Int,
12831290
usedLength usedBufferCount: UnsafeMutablePointer<Int>,
12841291
encoding: String.Encoding,
12851292
options: String.EncodingConversionOptions = [],
1286-
range: Range<Index>,
1293+
range: R,
12871294
remaining leftover: UnsafeMutablePointer<Range<Index>>
1288-
) -> Bool {
1295+
) -> Bool where R.Bound == Index {
12891296
return _withOptionalOutParameter(leftover) {
12901297
self._ns.getBytes(
12911298
&buffer,
12921299
maxLength: Swift.min(buffer.count, maxBufferCount),
12931300
usedLength: usedBufferCount,
12941301
encoding: encoding.rawValue,
12951302
options: options,
1296-
range: _toNSRange(range),
1303+
range: _toNSRange(range.relative(to: self)),
12971304
remaining: $0)
12981305
}
12991306
}
@@ -1306,19 +1313,21 @@ extension StringProtocol where Index == String.Index {
13061313

13071314
/// Returns by reference the beginning of the first line and
13081315
/// the end of the last line touched by the given range.
1309-
public func getLineStart(
1316+
public func getLineStart<
1317+
R : RangeExpression
1318+
>(
13101319
_ start: UnsafeMutablePointer<Index>,
13111320
end: UnsafeMutablePointer<Index>,
13121321
contentsEnd: UnsafeMutablePointer<Index>,
1313-
for range: Range<Index>
1314-
) {
1322+
for range: R
1323+
) where R.Bound == Index {
13151324
_withOptionalOutParameter(start) {
13161325
start in self._withOptionalOutParameter(end) {
13171326
end in self._withOptionalOutParameter(contentsEnd) {
13181327
contentsEnd in self._ns.getLineStart(
13191328
start, end: end,
13201329
contentsEnd: contentsEnd,
1321-
for: _toNSRange(range))
1330+
for: _toNSRange(range.relative(to: self)))
13221331
}
13231332
}
13241333
}
@@ -1332,19 +1341,21 @@ extension StringProtocol where Index == String.Index {
13321341

13331342
/// Returns by reference the beginning of the first paragraph
13341343
/// and the end of the last paragraph touched by the given range.
1335-
public func getParagraphStart(
1344+
public func getParagraphStart<
1345+
R : RangeExpression
1346+
>(
13361347
_ start: UnsafeMutablePointer<Index>,
13371348
end: UnsafeMutablePointer<Index>,
13381349
contentsEnd: UnsafeMutablePointer<Index>,
1339-
for range: Range<Index>
1340-
) {
1350+
for range: R
1351+
) where R.Bound == Index {
13411352
_withOptionalOutParameter(start) {
13421353
start in self._withOptionalOutParameter(end) {
13431354
end in self._withOptionalOutParameter(contentsEnd) {
13441355
contentsEnd in self._ns.getParagraphStart(
13451356
start, end: end,
13461357
contentsEnd: contentsEnd,
1347-
for: _toNSRange(range))
1358+
for: _toNSRange(range.relative(to: self)))
13481359
}
13491360
}
13501361
}
@@ -1368,8 +1379,10 @@ extension StringProtocol where Index == String.Index {
13681379

13691380
/// Returns the range of characters representing the line or lines
13701381
/// 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))))
13731386
}
13741387

13751388
// - (NSArray *)
@@ -1382,18 +1395,18 @@ extension StringProtocol where Index == String.Index {
13821395
/// Returns an array of linguistic tags for the specified
13831396
/// range and requested tags within the receiving string.
13841397
public func linguisticTags<
1385-
T : StringProtocol
1398+
T : StringProtocol, R : RangeExpression
13861399
>(
1387-
in range: Range<Index>,
1400+
in range: R,
13881401
scheme tagScheme: T,
13891402
options opts: NSLinguisticTagger.Options = [],
13901403
orthography: NSOrthography? = nil,
13911404
tokenRanges: UnsafeMutablePointer<[Range<Index>]>? = nil // FIXME:Can this be nil?
1392-
) -> [String] {
1405+
) -> [String] where R.Bound == Index {
13931406
var nsTokenRanges: NSArray?
13941407
let result = tokenRanges._withNilOrAddress(of: &nsTokenRanges) {
13951408
self._ns.linguisticTags(
1396-
in: _toNSRange(range),
1409+
in: _toNSRange(range.relative(to: self)),
13971410
scheme: tagScheme._ephemeralString,
13981411
options: opts,
13991412
orthography: orthography,
@@ -1413,8 +1426,11 @@ extension StringProtocol where Index == String.Index {
14131426

14141427
/// Returns the range of characters representing the
14151428
/// 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))))
14181434
}
14191435

14201436
// - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet
@@ -1461,14 +1477,17 @@ extension StringProtocol where Index == String.Index {
14611477

14621478
/// Returns the range in the string of the composed character
14631479
/// 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 {
14671485
// Theoretically, this will be the identity function. In practice
14681486
// I think users will be able to observe differences in the input
14691487
// and output ranges due (if nothing else) to locale changes
14701488
return _range(
1471-
_ns.rangeOfComposedCharacterSequences(for: _toNSRange(range)))
1489+
_ns.rangeOfComposedCharacterSequences(
1490+
for: _toNSRange(range.relative(to: self))))
14721491
}
14731492

14741493
// - (NSRange)rangeOfString:(NSString *)aString
@@ -1540,7 +1559,8 @@ extension StringProtocol where Index == String.Index {
15401559
/// Returns a representation of the `String` using a given
15411560
/// encoding to determine the percent escapes necessary to convert
15421561
/// 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.")
15441564
public func addingPercentEscapes(
15451565
using encoding: String.Encoding
15461566
) -> String? {
@@ -1556,10 +1576,9 @@ extension StringProtocol where Index == String.Index {
15561576
///
15571577
/// Equivalent to `self.rangeOfString(other) != nil`
15581578
public func contains<T : StringProtocol>(_ other: T) -> Bool {
1559-
let other = other._ephemeralString
15601579
let r = self.range(of: other) != nil
15611580
if #available(OSX 10.10, iOS 8.0, *) {
1562-
_sanityCheck(r == _ns.contains(other))
1581+
_sanityCheck(r == _ns.contains(other._ephemeralString))
15631582
}
15641583
return r
15651584
}
@@ -1578,12 +1597,12 @@ extension StringProtocol where Index == String.Index {
15781597
public func localizedCaseInsensitiveContains<
15791598
T : StringProtocol
15801599
>(_ other: T) -> Bool {
1581-
let other = other._ephemeralString
15821600
let r = self.range(
15831601
of: other, options: .caseInsensitive, locale: Locale.current
15841602
) != nil
15851603
if #available(OSX 10.10, iOS 8.0, *) {
1586-
_sanityCheck(r == _ns.localizedCaseInsensitiveContains(other))
1604+
_sanityCheck(r ==
1605+
_ns.localizedCaseInsensitiveContains(other._ephemeralString))
15871606
}
15881607
return r
15891608
}

0 commit comments

Comments
 (0)