#string #replace #multiple #substitute #substring

rreplace

rreplace is a rust library designed to streamline string replacements. It can handle multiple unique replacements and iterates the string only once.

1 unstable release

0.1.5 Aug 28, 2019
0.1.4 Aug 28, 2019

#1307 in Text processing

MIT license

20KB
395 lines

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.

  • First match
  • Longest match

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.

[source,rust]

// 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

[source,rust]

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

[source,rust]

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.

More Replacemets and Failing

Eclipsing

[source,rust]

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

[source,rust]

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.

No runtime deps