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 `replace_newlines_with_space` method.
///
/// This can be useful when you need to normalize multiline strings into a single line for logging, database storage, or display purposes.
pub trait ReplaceNewlinesWithSpace<'a> {
    /// Returns a `Cow<str>` where all newline sequences are replaced with a space.
    ///
    /// Handles LF, VT, FF, CRLF, CR, NEL, LS, and PS. CRLF is treated as one newline sequence.
    fn replace_newlines_with_space(self) -> Cow<'a, str>;
}

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

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