Skip to content

Commit 058ca94

Browse files
authored
Merge pull request #10947 from moiseev/foundation-string-extensions-4
[swift-4.0-branch][overlay] Port Foundation String extensions to StringProtocol
2 parents 26a00d4 + 832c5d6 commit 058ca94

File tree

3 files changed

+1311
-1217
lines changed

3 files changed

+1311
-1217
lines changed

stdlib/public/SDK/Foundation/NSRange.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,20 @@ extension NSRange {
140140
//===----------------------------------------------------------------------===//
141141

142142
extension NSRange {
143-
public init<R: RangeExpression>(_ rangeExpression: R)
143+
public init<R: RangeExpression>(_ region: R)
144144
where R.Bound: FixedWidthInteger, R.Bound.Stride : SignedInteger {
145-
let range = rangeExpression.relative(to: 0..<R.Bound.max)
146-
let start: Int = numericCast(range.lowerBound)
147-
let end: Int = numericCast(range.upperBound)
148-
self = NSRange(location: start, length: end - start)
145+
let r = region.relative(to: 0..<R.Bound.max)
146+
location = numericCast(r.lowerBound)
147+
length = numericCast(r.count)
149148
}
150149

151-
public init<R: RangeExpression, S: StringProtocol>(_ rangeExpression: R, in string: S)
152-
where R.Bound == String.Index, S.Index == String.Index {
153-
let range = rangeExpression.relative(to: string)
154-
let start = range.lowerBound.samePosition(in: string.utf16)
155-
let end = range.upperBound.samePosition(in: string.utf16)
156-
let location = string.utf16.distance(from: string.utf16.startIndex, to: start)
157-
let length = string.utf16.distance(from: start, to: end)
158-
self = NSRange(location: location, length: length)
150+
public init<R: RangeExpression, S: StringProtocol>(_ region: R, in target: S)
151+
where R.Bound == S.Index, S.Index == String.Index {
152+
let r = region.relative(to: target)
153+
self = NSRange(
154+
location: r.lowerBound._utf16Index - target.startIndex._utf16Index,
155+
length: r.upperBound._utf16Index - r.lowerBound._utf16Index
156+
)
159157
}
160158

161159
@available(swift, deprecated: 4, renamed: "Range.init(_:)")

0 commit comments

Comments
 (0)