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
impl LineEnding
Sourcepub fn score_mixed_types(s: &str) -> HashMap<LineEnding, usize>
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 ofCRLF
.LF (\n)
is counted separately, also subtracting occurrences ofCRLF
.
§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);
Sourcepub fn as_str(&self) -> &'static str
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");
Sourcepub fn normalize(s: &str) -> String
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");
Sourcepub fn denormalize(&self, s: &str) -> String
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");
Sourcepub fn split(s: &str) -> Vec<String>
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"]);
Sourcepub fn split_with(&self, s: &str) -> Vec<String>
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"]);
Sourcepub fn join(&self, lines: Vec<String>) -> String
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");
Sourcepub fn apply(&self, s: &str) -> String
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
impl Clone for LineEnding
Source§fn clone(&self) -> LineEnding
fn clone(&self) -> LineEnding
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for LineEnding
impl Debug for LineEnding
Source§impl From<&str> for LineEnding
impl From<&str> for LineEnding
Source§fn from(s: &str) -> LineEnding
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);