[−][src]Trait stringedits::Edit
The Edit trait provides access to a number of iterators for single-and-double character edits of a string.
It is implemented for any type that implements AsRef<str>
.
Provided methods
ⓘImportant traits for Splits<'a>fn splits(&self) -> Splits
create an iterator over all the splits at a single position of a word.
let want: HashSet<(String, String)> = vec![("", "foo"), ("f", "oo"), ("fo", "o"), ("foo", "")] .into_iter() .map(|(a, b)| (a.to_string(), b.to_string())) .collect(); let got: HashSet<(String, String)> = "foo".splits().map(|(q, r)| (q.to_string(), r.to_string())).collect(); assert_eq!(want, got);
ⓘImportant traits for Transposes<'a>fn transposes(&self) -> Transposes
create an iterator over all distance-1 transpositions of a word
let transpositions = "bar".transposes().collect::<Vec<String>>().join(" "); assert_eq!(transpositions, "abr bra");
ⓘImportant traits for CustomReplaces<'a>fn replaces_ascii_lowercase(&self) -> CustomReplaces
an iterator over all distance-1 replacements with a lowercase ASCII letter ('a'..='z'
)
let replaces = "ab".replaces_ascii_lowercase().collect::<Vec<String>>().join(" "); let want = concat!( "ab bb cb db eb fb gb hb ib jb kb lb mb nb ob pb qb rb sb tb ub vb wb xb yb zb ", // replace 'a' "aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az", // replace 'b' ); assert_eq!(want, replaces)
ⓘImportant traits for CustomInsert<'a>fn inserts_ascii_lowercase(&self) -> CustomInsert
create an iterator over all distance-1 insertions of a lowercase ASCII letter ('b'a..=b'z'
)
let a_inserts = "a".inserts(ASCII_LOWERCASE).collect::<Vec<String>>().join(" "); let want = concat!( "aa ba ca da ea fa ga ha ia ja ka la ma na oa pa qa ra sa ta ua va wa xa ya za ", // insert before a "aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az" // insert after a ); assert_eq!(want, a_inserts);
ⓘImportant traits for Deletions<'a>fn deletions(&self) -> Deletions
create an iterator over all single-character deletions of a word
let deletions = "bar".deletions().collect::<Vec<String>>().join(" "); assert_eq!("ar br ba", deletions);
ⓘImportant traits for Dist1Edits<'a>fn dist1edits<'a>(&'a self, alphabet: &'a [char]) -> Dist1Edits
create an iterator over all distance-1 edits of the string; that is, the transposes, replacements, insertions, and deletions
let got: HashSet<String> = "bar".dist1edits(ASCII_LOWERCASE).collect(); assert!(got.contains("br")); // deletion assert!(got.contains("bbar")); // addition assert!(got.contains("bra")); // transposition assert!(got.contains("baz")); // substitution
ⓘImportant traits for Dist2Edits<'a>fn dist2edits<'a>(&'a self, alphabet: &'a [char]) -> Dist2Edits
an iterator over all distance-2 edits of the string; that is, the combinations of up two transpositions, replacements, insertions, and/or deletions
let dist2: HashSet<String> = "foo".dist2edits(ASCII_LOWERCASE).collect(); assert!(dist2.contains("f")); // two deletions assert!(dist2.contains("afooa")); // two additions assert!(dist2.contains("of")); // delete & transpose assert!(dist2.contains("ofog")); // transposition and addition
ⓘImportant traits for CustomReplaces<'a>fn replaces<'a>(&'a self, alphabet: &'a [char]) -> CustomReplaces<'a>
An iterator over all distance_1 replacements into the string using the provided alphabet. ("bar" -> "baa", "baz")
assert_eq!("foo".replaces(ASCII_LOWERCASE).collect::<String>(), "foo".replaces(ASCII_LOWERCASE).collect::<String>()); assert_eq!("!oo f!o fo!", "foo".replaces(&['!']).collect::<Vec<String>>().join(" "));
ⓘImportant traits for CustomInsert<'a>fn inserts<'a>(&'a self, alphabet: &'a [char]) -> CustomInsert<'a>
An iterator over all distance-1 insertions into the string using the provided alphabet. ("bar" -> "bard", "boar") These insertions include before the first character and after the last one; however, an empty string will only be inserted into once. See ASCII_LOWERCASE and ASCII_UPPERCASE for useful alphabets.
assert_eq!(vec!["!foo", "f!oo", "fo!o", "foo!"], "foo".inserts(&['!']).collect::<Vec<String>>()); assert_eq!("1234", "".inserts(&['1', '2', '3', '4']).collect::<String>());