Range
Range is a half-open range of indexes, [loc, loc + len) (NSRange in
shape). It is a class rather than a struct so ranges can live in an
Array, a Set or a
Map: a wrapped line, a selected block of rows and a run
of styled characters are each a list of ranges. From 0.72.
#import "Range.xc" // or the Foundation umbrellaOverview
Section titled “Overview”Range* r = Range.make((i32)3, (i32)4); // [3, 7)r.contains((i32)3); // truer.contains((i32)7); // false: end() is not in the rangeRange* both = r.intersection(Range.make((i32)5, (i32)10)); // [5, 7)Half-open is the contract. loc is in the range and end is not.
An empty range has len 0, and adjacent ranges meet with a.end() == b.loc:
no gap and no overlap.
Equal by value. Two ranges are equal when their loc and len are, and
hash agrees, so a range works as a Set member or a Map key by
value.
Topics
Section titled “Topics”Creating · make · loc and len
Testing · end · isEmpty · contains · overlaps
Combining · intersection · unionWith
Creating
Section titled “Creating”static Range* make(i32 loc, i32 len)A new range starting at loc, len long.
loc and len
Section titled “loc and len”i32 loc;i32 len;The first index and the length. new Range() is the empty range at 0.
Testing
Section titled “Testing”i32 end(void)One past the last index: loc + len.
isEmpty
Section titled “isEmpty”bool isEmpty(void)Whether len is zero (or negative).
contains
Section titled “contains”bool contains(i32 i)Whether loc <= i < end().
overlaps
Section titled “overlaps”bool overlaps(Range* o)Whether the two cover an index in common. Ranges that only touch end to end
([0,3) and [3,5)) do not overlap, and an empty range overlaps nothing, even
a range it sits inside. A null o overlaps nothing.
Combining
Section titled “Combining”intersection
Section titled “intersection”Range* intersection(Range* o)A new range of the indexes both cover, or an empty range at the later start when they share none.
unionWith
Section titled “unionWith”Range* unionWith(Range* o)A new range, the smallest covering both, including any gap between them. An
empty or null o gives a copy of this range.
Equality
Section titled “Equality”equals
Section titled “equals”bool equals(Object* other)Whether other is a Range with the same loc and len.
u32 hash(void) // u8 on the 6502A hash of loc and len; equal ranges hash equally.