[][src]Function lazy_transform_str::transform

pub fn transform(
    str: &str,
    transform_next: impl FnMut(&mut &str) -> TransformedPart
) -> Cow<'_, String, str>

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

With each invocation, transform_next should consume part of the input (by slicing its parameter in place) and return a replacement String if necessary. transform returns once the input is an empty str.

Example

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

let input = r#"a "quoted" word"#;

// Escape double quotes
let output = transform(input, |rest| match rest.unshift().unwrap() {
    c @ '\\' | c @ '"' => {
        let mut changed = String::from(r"\");
        changed.push(c);
        TransformedPart::Changed(changed)
    }
    _ => TransformedPart::Unchanged,
});

assert_eq!(output, Cow::Owned(r#"a \"quoted\" word"#.into()));