Skip to content

Commit 533ad9f

Browse files
committed
[SDK] Replace NSRange.toRange() with failable initialiser on Range<Int>
Fixes ABI FIXME #75
1 parent b454c78 commit 533ad9f

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

stdlib/public/SDK/Foundation/Calendar.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
368368
/// - parameter component: A component to calculate a range for.
369369
/// - returns: The range, or nil if it could not be calculated.
370370
public func minimumRange(of component: Component) -> Range<Int>? {
371-
return _handle.map { $0.minimumRange(of: Calendar._toCalendarUnit([component])).toRange() }
371+
return _handle.map { Range($0.minimumRange(of: Calendar._toCalendarUnit([component]))) }
372372
}
373373

374374
/// The maximum range limits of the values that a given component can take on in the receive
@@ -377,7 +377,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
377377
/// - parameter component: A component to calculate a range for.
378378
/// - returns: The range, or nil if it could not be calculated.
379379
public func maximumRange(of component: Component) -> Range<Int>? {
380-
return _handle.map { $0.maximumRange(of: Calendar._toCalendarUnit([component])).toRange() }
380+
return _handle.map { Range($0.maximumRange(of: Calendar._toCalendarUnit([component]))) }
381381
}
382382

383383

@@ -394,7 +394,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
394394
/// - parameter date: The absolute time for which the calculation is performed.
395395
/// - returns: The range of absolute time values smaller can take on in larger at the time specified by date. Returns `nil` if larger is not logically bigger than smaller in the calendar, or the given combination of components does not make sense (or is a computation which is undefined).
396396
public func range(of smaller: Component, in larger: Component, for date: Date) -> Range<Int>? {
397-
return _handle.map { $0.range(of: Calendar._toCalendarUnit([smaller]), in: Calendar._toCalendarUnit([larger]), for: date).toRange() }
397+
return _handle.map { Range($0.range(of: Calendar._toCalendarUnit([smaller]), in: Calendar._toCalendarUnit([larger]), for: date)) }
398398
}
399399

400400
@available(*, unavailable, message: "use range(of:in:for:) instead")

stdlib/public/SDK/Foundation/NSRange.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616
// Ranges
1717
//===----------------------------------------------------------------------===//
1818

19+
extension Range where Bound == Int {
20+
public init?(_ x: NSRange) {
21+
guard x.location != NSNotFound else { return nil }
22+
self.init(uncheckedBounds: (x.location, x.location + x.length))
23+
}
24+
}
25+
1926
extension NSRange {
2027
public init(_ x: Range<Int>) {
2128
location = x.lowerBound
2229
length = x.count
2330
}
2431

25-
// FIXME(ABI)#75 (Conditional Conformance): this API should be an extension on Range.
26-
// Can't express it now because the compiler does not support conditional
27-
// extensions with type equality constraints.
32+
@available(*, deprecated, message: "Use Range(_: NSRange) initializer instead")
2833
public func toRange() -> Range<Int>? {
29-
if location == NSNotFound { return nil }
30-
return location..<(location+length)
34+
return Range(self)
3135
}
3236
}
3337

stdlib/public/core/StringUTF16.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ extension String {
9191
/// `NSRange`. To convert an `NSRange` instance to a range of
9292
/// `String.UTF16View.Index`, follow these steps:
9393
///
94-
/// 1. Use the `NSRange` type's `toRange` method to convert the instance to
95-
/// an optional range of `Int` values.
94+
/// 1. Use the `Range` type's failable initializer to convert the `NSRange` to
95+
/// a range of `Int` values.
9696
/// 2. Use your string's `utf16` view's index manipulation methods to convert
9797
/// the integer bounds to `String.UTF16View.Index` values.
9898
/// 3. Create a new `Range` instance from the new index values.
@@ -103,7 +103,7 @@ extension String {
103103
///
104104
/// let snowy = "❄️ Let it snow! ☃️"
105105
/// let nsrange = NSRange(location: 3, length: 12)
106-
/// if let r = nsrange.toRange() {
106+
/// if let r = Range(nsrange) {
107107
/// let start = snowy.utf16.index(snowy.utf16.startIndex, offsetBy: r.lowerBound)
108108
/// let end = snowy.utf16.index(snowy.utf16.startIndex, offsetBy: r.upperBound)
109109
/// let substringRange = start..<end

test/Interpreter/SDK/Foundation_NSString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ print(hello)
1111

1212
// CHECK: ello,
1313
var helloStr: String = hello as String
14-
print(String(helloStr._core[NSRange(location: 1, length: 5).toRange()!]))
14+
print(String(helloStr._core[Range(NSRange(location: 1, length: 5))!]))
1515

1616
var upperHello = hello.uppercased
1717
// CHECK: HELLO, WORLD!

0 commit comments

Comments
 (0)