Skip to main content

object_rainbow/
ascii.rs

1use crate::{
2    map_extra::{SmExtra, StaticMap},
3    zero_terminated::Zt,
4};
5
6pub struct StaticAsciiSplit;
7
8impl<S: AsRef<str>> StaticMap<S> for StaticAsciiSplit {
9    type Mapped = Vec<Zt<String>>;
10
11    fn static_map(x: S) -> Self::Mapped {
12        x.as_ref()
13            .split(['\0', '\t', '\n', '\x0C', '\r', ' '].as_slice())
14            .filter(|s| !s.is_empty())
15            .map(|x| Zt::new(String::from(x)).expect("no zeroes allowed here"))
16            .collect()
17    }
18}
19
20pub type AsciiSplit = SmExtra<StaticAsciiSplit>;
21
22#[test]
23fn ascii_split1() -> crate::Result<()> {
24    use crate::{
25        map_extra::Compose,
26        tuple_extra::{Map1, OneCrossN},
27    };
28    type AsciiSplit1 = Compose<Map1<AsciiSplit>, OneCrossN>;
29    assert_eq!(
30        AsciiSplit1::static_map((1, "a b")),
31        [(1, "a".parse()?), (1, "b".parse()?)],
32    );
33    Ok(())
34}