Crate in_place_string_map[−][src]
Expand description
in_place_string_map
is a library for doing string manipulation in place.
Normally in Rust, if you wanted to handle escapes, for example, you’d need to either map to a
new String, causing allocations, or do .remove
and .insert
calls on a String, which
wouldn’t cause reallocations if you never grow the String, but would cause slowdowns on large
strings due to the need to backshift elements on every item.
Here, you can just do
use in_place_string_map::MapInPlace; fn decode_percent(s: &mut str) -> &mut str { let mut m = MapInPlace::new(s); while let Some(c) = m.pop() { match c { '%' => { let num = m.pop_chars(2).expect("not enough chars"); let n = u8::from_str_radix(num, 16).expect("invalid hex"); m.push(n as char).expect("no more capacity"); } _ => { m.push(c).expect("no more capacity"); } } } m.into_mapped() } let mut input = String::from("%54r%61ns %52igh%74%73%21"); assert_eq!(decode_percent(&mut input), "Trans Rights!");
Safety
This library takes care to ensure that the input string is always left in a valid state.
Since core::mem::forget
is safe, no code can soundly rely on users to call destructors. The
contents of the original borrowed string after any operation is left unspecified generally, but
it is guaranteed to always be valid UTF-8.
Structs
MapInPlace | A mutable reference to a |
NoCapacityError | An error indicating that there was no capacity remaining when a push was attempted. |