#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct BomResult {
pub(crate) bom: String,
pub(crate) text: String,
}
pub(crate) fn strip_utf8_bom(text: &str) -> BomResult {
if let Some(stripped) = text.strip_prefix('\u{FEFF}') {
return BomResult {
bom: "\u{FEFF}".to_string(),
text: stripped.to_string(),
};
}
BomResult {
bom: String::new(),
text: text.to_string(),
}
}
pub(crate) fn normalize_to_lf(text: &str) -> String {
text.replace("\r\n", "\n").replace('\r', "\n")
}
pub(crate) fn normalize_for_snapshot_storage(text: &str) -> String {
normalize_to_lf(&strip_utf8_bom(text).text)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn snapshot_normalization_strips_utf8_bom_and_normalizes_lf() {
assert_eq!(
normalize_for_snapshot_storage("\u{FEFF}one\r\ntwo\rthree"),
"one\ntwo\nthree"
);
}
}