mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
* Make Range conditionally a Collection
* Convert ClosedRange to conditionally a collection
* De-gyb Range/ClosedRange, refactoring some methods.
* Remove use of Countable{Closed}Range from stdlib
* Remove Countable use from Foundation
* Fix test errors and warnings resulting from Range/CountableRange collapse
* fix prespecialize test for new mangling
* Update CoreAudio use of CountableRange
* Update SwiftSyntax use of CountableRange
* Restore ClosedRange.Index: Hashable conformance
* Move fixed typechecker slowness test for array-of-ranges from slow to fast, yay
* Apply Doug's patch to loosen test to just check for error
87 lines
3.0 KiB
Swift
87 lines
3.0 KiB
Swift
//===--- RangeSelection.swift ---------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import StdlibUnittest
|
|
|
|
public enum RangeSelection {
|
|
case emptyRange
|
|
case leftEdge
|
|
case rightEdge
|
|
case middle
|
|
case leftHalf
|
|
case rightHalf
|
|
case full
|
|
case offsets(Int, Int)
|
|
|
|
public var isEmpty: Bool {
|
|
switch self {
|
|
case .emptyRange: return true
|
|
default: return false
|
|
}
|
|
}
|
|
|
|
public func range<C : Collection>(in c: C) -> Range<C.Index> {
|
|
switch self {
|
|
case .emptyRange: return c.endIndex..<c.endIndex
|
|
case .leftEdge: return c.startIndex..<c.startIndex
|
|
case .rightEdge: return c.endIndex..<c.endIndex
|
|
case .middle:
|
|
let start = c.index(c.startIndex, offsetBy: c.count / 4)
|
|
let end = c.index(c.startIndex, offsetBy: 3 * c.count / 4 + 1)
|
|
return start..<end
|
|
case .leftHalf:
|
|
let start = c.startIndex
|
|
let end = c.index(start, offsetBy: c.count / 2)
|
|
return start..<end
|
|
case .rightHalf:
|
|
let start = c.index(c.startIndex, offsetBy: c.count / 2)
|
|
let end = c.endIndex
|
|
return start..<end
|
|
case .full:
|
|
return c.startIndex..<c.endIndex
|
|
case let .offsets(lowerBound, upperBound):
|
|
let start = c.index(c.startIndex, offsetBy: numericCast(lowerBound))
|
|
let end = c.index(c.startIndex, offsetBy: numericCast(upperBound))
|
|
return start..<end
|
|
}
|
|
}
|
|
|
|
public func closedRange<C : Collection>(in c: C) -> ClosedRange<C.Index> {
|
|
switch self {
|
|
case .emptyRange: fatalError("Closed range cannot be empty")
|
|
case .leftEdge: return c.startIndex...c.startIndex
|
|
case .rightEdge:
|
|
let beforeEnd = c.index(c.startIndex, offsetBy: c.count - 1)
|
|
return beforeEnd...beforeEnd
|
|
case .middle:
|
|
let start = c.index(c.startIndex, offsetBy: c.count / 4)
|
|
let end = c.index(c.startIndex, offsetBy: 3 * c.count / 4)
|
|
return start...end
|
|
case .leftHalf:
|
|
let start = c.startIndex
|
|
let end = c.index(start, offsetBy: c.count / 2 - 1)
|
|
return start...end
|
|
case .rightHalf:
|
|
let start = c.index(c.startIndex, offsetBy: c.count / 2)
|
|
let beforeEnd = c.index(c.startIndex, offsetBy: c.count - 1)
|
|
return start...beforeEnd
|
|
case .full:
|
|
let beforeEnd = c.index(c.startIndex, offsetBy: c.count - 1)
|
|
return c.startIndex...beforeEnd
|
|
case let .offsets(lowerBound, upperBound):
|
|
let start = c.index(c.startIndex, offsetBy: numericCast(lowerBound))
|
|
let end = c.index(c.startIndex, offsetBy: numericCast(upperBound))
|
|
return start...end
|
|
}
|
|
}
|
|
}
|