Function read_as_string_with_crlf_suppression

Source
pub fn read_as_string_with_crlf_suppression<R: Read>(
    reader: R,
) -> Result<String>
Expand description

Reads all bytes from the given reader, suppressing CR (\r) bytes that are immediately followed by LF (\n), and returns the resulting data as a UTF-8 string.

This function is useful for normalizing line endings (converting CRLF to LF) and reading textual data from any source that implements std::io::Read.

§Arguments

  • reader - Any type implementing std::io::Read, such as a file, buffer, or stream.

§Returns

Returns an std::io::Result containing the resulting String if all bytes are valid UTF-8, or an error if reading fails or the data is not valid UTF-8.

§Errors

Returns an error if an I/O error occurs while reading, or if the data read is not valid UTF-8.

§Example

use std::io::Cursor;
use tpnote_lib::text_reader::read_as_string_with_crlf_suppression;

let input = b"hello\r\nworld";
let cursor = Cursor::new(input);
let output = read_as_string_with_crlf_suppression(cursor).unwrap();
assert_eq!(output, "hello\nworld");

§See Also

read_with_crlf_suppression