Function stanza::renderer::split_preserving_whitespace
source · [−]Expand description
Splits a string slice by whitespace, while preserving extraneous whitespace that appears after the first encountered separator.
This splitter has the convenient property that the number of whitespace characters in the input can be deterministically obtained by counting the number of whitespace characters in the output fragments and adding the number of fragments, less one.
Examples
use stanza::renderer::split_preserving_whitespace;
let input = " what a wonderful day ";
let output = split_preserving_whitespace(input);
assert_eq!(vec![" what", "a", " wonderful", "day", ""], output);
fn count_whitespace(s: &str) -> usize {
s.chars().filter(|ch| ch.is_whitespace()).count()
}
let whitespace_in_input = count_whitespace(input);
let whitespace_in_output = output.iter().map(|frag| count_whitespace(frag)).sum::<usize>();
assert_eq!(whitespace_in_input, whitespace_in_output + output.len() - 1);