|
|
@ -10,12 +10,18 @@ pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
|
|
|
|
pub enum LineEnding {
|
|
|
|
pub enum LineEnding {
|
|
|
|
Crlf, // CarriageReturn followed by LineFeed
|
|
|
|
Crlf, // CarriageReturn followed by LineFeed
|
|
|
|
LF, // U+000A -- LineFeed
|
|
|
|
LF, // U+000A -- LineFeed
|
|
|
|
VT, // U+000B -- VerticalTab
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
FF, // U+000C -- FormFeed
|
|
|
|
VT, // U+000B -- VerticalTab
|
|
|
|
CR, // U+000D -- CarriageReturn
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Nel, // U+0085 -- NextLine
|
|
|
|
FF, // U+000C -- FormFeed
|
|
|
|
LS, // U+2028 -- Line Separator
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
PS, // U+2029 -- ParagraphSeparator
|
|
|
|
CR, // U+000D -- CarriageReturn
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
|
|
|
|
Nel, // U+0085 -- NextLine
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
|
|
|
|
LS, // U+2028 -- Line Separator
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
|
|
|
|
PS, // U+2029 -- ParagraphSeparator
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl LineEnding {
|
|
|
|
impl LineEnding {
|
|
|
@ -32,11 +38,17 @@ impl LineEnding {
|
|
|
|
match self {
|
|
|
|
match self {
|
|
|
|
Self::Crlf => "\u{000D}\u{000A}",
|
|
|
|
Self::Crlf => "\u{000D}\u{000A}",
|
|
|
|
Self::LF => "\u{000A}",
|
|
|
|
Self::LF => "\u{000A}",
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Self::VT => "\u{000B}",
|
|
|
|
Self::VT => "\u{000B}",
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Self::FF => "\u{000C}",
|
|
|
|
Self::FF => "\u{000C}",
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Self::CR => "\u{000D}",
|
|
|
|
Self::CR => "\u{000D}",
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Self::Nel => "\u{0085}",
|
|
|
|
Self::Nel => "\u{0085}",
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Self::LS => "\u{2028}",
|
|
|
|
Self::LS => "\u{2028}",
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
Self::PS => "\u{2029}",
|
|
|
|
Self::PS => "\u{2029}",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -45,11 +57,17 @@ impl LineEnding {
|
|
|
|
pub const fn from_char(ch: char) -> Option<LineEnding> {
|
|
|
|
pub const fn from_char(ch: char) -> Option<LineEnding> {
|
|
|
|
match ch {
|
|
|
|
match ch {
|
|
|
|
'\u{000A}' => Some(LineEnding::LF),
|
|
|
|
'\u{000A}' => Some(LineEnding::LF),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
'\u{000B}' => Some(LineEnding::VT),
|
|
|
|
'\u{000B}' => Some(LineEnding::VT),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
'\u{000C}' => Some(LineEnding::FF),
|
|
|
|
'\u{000C}' => Some(LineEnding::FF),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
'\u{000D}' => Some(LineEnding::CR),
|
|
|
|
'\u{000D}' => Some(LineEnding::CR),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
'\u{0085}' => Some(LineEnding::Nel),
|
|
|
|
'\u{0085}' => Some(LineEnding::Nel),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
'\u{2028}' => Some(LineEnding::LS),
|
|
|
|
'\u{2028}' => Some(LineEnding::LS),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
'\u{2029}' => Some(LineEnding::PS),
|
|
|
|
'\u{2029}' => Some(LineEnding::PS),
|
|
|
|
// Not a line ending
|
|
|
|
// Not a line ending
|
|
|
|
_ => None,
|
|
|
|
_ => None,
|
|
|
@ -65,11 +83,17 @@ impl LineEnding {
|
|
|
|
match g {
|
|
|
|
match g {
|
|
|
|
"\u{000D}\u{000A}" => Some(LineEnding::Crlf),
|
|
|
|
"\u{000D}\u{000A}" => Some(LineEnding::Crlf),
|
|
|
|
"\u{000A}" => Some(LineEnding::LF),
|
|
|
|
"\u{000A}" => Some(LineEnding::LF),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
"\u{000B}" => Some(LineEnding::VT),
|
|
|
|
"\u{000B}" => Some(LineEnding::VT),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
"\u{000C}" => Some(LineEnding::FF),
|
|
|
|
"\u{000C}" => Some(LineEnding::FF),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
"\u{000D}" => Some(LineEnding::CR),
|
|
|
|
"\u{000D}" => Some(LineEnding::CR),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
"\u{0085}" => Some(LineEnding::Nel),
|
|
|
|
"\u{0085}" => Some(LineEnding::Nel),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
"\u{2028}" => Some(LineEnding::LS),
|
|
|
|
"\u{2028}" => Some(LineEnding::LS),
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
"\u{2029}" => Some(LineEnding::PS),
|
|
|
|
"\u{2029}" => Some(LineEnding::PS),
|
|
|
|
// Not a line ending
|
|
|
|
// Not a line ending
|
|
|
|
_ => None,
|
|
|
|
_ => None,
|
|
|
@ -101,7 +125,9 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
|
|
|
|
// are being matched, as they might be special-use only
|
|
|
|
// are being matched, as they might be special-use only
|
|
|
|
for line in doc.lines().take(100) {
|
|
|
|
for line in doc.lines().take(100) {
|
|
|
|
match get_line_ending(&line) {
|
|
|
|
match get_line_ending(&line) {
|
|
|
|
None | Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
|
|
|
|
None => {}
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
|
|
|
|
Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
|
|
|
|
ending => return ending,
|
|
|
|
ending => return ending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -128,6 +154,19 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
|
|
|
|
LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
|
|
|
|
LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unicode-lines"))]
|
|
|
|
|
|
|
|
/// Returns the passed line's line ending, if any.
|
|
|
|
|
|
|
|
pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
|
|
|
|
|
|
|
|
if line.ends_with("\u{000D}\u{000A}") {
|
|
|
|
|
|
|
|
Some(LineEnding::Crlf)
|
|
|
|
|
|
|
|
} else if line.ends_with('\u{000A}') {
|
|
|
|
|
|
|
|
Some(LineEnding::LF)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
/// Returns the passed line's line ending, if any.
|
|
|
|
/// Returns the passed line's line ending, if any.
|
|
|
|
pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
|
|
|
|
pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
|
|
|
|
if line.ends_with("\u{000D}\u{000A}") {
|
|
|
|
if line.ends_with("\u{000D}\u{000A}") {
|
|
|
@ -211,6 +250,7 @@ mod line_ending_tests {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn str_to_line_ending() {
|
|
|
|
fn str_to_line_ending() {
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
assert_eq!(LineEnding::from_str("\r"), Some(LineEnding::CR));
|
|
|
|
assert_eq!(LineEnding::from_str("\r"), Some(LineEnding::CR));
|
|
|
|
assert_eq!(LineEnding::from_str("\n"), Some(LineEnding::LF));
|
|
|
|
assert_eq!(LineEnding::from_str("\n"), Some(LineEnding::LF));
|
|
|
|
assert_eq!(LineEnding::from_str("\r\n"), Some(LineEnding::Crlf));
|
|
|
|
assert_eq!(LineEnding::from_str("\r\n"), Some(LineEnding::Crlf));
|
|
|
@ -220,6 +260,7 @@ mod line_ending_tests {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn rope_slice_to_line_ending() {
|
|
|
|
fn rope_slice_to_line_ending() {
|
|
|
|
let r = Rope::from_str("hello\r\n");
|
|
|
|
let r = Rope::from_str("hello\r\n");
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
assert_eq!(
|
|
|
|
assert_eq!(
|
|
|
|
LineEnding::from_rope_slice(&r.slice(5..6)),
|
|
|
|
LineEnding::from_rope_slice(&r.slice(5..6)),
|
|
|
|
Some(LineEnding::CR)
|
|
|
|
Some(LineEnding::CR)
|
|
|
@ -238,6 +279,7 @@ mod line_ending_tests {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn get_line_ending_rope_slice() {
|
|
|
|
fn get_line_ending_rope_slice() {
|
|
|
|
let r = Rope::from_str("Hello\rworld\nhow\r\nare you?");
|
|
|
|
let r = Rope::from_str("Hello\rworld\nhow\r\nare you?");
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
assert_eq!(get_line_ending(&r.slice(..6)), Some(LineEnding::CR));
|
|
|
|
assert_eq!(get_line_ending(&r.slice(..6)), Some(LineEnding::CR));
|
|
|
|
assert_eq!(get_line_ending(&r.slice(..12)), Some(LineEnding::LF));
|
|
|
|
assert_eq!(get_line_ending(&r.slice(..12)), Some(LineEnding::LF));
|
|
|
|
assert_eq!(get_line_ending(&r.slice(..17)), Some(LineEnding::Crlf));
|
|
|
|
assert_eq!(get_line_ending(&r.slice(..17)), Some(LineEnding::Crlf));
|
|
|
@ -247,6 +289,7 @@ mod line_ending_tests {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn get_line_ending_str() {
|
|
|
|
fn get_line_ending_str() {
|
|
|
|
let text = "Hello\rworld\nhow\r\nare you?";
|
|
|
|
let text = "Hello\rworld\nhow\r\nare you?";
|
|
|
|
|
|
|
|
#[cfg(feature = "unicode-lines")]
|
|
|
|
assert_eq!(get_line_ending_of_str(&text[..6]), Some(LineEnding::CR));
|
|
|
|
assert_eq!(get_line_ending_of_str(&text[..6]), Some(LineEnding::CR));
|
|
|
|
assert_eq!(get_line_ending_of_str(&text[..12]), Some(LineEnding::LF));
|
|
|
|
assert_eq!(get_line_ending_of_str(&text[..12]), Some(LineEnding::LF));
|
|
|
|
assert_eq!(get_line_ending_of_str(&text[..17]), Some(LineEnding::Crlf));
|
|
|
|
assert_eq!(get_line_ending_of_str(&text[..17]), Some(LineEnding::Crlf));
|
|
|
|