svg-path-bbox 0.0.1

SVG paths bounding box calculator in Rust
Documentation
use regex::Regex;

pub fn tokenize(path: &str) -> Vec<Vec<&str>> {
    let re = Regex::new(r"[a-zA-Z]|-?\d*\.?\d+(?:[eE][-+]?\d+)?").unwrap();
    let tokens: Vec<&str> = re.find_iter(path).map(|m| m.as_str()).collect();

    tokens.into_iter().fold(Vec::new(), |mut acc, tok| {
        if tok.chars().all(|c| c.is_alphabetic()) || acc.is_empty() {
            acc.push(vec![tok]);
        } else {
            acc.last_mut().unwrap().push(tok);
        }
        acc
    })
}

#[test]
fn test_tokenize() {
    // https://cdn.simpleicons.org/kotlin
    assert_eq!(
        tokenize("M24 24H0V0h24L12 12Z"),
        vec![
            vec!["M", "24", "24"],
            vec!["H", "0"],
            vec!["V", "0"],
            vec!["h", "24"],
            vec!["L", "12", "12"],
            vec!["Z"]
        ]
    );

    // https://cdn.simpleicons.org/vercel
    assert_eq!(
        tokenize("m12 1.608 12 20.784H0Z"),
        vec![
            vec!["m", "12", "1.608", "12", "20.784"],
            vec!["H", "0"],
            vec!["Z"]
        ]
    );

    assert_eq!(
        tokenize("C1e2 -3E-2 4.5 6 7 8"),
        vec![vec!["C", "1e2", "-3E-2", "4.5", "6", "7", "8"]]
    );
}