Skip to main content

Crate detect_newline

Crate detect_newline 

Source
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, or None if it has none.
dominant
Detect the most frequent line ending in text, or None if it has none.
has_trailing_newline
Whether text ends with a line ending.
lines
Iterate the lines of text, splitting on \n, \r\n, and lone \r.
normalize
Replace every line ending in text with to.
strip_trailing_newline
Strip a single trailing line ending (\r\n, \n, or \r) from text.
to_cr
Normalize every line ending in text to \r.
to_crlf
Normalize every line ending in text to \r\n.
to_lf
Normalize every line ending in text to \n.