intrange

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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: 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.

Keep those divergences: changing them changes which job templates are accepted and the order in which tasks are generated.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonical added in v0.4.0

func Canonical(rs []Range) (string, bool)

Canonical returns the canonical form (Template Schemas §3.4.1.1.1) of the range expression whose elements are rs: a string determined by its list of values alone, so element order, how values are split into elements, and white space do not show in it. The list is split from the left into runs of a constant difference; a run of three or more values is written "x-y" (step 1) or "x-y:s", any shorter run one value at a time, and the runs are joined by "," with no white space.

It is computed from the elements' arithmetic progressions, never by listing the values: a run that follows a progression jumps to its end in one move, so the work is proportional to the number of elements however many values they name.

ok is false when two elements' spans overlap (see Overlaps): their union is then not a concatenation of progressions, and settling it needs the values, so the caller decides what to do. ok is also false for an empty rs.

func CanonicalValues added in v0.4.0

func CanonicalValues(vals []int) string

CanonicalValues returns the canonical form of a set of integers, given in any order and possibly repeated. Distinct values never overlap, so it always has an answer; vals is not modified.

func Overlaps added in v0.4.0

func Overlaps(rs []Range) bool

Overlaps reports whether two elements of a range expression overlap, as Template Schemas §3.4.1.1.1 defines it: an element's span runs from the smallest to the largest value in its set, not between its written endpoints, and two elements overlap when their spans share an integer, even when their sets share no value ("1-9:2,2-10:2" overlaps; "1-10:4,10-15" does not). It sorts once and compares neighbors, so it costs O(n log n) in the number of elements however many values they name.

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

type Range struct {
	Start int
	End   int
	Step  int
}

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

func Parse(s string) ([]Range, error)

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

func ParseWithPolicy(s string, p Policy) ([]Range, error)

ParseWithPolicy is Parse, restricted by p.

func (Range) Count

func (r Range) Count() int

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

func (r Range) Iterate() []int

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 calling this (internal/openjd's sumRangeCounts and internal/openjd/expr's rangeInts), so the early-out is unreachable through them; it gives a caller that omits that bound an empty result instead of an out-of-memory kill. 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.

func (Range) String

func (r Range) String() string

String renders the range in the grammar's own notation, for test names and diagnostics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL