fn clamp_end(index: usize, ranges: &[Range<usize>]) -> Option<usize>
Decrease the index until it is inside or one past the end of one of the ranges.
The ranges must be sorted for this to work correctly.