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.
- First match
- Longest match
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"
The priority with which rreplace updates strings is shown in several examples below.
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.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.
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.
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.