= rreplace
rreplace is a rust library designed to streamline string replacements. It can handle multiple unique replacements and iterates the string only once. Multiple unique repacements may eclipse one another, therefore rreplace follows this replacement priority.
== rreplace in action
run(&str, Hashmap<&str,&str>) -> String:: run
takes a string argument to search and a Hashmap of replacements.
The Key of the hashmap is the sequence to match, the Value is the sequence to replace.
// 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"
== Complex examples The priority with which rreplace updates strings is shown in several examples below.
=== Replace First
replace.insert("This string", "xxx"); replace.insert("string", "yyy");
rreplace::run("This string is foo", r);
"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);
Both seqences begin matching on the same index, therefore the longer replacement takes priority.
=== More Replacemets and Failing
==== Eclipsing
replace.insert("string is foo", "yyy"); replace.insert("i", "I");
rreplace::run("This string is foo", replace);
"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);
"string is X"
is unable to find a complete match and therefore "i"
matches with every i
within the string.