rreplace

Crates.iorreplace
lib.rsrreplace
version0.1.5
sourcesrc
created_at2019-08-27 22:43:43.898712
updated_at2019-08-28 18:53:52.019445
descriptionString replacement made easy! It can handle multiple unique replacements in one pass.
homepage
repositoryhttps://github.com/bcopp/rreplace
max_upload_size
id160238
size19,081
Brendan Copp (bcopp)

documentation

README

rreplace

rreplace is a rust library designed to streamline string replacements. It can handle multiple unique replacements and iterates the string only once. Repacements may eclipse one another, therefore rreplace follows this priority.

  1. First match
  2. Longest match

In Action

// Create HashMap to define replacements
let replace: Hashmap<&str, &str> = Hashmap::new();
replace.insert("This", "xxxx");
replace.insert("foo", "foobar");

rreplace::run("This string is foo", r);
// Returns:   "xxxx string is foobar"

Examples

Replace First

replace.insert("This string", "xxx");
replace.insert("string", "yyy");

rreplace::run("This string is foo", r);
// Returns:   "xxx is foo"

"This string" begins matching before "string" and therefore takes replacement priority.

Replace Longest

replace.insert("This string", "yyy");
replace.insert("This", "xxx");

rreplace::run("This string is foo", replace);
// Returns:   "xxx is foo"

Both seqences begin matching on the same index, therefore the longer replacement takes priority.

Eclipsing

replace.insert("string is foo", "yyy");
replace.insert("i", "I");

rreplace::run("This string is foo", replace);
// Returns:   "ThIs xxx"

"i" cannot be replaced within "string is foo". This is because "string is foo" begins matching earlier than the individual "i" within.

Failing

replace.insert("string is X", "yyy");
replace.insert("i", "I");

rreplace::run("This string is foo", replace);
// Returns:   "ThIs strIng Is foo"

"string is X" is unable to find a complete match and therefore "i" matches with every i within the string.

I new to Rust and would love feedback on my code! Send me an email or contact me through my repository page.

Commit count: 7

cargo fmt