Expand description
§detect-newline — detect and normalize line endings
Detect which line ending a string uses (\n, \r\n, or \r), normalize a
string to one style, count the styles, iterate lines (splitting on all three),
and trim trailing newlines. Zero dependencies and no_std. A Rust take on
Node’s detect-newline.
use detect_newline::{detect, dominant, to_lf, Newline};
assert_eq!(detect("a\r\nb"), Some(Newline::CrLf)); // first newline found
assert_eq!(dominant("a\nb\nc\r\nd"), Some(Newline::Lf)); // most common
assert_eq!(to_lf("a\r\nb\rc"), "a\nb\nc"); // normalize\r\n is always treated as a single line ending, never as a \r followed by a
\n. detect returns the first ending found; dominant returns the most
frequent (ties broken by which occurs first).
Structs§
- Counts
- How many of each line-ending style a string contains. See
count. - Lines
- Iterator over the lines of a string. Created by
lines.
Enums§
- Newline
- A line-ending style.
Functions§
- count
- Count each line-ending style in
text. - detect
- Detect the first line ending in
text, orNoneif it has none. - dominant
- Detect the most frequent line ending in
text, orNoneif it has none. - has_
trailing_ newline - Whether
textends with a line ending. - lines
- Iterate the lines of
text, splitting on\n,\r\n, and lone\r. - normalize
- Replace every line ending in
textwithto. - strip_
trailing_ newline - Strip a single trailing line ending (
\r\n,\n, or\r) fromtext. - to_cr
- Normalize every line ending in
textto\r. - to_crlf
- Normalize every line ending in
textto\r\n. - to_lf
- Normalize every line ending in
textto\n.