beancount_parser_lima/
trim.rs

1#[cfg(test)]
2use test_case::test_case;
3
4// adjust the span to exclude trailing whitespace
5pub fn trim_trailing_whitespace(content: &str, start: usize, end: usize) -> usize {
6    match &content[start..end].rfind(|c: char| !c.is_whitespace()) {
7        Some(i) => start + *i + 1,
8        None => end,
9    }
10}
11
12#[cfg(test)]
13#[test_case("abc", "abc"; "abc")]
14#[test_case("abc ", "abc"; "abc space")]
15#[test_case("abc\n", "abc"; "abc newline")]
16#[test_case("abc\ndef\n\n", "abc\ndef"; "abc def")]
17#[test_case("abc\n😱\ndef\n", "abc\n😱\ndef"; "abc scream def")]
18fn test_trim_trailing_whitespace(source: &str, expected: &str) {
19    let trimmed_end = trim_trailing_whitespace(source, 0, source.len());
20    assert_eq!(&source[..trimmed_end], expected);
21}