use super::types::Eol;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Line {
pub raw: String,
pub eol: Eol,
}
pub(crate) fn split_lines(text: &str) -> Vec<Line> {
let bytes = text.as_bytes();
let mut lines = Vec::new();
let mut start = 0;
let mut i = 0;
let piece = |a: usize, b: usize| text.get(a..b).unwrap_or("").to_string();
while let Some(&c) = bytes.get(i) {
if c == 0x0a {
lines.push(Line { raw: piece(start, i), eol: Eol::Lf });
start = i + 1;
} else if c == 0x0d {
if bytes.get(i + 1) == Some(&0x0a) {
lines.push(Line { raw: piece(start, i), eol: Eol::CrLf });
i += 1;
} else {
lines.push(Line { raw: piece(start, i), eol: Eol::Cr });
}
start = i + 1;
}
i += 1;
}
if start < bytes.len() {
lines.push(Line { raw: piece(start, bytes.len()), eol: Eol::None });
}
lines
}
pub(crate) fn has_line_break(s: &str) -> bool {
s.bytes().any(|c| c == 0x0a || c == 0x0d)
}