stybulate
stybulate copied to clipboard
panic: 'attempt to subtract with overflow'
Describe the bug Formatting a cell containing some unicode escape characters causes a panic
To Reproduce
fn main() {
use stybulate::{Cell, Table, Style};
//let s = "foo \u{1b} bar";
let s = "\u{1b}";
let v = vec![vec![
Cell::from(s),
]];
let result = Table::new(
Style::Plain,
v,
None,
)
.tabulate();
}
Fails with
thread 'main' panicked at 'attempt to subtract with overflow', /home/mcgibbon/.cargo/registry/src/github.com-1ecc6299db9ec823/stybulate-1.1.2/src/lib.rs:368:21
stack backtrace:
0: rust_begin_unwind
1: core::panicking::panic_fmt
2: core::panicking::panic
3: stybulate::format_unstylable
at /home/mcgibbon/.cargo/registry/src/github.com-1ecc6299db9ec823/stybulate-1.1.2/src/lib.rs:368:21
4: stybulate::create_data_lines::{{closure}}
at /home/mcgibbon/.cargo/registry/src/github.com-1ecc6299db9ec823/stybulate-1.1.2/src/lib.rs:421:17
5: core::iter::adapters::map::map_fold::{{closure}}
at /build/rustc-1.58.1-src/library/core/src/iter/adapters/map.rs:84:28
6: <core::iter::adapters::enumerate::Enumerate<I> as core::iter::traits::iterator::Iterator>::fold::enumerate::{{closure}}
at /build/rustc-1.58.1-src/library/core/src/iter/adapters/enumerate.rs:106:27
7: core::iter::traits::iterator::Iterator::fold
at /build/rustc-1.58.1-src/library/core/src/iter/traits/iterator.rs:2171:21
8: <core::iter::adapters::enumerate::Enumerate<I> as core::iter::traits::iterator::Iterator>::fold
at /build/rustc-1.58.1-src/library/core/src/iter/adapters/enumerate.rs:112:9
9: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
at /build/rustc-1.58.1-src/library/core/src/iter/adapters/map.rs:124:9
10: core::iter::traits::iterator::Iterator::for_each
at /build/rustc-1.58.1-src/library/core/src/iter/traits/iterator.rs:737:9
11: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
at /build/rustc-1.58.1-src/library/alloc/src/vec/spec_extend.rs:40:17
12: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
at /build/rustc-1.58.1-src/library/alloc/src/vec/spec_from_iter_nested.rs:56:9
13: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
at /build/rustc-1.58.1-src/library/alloc/src/vec/spec_from_iter.rs:33:9
14: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
at /build/rustc-1.58.1-src/library/alloc/src/vec/mod.rs:2549:9
15: core::iter::traits::iterator::Iterator::collect
at /build/rustc-1.58.1-src/library/core/src/iter/traits/iterator.rs:1745:9
16: stybulate::create_data_lines
at /home/mcgibbon/.cargo/registry/src/github.com-1ecc6299db9ec823/stybulate-1.1.2/src/lib.rs:410:33
17: stybulate::Table::tabulate
at /home/mcgibbon/.cargo/registry/src/github.com-1ecc6299db9ec823/stybulate-1.1.2/src/lib.rs:243:25
18: history::main
at ./src/main.rs:30:18
19: core::ops::function::FnOnce::call_once
at /build/rustc-1.58.1-src/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Toolchain: 1.58
I believe I was able to fix this in my code by running the strings through this before adding them to the table.
fn remove_zero_width_graphemes(s: &str) -> String {
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
UnicodeSegmentation::graphemes(s, true)
.map(|x| match UnicodeWidthStr::width(x) {
0 => "",
_ => x,
})
.collect()
}