Expand description
§detect-newline-style
Determine a string’s preferred newline character
§Installation
cargo add detect-newline-style§Examples
use detect_newline_style::LineEnding;
fn main() {
let text = "one\rtwo\r\nthree\nfour\n";
let eol = LineEnding::find(text, LineEnding::Lf);
assert_eq!(eol, LineEnding::Lf);
let text = "one\rtwo\r\nthree\n";
let eol = LineEnding::find_or_use_lf(text);
assert_eq!(eol, LineEnding::Lf);
let text = "one\rtwo\r\nthree\n";
let eol = LineEnding::find_or_use_crlf(text);
assert_eq!(eol, LineEnding::Crlf);
assert_eq!(format!("{}", LineEnding::Cr), "\r");
assert_eq!(format!("{}", LineEnding::Lf), "\n");
assert_eq!(format!("{}", LineEnding::Crlf), "\r\n");
}§Migrations
0.x -> 1.x
Variant names now follow RFC 430
| before | after |
|---|---|
LineEnding::CR | LineEnding::Cr |
LineEnding::LF | LineEnding::Lf |
LineEnding::CRLF | LineEnding::Crlf |
FromStr returns a concrete error type
LineEnding::from_str now fails with ParseLineEndingError instead of Box<dyn Error>. The old type was neither Send nor Sync, so the error could not cross a thread boundary or compose with most application error types.
Code that propagates with ? into a function returning Box<dyn Error> keeps working unchanged. Code that names the error type explicitly needs updating:
// before
let eol: Result<LineEnding, Box<dyn Error>> = LineEnding::from_str("\n");
// after
let eol: Result<LineEnding, ParseLineEndingError> = LineEnding::from_str("\n");from_str also no longer lowercases its input. This has no observable effect, since the only valid inputs are "\r", "\n" and "\r\n".
LineEnding is now Copy
Existing code is unaffected - values that were previously moved are now copied.
The regex dependency is gone
This crate now has no dependencies at all. Behaviour is unchanged; see the tests covering "\r\r\n" and multi-byte input for the edge cases.
Structs§
- Parse
Line Ending Error - Returned by
LineEnding::from_strwhen the input is not one of"\r","\n"or"\r\n"
Enums§
- Line
Ending - A newline style - see
findto detect which style a given string prefers