#str #repeat #cache #pattern #rep

rep-str

A crate for caching a repeat pattern of a str

1 unstable release

0.1.0 Jun 10, 2022

#354 in Caching

GPL-3.0 license

14KB

This is a crate for caching a repeat pattern of a str with only one allocation by generate a new RepStr struct this crate provides.

Example 1: Crate RepStr directly.

use rep_str::RepStr;
let repstr = rep_str::RepStr::new("#", 50); // generate a RepStr object with max repeat time 50
assert!("##########" == repstr.repeat_unwrap(10));
assert!("####################" == repstr.repeat_unwrap(20));
// no extra allocation would occurs:
assert!(repstr.repeat_unwrap(20).as_ptr() == repstr.repeat(12).unwrap().as_ptr())
// repstr.repeat_unwrap(51) // panic!

Example 2: Crate RepStr by IntoRepStr trait

use rep_str::IntoRepStr;
let repstr = "🦀".repeat_cache(20);
assert!(Some("🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀") == repstr.repeat(20));
assert!(None == repstr.repeat(21));

No runtime deps