pub fn sanitize_spd(input: &str) -> String {
let mut out = String::with_capacity(input.len());
let ends_with_newline = input.ends_with('\n');
let mut lines = input.split('\n').peekable();
while let Some(line) = lines.next() {
if line.trim_start().starts_with('#') {
continue;
}
out.push_str(line);
if lines.peek().is_some() {
out.push('\n');
}
}
if ends_with_newline && !out.ends_with('\n') {
out.push('\n');
}
out
}
#[cfg(test)]
mod tests {
use super::sanitize_spd;
#[test]
fn strips_leading_comment_block() {
let input = "# header\n# meta\n380\t0.5\n385\t0.6\n";
assert_eq!(sanitize_spd(input), "380\t0.5\n385\t0.6\n");
}
#[test]
fn strips_comment_with_leading_whitespace() {
let input = "380\t0.5\n # inline comment\n385\t0.6\n";
assert_eq!(sanitize_spd(input), "380\t0.5\n385\t0.6\n");
}
#[test]
fn preserves_data_only_input() {
let input = "380\t0.5\n385\t0.6\n";
assert_eq!(sanitize_spd(input), input);
}
#[test]
fn empty_stays_empty() {
assert_eq!(sanitize_spd(""), "");
}
#[test]
fn preserves_trailing_newline_absence() {
let input = "# c\n380\t0.5";
assert_eq!(sanitize_spd(input), "380\t0.5");
}
#[test]
fn keeps_blank_lines_as_is() {
let input = "# c\n380\t0.5\n\n385\t0.6\n";
assert_eq!(sanitize_spd(input), "380\t0.5\n\n385\t0.6\n");
}
}