str-utils 0.3.7

This crate provides some traits to extend `[u8]`, `str` and `Cow<str>`.
Documentation
use alloc::borrow::Cow;

/// To extend `str` and `Cow<str>` to have `normalize_newlines` method.
///
/// This converts Unicode newline sequences to LF (`\n`) while avoiding allocation when the text is already normalized.
pub trait NormalizeNewlines<'a> {
    /// Returns a `Cow<str>` where newline sequences are normalized to LF (`\n`).
    ///
    /// Handles LF, VT, FF, CRLF, CR, NEL, LS, and PS. CRLF is treated as one newline sequence.
    fn normalize_newlines(self) -> Cow<'a, str>;
}

impl<'a> NormalizeNewlines<'a> for &'a str {
    #[inline]
    fn normalize_newlines(self) -> Cow<'a, str> {
        crate::newlines::replace_newlines_with(self, b'\n')
    }
}

impl<'a> NormalizeNewlines<'a> for Cow<'a, str> {
    #[inline]
    fn normalize_newlines(self) -> Cow<'a, str> {
        match self {
            Cow::Borrowed(s) => s.normalize_newlines(),
            Cow::Owned(s) => {
                Cow::Owned(crate::cow_into_owned!(s, s.as_str().normalize_newlines(),))
            },
        }
    }
}