47
return new OffsetRange(start, endExclusive);
48
}
50
>
public static ofLength(length: number): OffsetRange {
51
return new OffsetRange(0, length);
52
}
54
>
public static ofStartAndLength(start: number, length: number): OffsetRange {
55
return new OffsetRange(start, start + length);
56
}
58
>
public static emptyAt(offset: number): OffsetRange {
59
return new OffsetRange(offset, offset);
60
}
62
>
constructor(public readonly start: number, public readonly endExclusive: number) {
63
if (start > endExclusive) {
64
throw new BugIndicatingError(`Invalid range: ${this.toString()}`);
65
}
66
}
68
>
get isEmpty(): boolean {
69
return this.start === this.endExclusive;
70
}
72
>
public delta(offset: number): OffsetRange {
73
return new OffsetRange(this.start + offset, this.endExclusive + offset);
74
}
76
>
public deltaStart(offset: number): OffsetRange {
77
return new OffsetRange(this.start + offset, this.endExclusive);
78
}
80
>
public deltaEnd(offset: number): OffsetRange {
81
return new OffsetRange(this.start, this.endExclusive + offset);
82
}
84
>
public get length(): number {
85
return this.endExclusive - this.start;
86
}
88
>
public toString() {
89
return `[${this.start}, ${this.endExclusive})`;
90
}
92
>
public equals(other: OffsetRange): boolean {
93
return this.start === other.start && this.endExclusive === other.endExclusive;
94
}
96
>
public containsRange(other: OffsetRange): boolean {
97
return this.start <= other.start && other.endExclusive <= this.endExclusive;
98
}
100
>
public contains(offset: number): boolean {
101
return this.start <= offset && offset < this.endExclusive;
102
}
104
>
/**
105
>
* for all numbers n: range1.contains(n) or range2.contains(n) => range1.join(range2).contains(n)
106
>
* The joined range is the smallest range that contains both ranges.
107
>
*/
108
>
public join(other: OffsetRange): OffsetRange {
109
return new OffsetRange(Math.min(this.start, other.start), Math.max(this.endExclusive, other.endExclusive));
110
}
112
>
/**
113
>
* for all numbers n: range1.contains(n) and range2.contains(n) <=> range1.intersect(range2).contains(n)
114
>
*
115
>
* The resulting range is empty if the ranges do not intersect, but touch.
116
>
* If the ranges don't even touch, the result is undefined.
117
>
*/
118
>
public intersect(other: OffsetRange): OffsetRange | undefined {
119
const start = Math.max(this.start, other.start);
120
const end = Math.min(this.endExclusive, other.endExclusive);