Skip to content

RougeWare/Swift-Range-Tools

Repository files navigation

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