Some small tools to make Ranges easier to work with in Swift.
So far, this is the only feature of this package: some protocols to genericize ranges.
In Swift's standard library, all the range types conform to
RangeExpression. However, this doesn't give you much insight: All it guarantees is that the range's bounds are comparable, that it can contain a value, and that it might be resolved to a
Range within a given collection.
This package adds more protocols. These, for accessing members of a range generically:
RangeProtocol: A protocol to which all ranges, even
NSRange, conform. Also includes info on whether that upper bound is inclusive.
RangeWithLowerBound: Any range which has a lower bound, such as
RangeWithUpperBound: Any range which has an upper bound, such as
RangeWithLowerAndUpperBound: Any range which has both a lower and an upper bound, such as
And these for creating ranges generically:
RangeWhichCanBeInitializedWithOnlyLowerBound: Any range which can be initialized only with a lower bound, like
RangeWhichCanBeInitializedWithOnlyUpperBound: Any range which can be initialized only with an upper bound, like
RangeWhichCanBeInitializedWithBothLowerAndUpperBounds: Any range which can be initialized with both lower and upper bounds, like
|Last commit: 2 weeks ago|
This provides static info on whether that upper bound is inclusive. It will be
true iff the upper bound of this protocol includes the element at its index, like
false indicates that the upper bound does not include that element, like
This patch allows all ranges to specify whether their upper bound is inclusive; in 1.2.0,
upperBoundIsInclusive was only available on
Even when an upper bound isn't explicitly specified, it's still good to acknowledge whether it's inclusive, so we know that things like
myArray = [0,1,2] can return
 validly when accessed like
We might imagine that, given a hypothetical
myArray[3..<] would not validly return
let myArray = [0, 1, 2] myArray[0...] == [0, 1, 2] myArray[1...] == [ 1, 2] myArray[2...] == [ 2] myArray[3...] == [ ] myArray[0..<] == [0, 1] myArray[1..<] == [ 1] myArray[2..<] == [ ] myArray[3..<] == invalid!
So, even if the range doesn't specify its upper bound, we can still gain useful knowledge by understanding whether the upper end of it is inclusive