[][src]Crate lazy_transform_str

Lazy-copying lazy-allocated scanning str transformations.
This is good e.g. for (un)escaping text, especially if individual strings are short.

Note that this library uses smartstring (and as such returns [Woc]s instead of Cows).
The output is still [Deref<Target = str>] regardless, so there should be no issue with ease of use.

Example

use {
    cervine::Cow,
    gnaw::Unshift as _,
    lazy_transform_str::{Transform as _, TransformedPart},
    smartstring::alias::String,
};

fn double_a(str: &str) -> Cow<String, str> {
    str.transform(|rest /*: &mut &str */| {
        // Consume some of the input. `rest` is never empty here.
        match rest.unshift().unwrap() {
            'a' => TransformedPart::Changed(String::from("aa")),
            _ => TransformedPart::Unchanged,
        }
    } /*: impl FnMut(…) -> … */ )
}

assert_eq!(double_a("abc"), Cow::Owned(String::from("aabc")));
assert_eq!(double_a("bcd"), Cow::Borrowed("bcd"));

See escape_double_quotes and [unescape_backlashed_verbatim]'s sources for more real-world examples.

Enums

TransformedPart

Inidicates whether the consumed part of the input remains unchanged or is to be replaced.

Traits

Transform

Helper trait to call transform as method on &str.

Functions

escape_double_quotes

Replaces \ and " in string with (repectively) \\ and \", as lazily as possible.

transform

Transforms the given str according to transform_next as lazily as possible.

unescape_backslashed_verbatim

Replaces \ followed by any Unicode char in string with that char, as lazily as possible.
If \\ is found, this sequence is consumed at once and a single \ remains in the output.