Skip to content

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 umbrella
Range* r = Range.make((i32)3, (i32)4); // [3, 7)
r.contains((i32)3); // true
r.contains((i32)7); // false: end() is not in the range
Range* 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.

Creating · make · loc and len

Testing · end · isEmpty · contains · overlaps

Combining · intersection · unionWith

Equality · equals · hash


static Range* make(i32 loc, i32 len)

A new range starting at loc, len long.

i32 loc;
i32 len;

The first index and the length. new Range() is the empty range at 0.

↑ Topics

i32 end(void)

One past the last index: loc + len.

bool isEmpty(void)

Whether len is zero (or negative).

bool contains(i32 i)

Whether loc <= i < end().

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.

↑ Topics

Range* intersection(Range* o)

A new range of the indexes both cover, or an empty range at the later start when they share none.

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.

↑ Topics

bool equals(Object* other)

Whether other is a Range with the same loc and len.

u32 hash(void) // u8 on the 6502

A hash of loc and len; equal ranges hash equally.

↑ Topics