diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2021-08-17 01:41:28 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2021-08-17 01:41:28 +0800 |
commit | c9dcea32e6510707ce9228d3bca94d0238f01ac1 (patch) | |
tree | 54f8a9c2020ce2c55be9d51894340bfb6d6e19aa | |
parent | c87fe39449c6df3f3eb5ecca842adf824dc3264b (diff) |
Add test for issue#38 and remove panicking code due to char boundary error
-rw-r--r-- | svgbob/src/buffer/cell_buffer.rs | 25 | ||||
-rw-r--r-- | svgbob/tests/issue38.rs | 41 |
2 files changed, 62 insertions, 4 deletions
diff --git a/svgbob/src/buffer/cell_buffer.rs b/svgbob/src/buffer/cell_buffer.rs index 10eb165..de09bc2 100644 --- a/svgbob/src/buffer/cell_buffer.rs +++ b/svgbob/src/buffer/cell_buffer.rs @@ -19,6 +19,7 @@ use std::{ fmt, ops::{Deref, DerefMut}, }; +use unicode_width::UnicodeWidthStr; mod cell; mod contacts; @@ -537,13 +538,29 @@ impl CellBuffer { if char_locs.is_empty() { no_escaped_text = raw.to_string(); } else { + println!("raw: {}", raw); + dbg!(&input_chars); for (start, end) in char_locs.iter() { - let escaped = raw[*start + 1..*end].to_string(); + let escaped = input_chars[*start + 1..*end].iter().fold( + String::new(), + |mut acc, c| { + acc.push(*c); + acc + }, + ); + let escaped_unicode_width = escaped.width(); let cell = Cell::new(*start as i32, line as i32); escaped_text.push((cell, escaped)); - - no_escaped_text.push_str(&raw[index..*start]); - no_escaped_text.push_str(&" ".repeat(end + 1 - start)); + no_escaped_text += &input_chars[index..*start].iter().fold( + String::new(), + |mut acc, c| { + acc.push(*c); + acc + }, + ); + + // we add 2 to account for the double quotes on end of the escaped string + no_escaped_text += &" ".repeat(escaped_unicode_width + 2); index = end + 1; } // the rest of the text diff --git a/svgbob/tests/issue38.rs b/svgbob/tests/issue38.rs new file mode 100644 index 0000000..936e552 --- /dev/null +++ b/svgbob/tests/issue38.rs @@ -0,0 +1,41 @@ +#[test] +fn cjk_errors() { + let input = r#" + +----+ + | 一 | + +----+ + + +--------+ + |"""一"""| + +--------+ + "#; + + let out = svgbob::to_svg_string_compressed(input); + println!("\n{}\n", out); + dbg!(&out); + + panic!(); +} + +#[test] +fn german_umlauts() { + let input = r#" ++----------+ +| ÖÄÜ | ++----------+ + ++----------+ +| "ÖÄÜ" | ++----------+ + ++----------+ +| "ÖÄÜ" | ++----------+ + "#; + + let out = svgbob::to_svg_string_compressed(input); + println!("\n{}\n", out); + dbg!(&out); + + panic!(); +} |