negateRanges function
Inverts ranges within the bounds from minValue to maxValue.
Returns a new list with inverted ranges.
Implementation
Ranges negateRanges(
Ranges ranges, {
int maxValue = 0x10FFFF,
int minValue = 0,
}) {
if (ranges.isEmpty) {
return [(minValue, maxValue)];
}
final normalized = normalizeRanges(ranges);
final first = normalized.first;
final last = normalized.last;
if (first.$1 < minValue) {
throw ArgumentError(
'The lowest range bound (${first.$1}) must not be less than minValue ($minValue)',
);
}
if (last.$2 > maxValue) {
throw ArgumentError(
'The highest range bound (${last.$2}) must not be greater than maxValue ($maxValue)',
);
}
final result = <Range>[];
var currentStart = minValue;
for (int i = 0; i < normalized.length; i++) {
final range = normalized[i];
final start = range.$1;
final end = range.$2;
if (start > currentStart) {
result.add((currentStart, start - 1));
}
if (end >= currentStart) {
currentStart = end + 1;
}
}
if (currentStart <= maxValue) {
result.add((currentStart, maxValue));
}
return result;
}