Skip to main content

LineEnding

Enum LineEnding 

Source
pub enum LineEnding {
    LF,
    CRLF,
    CR,
}
Expand description

Enum representing the detected line ending style.

Variants§

§

LF

Line Feed (LF) - Common on Unix, Linux, and macOS (\n).

§

CRLF

Carriage Return + Line Feed (CRLF) - Used on Windows (\r\n).

§

CR

Carriage Return (CR) - Used in older Mac OS (pre-OS X) (\r).

Implementations§

Source§

impl LineEnding

Source

pub fn score_mixed_types(s: &str) -> HashMap<LineEnding, usize>

Counts occurrences of each line ending type in the given string.

This function analyzes the input string and returns a LineEndingScores (a HashMap<LineEnding, usize>) containing the number of times each line ending appears.

  • CRLF (\r\n) is counted first to ensure \r inside \r\n is not double-counted.
  • CR (\r) is counted separately, subtracting occurrences of CRLF.
  • LF (\n) is counted separately, also subtracting occurrences of CRLF.
§Example
use line_ending::{LineEnding, LineEndingScores};

let text = "line1\r\nline2\r\nline3\nline4\r";
let scores = LineEnding::score_mixed_types(text);

assert_eq!(scores[&LineEnding::CRLF], 2);
assert_eq!(scores[&LineEnding::LF], 1);
assert_eq!(scores[&LineEnding::CR], 1);
Source

pub fn as_str(&self) -> &'static str

Returns the string representation of the line ending (\n, \r\n, or \r).

§Example
use line_ending::LineEnding;

assert_eq!(LineEnding::LF.as_str(), "\n");
assert_eq!(LineEnding::CRLF.as_str(), "\r\n");
assert_eq!(LineEnding::CR.as_str(), "\r");
Source

pub fn normalize(s: &str) -> String

Converts all line endings in a string to LF (\n) for consistent processing.

§Example
use line_ending::LineEnding;

let mixed = "first\r\nsecond\rthird\n";
assert_eq!(LineEnding::normalize(mixed), "first\nsecond\nthird\n");
Source

pub fn denormalize(&self, s: &str) -> String

Restores line endings in a string to the specified type.

§Example
use line_ending::LineEnding;

let normalized = "first\nsecond\nthird";
assert_eq!(LineEnding::CRLF.denormalize(normalized), "first\r\nsecond\r\nthird");
assert_eq!(LineEnding::CR.denormalize(normalized), "first\rsecond\rthird");
Source

pub fn split(s: &str) -> Vec<String>

Splits a string into a vector of strings using the auto-detected line ending parsed from the string.

§Example
use line_ending::LineEnding;

let text = "line1\r\nline2\r\nline3";
let lines = LineEnding::split(text);
assert_eq!(lines, vec!["line1", "line2", "line3"]);
Source

pub fn split_with(&self, s: &str) -> Vec<String>

Splits a string into lines using the specified line ending.

In most cases, split is the preferred method as it automatically detects the line ending to use.

Unlike LineEnding::split, which detects the line ending type from the input, this method explicitly uses the line ending type of self to split the string.

§Example
use line_ending::LineEnding;

let text = "line1\r\nline2\r\nline3";
let lines = LineEnding::CRLF.split_with(text);
assert_eq!(lines, vec!["line1", "line2", "line3"]);

let text = "line1\nline2\nline3";
let lines = LineEnding::LF.split_with(text);
assert_eq!(lines, vec!["line1", "line2", "line3"]);
Source

pub fn join(&self, lines: Vec<String>) -> String

Joins a vector of strings using the specified line ending.

§Example
use line_ending::LineEnding;

let lines = vec!["line1".to_string(), "line2".to_string(), "line3".to_string()];
assert_eq!(LineEnding::CRLF.join(lines.clone()), "line1\r\nline2\r\nline3");
assert_eq!(LineEnding::LF.join(lines.clone()), "line1\nline2\nline3");
Source

pub fn apply(&self, s: &str) -> String

Applies a specific line ending type to an existing string.

§Example
use line_ending::LineEnding;

let mixed_text = "first line\r\nsecond line\rthird line\n";
assert_eq!(LineEnding::CRLF.apply(mixed_text), "first line\r\nsecond line\r\nthird line\r\n");
assert_eq!(LineEnding::LF.apply(mixed_text), "first line\nsecond line\nthird line\n");

Trait Implementations§

Source§

impl Clone for LineEnding

Source§

fn clone(&self) -> LineEnding

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for LineEnding

Source§

impl Debug for LineEnding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for LineEnding

Source§

impl From<&str> for LineEnding

Source§

fn from(s: &str) -> LineEnding

Detects the predominant line ending style used in the input string.

Note: This assumes that the input string is not of varying types, in which case there is really

§Example
use line_ending::LineEnding;

let sample = "first line\r\nsecond line\r\nthird line";
assert_eq!(LineEnding::from(sample), LineEnding::CRLF);
Source§

impl Hash for LineEnding

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for LineEnding

Source§

fn eq(&self, other: &LineEnding) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for LineEnding

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.