RangeTools

1.2.1

Some tools to help you work with Swift ranges
RougeWare/Swift-Range-Tools

What's New

1.2 - `upperBoundIsInclusive`

2021-06-08T23:05:16Z

This provides static info on whether that upper bound is inclusive. It will betrue iff the upper bound of this protocol includes the element at its index, like ...b and a...b. false indicates that the upper bound does not include that element, like ..<b and a..<b.


1.2.1

This patch allows all ranges to specify whether their upper bound is inclusive; in 1.2.0, upperBoundIsInclusive was only available on RangeWithUpperBound.

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 myArray[3...].

We might imagine that, given a hypothetical a..< range, 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

Tested on GitHub Actions

Swift 5 swift package manager 5.3 is supported Supports macOS, iOS, tvOS, watchOS, Linux, & Windows

Swift RangeTools

Some small tools to make Ranges easier to work with in Swift.

Protocols

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 a..., a..<b, and a...b
  • RangeWithUpperBound: Any range which has an upper bound, such as ..<b, ...b, a..<b, and a...b
  • RangeWithLowerAndUpperBound: Any range which has both a lower and an upper bound, such as a..<b and a...b

And these for creating ranges generically:

  • RangeWhichCanBeInitializedWithOnlyLowerBound: Any range which can be initialized only with a lower bound, like a...
  • RangeWhichCanBeInitializedWithOnlyUpperBound: Any range which can be initialized only with an upper bound, like ..<b or ...b
  • RangeWhichCanBeInitializedWithBothLowerAndUpperBounds: Any range which can be initialized with both lower and upper bounds, like a..<b or a...b

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Oct 20 2024 15:58:30 GMT-0900 (Hawaii-Aleutian Daylight Time)