Expand description
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§
- Transformed
Part - Inidicates whether the consumed part of the input remains unchanged or is to be replaced.
Traits§
Functions§
- escape_
double_ quotes - Replaces
\and"instringwith (repectively)\\and\", as lazily as possible. - transform
- Transforms the given
straccording totransform_nextas lazily as possible. - unescape_
backslashed_ verbatim - Replaces
\followed by any Unicodecharinstringwith thatchar, as lazily as possible.
If\\is found, this sequence is consumed at once and a single\remains in the output.