pub fn is_display_separator(c: char) -> bool {
c.is_whitespace() || c == '-' || c == ','
}
pub fn render_grouped(s: &str, group_size: usize, separator: char) -> String {
if group_size == 0 {
return s.to_string();
}
let mut out = String::with_capacity(s.len() + s.len() / group_size);
for (i, ch) in s.chars().enumerate() {
if i > 0 && i % group_size == 0 {
out.push(separator);
}
out.push(ch);
}
out
}
pub fn strip_display_separators(s: &str) -> String {
s.chars().filter(|&c| !is_display_separator(c)).collect()
}
pub fn parse_separator(s: &str) -> Result<char, String> {
match s {
"space" | " " => Ok(' '),
"hyphen" | "-" => Ok('-'),
"comma" | "," => Ok(','),
other => Err(format!(
"invalid separator {other:?}; expected one of: space|hyphen|comma (or the literal char)"
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn render_grouped_separators_and_unbroken() {
assert_eq!(render_grouped("abcdefghij", 5, ' '), "abcde fghij");
assert_eq!(render_grouped("abcdefghij", 5, '-'), "abcde-fghij");
assert_eq!(render_grouped("abcdefghij", 5, ','), "abcde,fghij");
assert_eq!(render_grouped("abcdefghij", 0, ' '), "abcdefghij");
assert_eq!(render_grouped("abcde", 5, ' '), "abcde");
assert_eq!(render_grouped("abcdefg", 3, '-'), "abc-def-g");
assert_eq!(render_grouped("", 5, ' '), "");
}
#[test]
fn strip_display_separators_ws_hyphen_comma() {
assert_eq!(strip_display_separators("ab cd-ef,gh"), "abcdefgh");
assert_eq!(strip_display_separators("mk1\tqp\r\nzr"), "mk1qpzr");
let once = strip_display_separators("a b-c,d");
assert_eq!(strip_display_separators(&once), once);
}
#[test]
fn parse_separator_keyword_and_literal() {
assert_eq!(parse_separator("space").unwrap(), ' ');
assert_eq!(parse_separator(" ").unwrap(), ' ');
assert_eq!(parse_separator("hyphen").unwrap(), '-');
assert_eq!(parse_separator("comma").unwrap(), ',');
assert!(parse_separator("bogus").is_err());
}
}
#[cfg(test)]
mod conformance {
use super::{render_grouped, strip_display_separators};
fn decode(f: &str) -> String {
if f == "<empty>" {
return String::new();
}
f.replace("<sp>", " ")
.replace("<tab>", "\t")
.replace("<lf>", "\n")
.replace("<cr>", "\r")
}
fn sep(k: &str) -> char {
match k {
"space" => ' ',
"hyphen" => '-',
"comma" => ',',
"none" => ' ',
o => panic!("unknown separator keyword: {o}"),
}
}
#[test]
fn conformance_vectors_pass() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../design/display-grouping-vectors.tsv"
);
let text = std::fs::read_to_string(path).unwrap_or_else(|e| panic!("read {path}: {e}"));
let mut lines = text.lines();
assert_eq!(
lines.next().expect("header"),
"op\tinput\tgroup_size\tseparator\texpected\tnote",
"vector header drift"
);
let mut n = 0usize;
for (i, line) in lines.enumerate() {
if line.is_empty() {
continue;
}
let c: Vec<&str> = line.split('\t').collect();
assert_eq!(c.len(), 6, "row {} not 6 fields: {line:?}", i + 2);
let (op, input, gs, s, exp, note) =
(c[0], decode(c[1]), c[2], c[3], decode(c[4]), c[5]);
let gs: usize = gs
.parse()
.unwrap_or_else(|_| panic!("row {}: bad group_size", i + 2));
let got = match op {
"render" => render_grouped(&input, gs, sep(s)),
"strip" => strip_display_separators(&input),
o => panic!("row {}: unknown op {o:?}", i + 2),
};
assert_eq!(got, exp, "row {} ({note})", i + 2);
n += 1;
}
assert!(n >= 20, "expected >=20 rows, got {n}");
}
}