Crate string_view

Crate string_view 

Source
Expand description

§String View

§Use in-place modifications to avoid allocations.
use string_view::StrExt;
 
let text: &mut str = &mut String::from("Hello World");
 
text.chars_in_place()
    .filter(|ch| !ch.char().is_whitespace())
    .for_each(|ch| ch.make_uppercase());
 
assert_eq!(text, "HELLO WORLD");
§Work with views into string slices. Safely extend, reduce without losing parent string size.
let program_text = r#"
fn main() {
    let text = "Hello World";
}
"#;

use string_view::StrExt;

let mut view = program_text.view_part(0, 0);
view.extend_while(|ch| ch == ' ' || ch == '\n');
view.extend_while(char::is_alphabetic);
view.reduce_left_while(|ch| ch == ' ' || ch == '\n');
assert_eq!(view.as_str(), "fn");

view.try_extend(1).unwrap();
view.extend_while(char::is_alphabetic);
view.try_extend(2).unwrap();
assert_eq!(view.as_str(), "fn main()");

view.extend_while(|ch| ch == ' ' || ch == '\n' || ch == '{');
view.shrink_left();
view.extend_while(|_| true);
view.reduce_while(|ch| ch == ' ' || ch == '\n' || ch == '}');
assert_eq!(view.as_str(), r#"let text = "Hello World";"#);

view.reduce_while(|ch| ch == ';');
view.reduce(1);
view.shrink_left();
view.extend_left_while(|ch| ch != '"');
assert_eq!(view.as_str(), "Hello World");

Structs§

BaseStringIsTooShort
The only error case in StringView::try_extend.
Char
In-place character representation inside str slice
CharMut
In-place character representation inside mutable str slice
CharsHaveDifferentSizes
Common error case while working with chars in-place
CharsInPlace
Iterator of chars in-place
CharsInPlaceMut
Mutable iterator of chars in-place
StringView
View into str slice.
ViewIsTooShort
The only error case in StringView::try_reduce.

Traits§

StrExt