pub const DEFAULT_TAB_INTERVAL: u16 = 8;
const WORD_BITS: usize = u64::BITS as usize;
#[derive(Debug, Clone)]
pub struct TabStops {
stops: Vec<u64>,
width: u16,
next_stop: Vec<u16>,
next_unclamped: Vec<u16>,
prev_stop: Vec<u16>,
interval: u16,
}
#[allow(dead_code)] impl TabStops {
pub fn new(width: u16, interval: u16) -> Self {
let interval = interval.max(1);
let words = width.div_ceil(WORD_BITS as u16) as usize;
let mut s = Self {
stops: vec![0u64; words],
width,
next_stop: Vec::new(),
next_unclamped: Vec::new(),
prev_stop: Vec::new(),
interval,
};
s.init_default(0, width);
s.rebuild_neighbor_tables();
s
}
pub fn default_for(width: u16) -> Self {
Self::new(width, DEFAULT_TAB_INTERVAL)
}
pub fn resize(&mut self, width: u16) {
let old = self.width;
if width == old {
return;
}
let words = width.div_ceil(WORD_BITS as u16) as usize;
self.stops.resize(words, 0);
if width < old {
let last_bit = width as usize;
if last_bit < self.stops.len() * WORD_BITS {
let word_idx = last_bit / WORD_BITS;
let bit_idx = last_bit % WORD_BITS;
if let Some(w) = self.stops.get_mut(word_idx) {
let mask = (1u64 << bit_idx).wrapping_sub(1);
*w &= mask;
}
for w in &mut self.stops[word_idx + 1..] {
*w = 0;
}
}
}
self.width = width;
if width > old {
self.init_default(old, width);
}
self.rebuild_neighbor_tables();
}
pub fn width(&self) -> u16 {
self.width
}
pub fn is_stop(&self, x: u16) -> bool {
if x >= self.width {
return false;
}
let idx = (x as usize) / WORD_BITS;
let bit = (x as usize) % WORD_BITS;
(self.stops[idx] >> bit) & 1 != 0
}
pub fn next(&self, x: u16) -> u16 {
let w = self.width();
if w == 0 {
return x;
}
let idx = (x as usize).min(self.next_stop.len() - 1);
self.next_stop[idx]
}
pub fn next_stop(&self, x: u16) -> u16 {
let interval = self.interval;
if x >= self.width {
return (x / interval + 1) * interval;
}
self.next_unclamped[x as usize]
}
pub fn prev(&self, x: u16) -> u16 {
if x == 0 {
return 0;
}
let len = self.prev_stop.len();
if len == 0 {
return 0;
}
let idx = (x as usize).min(len - 1);
self.prev_stop[idx]
}
pub fn set(&mut self, x: u16) {
if x >= self.width {
return;
}
let idx = (x as usize) / WORD_BITS;
let bit = (x as usize) % WORD_BITS;
self.stops[idx] |= 1u64 << bit;
self.rebuild_neighbor_tables();
}
pub fn reset(&mut self, x: u16) {
if x >= self.width {
return;
}
let idx = (x as usize) / WORD_BITS;
let bit = (x as usize) % WORD_BITS;
self.stops[idx] &= !(1u64 << bit);
self.rebuild_neighbor_tables();
}
pub fn clear(&mut self) {
for w in &mut self.stops {
*w = 0;
}
self.rebuild_neighbor_tables();
}
fn init_default(&mut self, from: u16, to: u16) {
let interval = self.interval as usize;
let mut x = from as usize;
while x < to as usize {
if x.is_multiple_of(interval) {
let idx = x / WORD_BITS;
let bit = x % WORD_BITS;
self.stops[idx] |= 1u64 << bit;
}
x += 1;
}
}
fn rebuild_neighbor_tables(&mut self) {
let w = self.width as usize;
self.next_stop.resize(w, 0);
self.next_unclamped.resize(w, 0);
self.prev_stop.resize(w, 0);
if w == 0 {
return;
}
let last = (w - 1) as u16;
let interval = self.interval;
let mut nearest: u16 = last;
let mut nearest_real: Option<u16> = None;
for x in (0..w).rev() {
self.next_stop[x] = nearest;
self.next_unclamped[x] = match nearest_real {
Some(s) => s,
None => (x as u16 / interval + 1) * interval,
};
if self.bit(x) {
nearest = x as u16;
nearest_real = Some(x as u16);
}
}
let mut nearest: u16 = 0;
for x in 0..w {
self.prev_stop[x] = nearest;
if self.bit(x) {
nearest = x as u16;
}
}
}
#[inline]
fn bit(&self, x: usize) -> bool {
let idx = x / WORD_BITS;
let bit = x % WORD_BITS;
(self.stops[idx] >> bit) & 1 != 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_stops_every_eight() {
let ts = TabStops::default_for(32);
for c in 0..32 {
assert_eq!(ts.is_stop(c), c.is_multiple_of(8), "col {c}");
}
}
#[test]
fn next_returns_following_stop() {
let ts = TabStops::default_for(32);
assert_eq!(ts.next(0), 8);
assert_eq!(ts.next(7), 8);
assert_eq!(ts.next(8), 16);
assert_eq!(ts.next(30), 31);
}
#[test]
fn next_stop_is_unclamped_past_the_edge() {
let ts = TabStops::default_for(46);
assert_eq!(ts.next(24), 32);
assert_eq!(ts.next_stop(24), 32); assert_eq!(ts.next(40), 45); assert_eq!(ts.next_stop(40), 48); assert_eq!(ts.next_stop(41), 48);
assert_eq!(ts.next_stop(45), 48);
assert_eq!(ts.next_stop(46), 48);
assert_eq!(ts.next_stop(48), 56);
}
#[test]
fn prev_returns_preceding_stop() {
let ts = TabStops::default_for(32);
assert_eq!(ts.prev(0), 0);
assert_eq!(ts.prev(1), 0);
assert_eq!(ts.prev(8), 0);
assert_eq!(ts.prev(9), 8);
assert_eq!(ts.prev(17), 16);
}
#[test]
fn set_and_clear_individual_stops() {
let mut ts = TabStops::default_for(16);
ts.reset(8);
assert!(!ts.is_stop(8));
assert_eq!(ts.next(0), 15); ts.set(5);
assert!(ts.is_stop(5));
assert_eq!(ts.next(0), 5);
ts.clear();
for c in 0..16 {
assert!(!ts.is_stop(c));
}
}
#[test]
fn resize_preserves_existing_stops() {
let mut ts = TabStops::default_for(16);
ts.set(5);
ts.resize(32);
assert!(ts.is_stop(5));
assert!(ts.is_stop(24));
}
#[test]
fn prev_does_not_panic_on_zero_width() {
let ts = TabStops::new(0, DEFAULT_TAB_INTERVAL);
assert_eq!(ts.prev(0), 0);
assert_eq!(ts.prev(5), 0);
assert_eq!(ts.prev(u16::MAX), 0);
}
#[test]
fn prev_clamps_x_above_width() {
let ts = TabStops::default_for(16);
assert_eq!(ts.prev(u16::MAX), 8);
assert_eq!(ts.prev(16), 8);
}
#[test]
fn tabstops_custom_interval_is_stop() {
let mut ts = TabStops::new(16, 4);
for &(col, want) in &[
(0u16, true),
(3, false),
(4, true),
(7, false),
(8, true),
(12, true),
(15, false),
] {
assert_eq!(ts.is_stop(col), want, "col {col}");
}
let custom = 5;
ts.set(custom);
assert!(ts.is_stop(custom));
let regular = 4;
ts.reset(regular);
assert!(!ts.is_stop(regular));
}
#[test]
fn tabstops_default_interval_is_eight() {
assert_eq!(DEFAULT_TAB_INTERVAL, 8);
let ts = TabStops::default_for(24);
for &(col, want) in &[
(0u16, true),
(7, false),
(8, true),
(15, false),
(16, true),
(23, false),
] {
assert_eq!(ts.is_stop(col), want, "col {col}");
}
}
#[test]
fn tabstops_clear_unsets_all_default_stops() {
let mut ts = TabStops::default_for(24);
assert!(ts.is_stop(0) && ts.is_stop(8) && ts.is_stop(16));
ts.clear();
for c in 0..24 {
assert!(!ts.is_stop(c), "col {c} still set after clear");
}
}
#[test]
fn tabstops_resize_grow_initializes_new_default_stops() {
let mut ts = TabStops::default_for(16);
ts.resize(24);
assert_eq!(ts.width(), 24);
for &(col, want) in &[(0u16, true), (8, true), (16, true), (23, false)] {
assert_eq!(ts.is_stop(col), want, "col {col}");
}
}
#[test]
fn tabstops_resize_same_size_is_noop() {
let mut ts = TabStops::default_for(16);
ts.resize(16);
assert_eq!(ts.width(), 16);
assert!(ts.is_stop(0));
assert!(ts.is_stop(8));
assert!(!ts.is_stop(15));
}
#[test]
fn tabstops_resize_with_custom_interval_keeps_interval() {
let mut ts = TabStops::new(8, 4);
ts.resize(16);
assert_eq!(ts.width(), 16);
for &(col, want) in &[(0u16, true), (4, true), (8, true), (12, true), (15, false)] {
assert_eq!(ts.is_stop(col), want, "col {col}");
}
}
#[test]
fn tabstops_resize_to_zero_drops_all_stops() {
let mut ts = TabStops::default_for(8);
ts.resize(0);
assert_eq!(ts.width(), 0);
assert!(!ts.is_stop(0));
}
#[test]
fn tabstops_resize_to_very_large_keeps_default_interval() {
let mut ts = TabStops::default_for(8);
ts.resize(1000);
assert!(ts.is_stop(992));
assert!(!ts.is_stop(999));
}
#[test]
fn tabstops_multiple_resizes_keep_first_stop_set() {
let mut ts = TabStops::default_for(8);
for size in [16u16, 8, 24, 4] {
ts.resize(size);
assert_eq!(ts.width(), size);
assert!(ts.is_stop(0), "first stop missing after resize to {size}");
}
}
}