#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LineEnding {
#[default]
Lf,
Crlf,
}
impl LineEnding {
pub fn label(self) -> &'static str {
match self {
LineEnding::Lf => "LF",
LineEnding::Crlf => "CRLF",
}
}
pub fn as_str(self) -> &'static str {
match self {
LineEnding::Lf => "\n",
LineEnding::Crlf => "\r\n",
}
}
pub fn detect(text: &str) -> Self {
match text.find('\n') {
Some(0) => LineEnding::Lf,
Some(index) if text.as_bytes()[index - 1] == b'\r' => LineEnding::Crlf,
Some(_) => LineEnding::Lf,
None => LineEnding::Lf,
}
}
}