Documentation
¶
Overview ¶
Package intrange implements the OpenJD base specification's <IntRangeExpr> grammar (2023-09 Template Schemas, section 3.4.1.1.1): the succinct "1,3,7-10:2" form used for frame ranges.
It is a leaf shared by two callers that DISAGREE about what to accept, which is why Parse is spec-faithful and ParseWithPolicy exists:
- internal/openjd/expr (the EXPR extension) needs the spec's behavior, and uses Parse.
- internal/openjd (task-parameter expansion) is stricter than the spec in two ways that predate this package: it rejects a range whose start exceeds its end (the spec makes "1 - -1" the single value [1]) and it rejects a negative step (the spec's set formula defines one). It also orders values first-seen rather than increasing, which it does in its own expansion code rather than here.
Those divergences are deliberate to preserve, NOT bugs to fix in passing: changing them changes which job templates are accepted and the order in which tasks are generated. They are recorded in the sub-project B2 design document.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Policy ¶
type Policy struct {
// PositiveStepOnly rejects a zero or negative step.
PositiveStepOnly bool
// AscendingOnly rejects an element whose start exceeds its end.
AscendingOnly bool
}
Policy narrows what ParseWithPolicy accepts below what the specification permits. The zero Policy accepts everything the specification does.
type Range ¶
Range is one element of a range expression: a stepped sequence of integers anchored at Start. Step is non-zero and may be negative.
func Parse ¶
Parse reads a range expression into its elements, accepting everything the specification permits. It does not expand them; a caller decides its own ordering, de-duplication and size bound.
func ParseWithPolicy ¶
ParseWithPolicy is Parse, restricted by p.
func (Range) Count ¶
Count returns how many integers the range yields, computed arithmetically without allocating — the allocation-free equivalent of len(r.Iterate()), with one exception at the top of the scale: a count that SATURATES (below) is one Iterate declines to materialize at all, so it answers nil rather than that many values.
The specification's set is {x} union {x+mn : m in Z+, x+mn <= y if n > 0, x+mn >= y if n < 0}, so the start is ALWAYS a member and the count is never zero. If the span, or the arithmetic needed to turn it into a count, would overflow, Count saturates to math.MaxInt, which exceeds any caller's bound.
func (Range) Iterate ¶
Iterate yields every integer in the range, in the order the step defines.
A range whose Range.Count SATURATES to math.MaxInt cannot be materialized on any machine, so Iterate returns nil for it rather than trying: the loops below would otherwise append until the process ran out of memory. Both callers in this repository bound Count() themselves before ever calling this (internal/openjd's sumRangeCounts and internal/openjd/expr's rangeInts), so the early-out is unreachable through them — it exists so that a caller which FORGETS that bound gets an empty result instead of an out-of-memory kill, which is the only outcome the previous code could offer. It is a backstop, not a substitute for the bound: a range of, say, ten billion values does not saturate and is returned in full, one int per value.