pub trait Edit: AsRef<str> {
// Provided methods
fn splits(&self) -> Splits<'_> ⓘ { ... }
fn transposes(&self) -> Transposes<'_> ⓘ { ... }
fn replaces_ascii_lowercase(&self) -> CustomReplaces<'_> ⓘ { ... }
fn inserts_ascii_lowercase(&self) -> CustomInsert<'_> ⓘ { ... }
fn deletions(&self) -> Deletions<'_> ⓘ { ... }
fn dist1edits<'a>(&'a self, alphabet: &'a [char]) -> Dist1Edits<'_> ⓘ { ... }
fn dist2edits<'a>(&'a self, alphabet: &'a [char]) -> Dist2Edits<'_> ⓘ { ... }
fn replaces<'a>(&'a self, alphabet: &'a [char]) -> CustomReplaces<'a> ⓘ { ... }
fn inserts<'a>(&'a self, alphabet: &'a [char]) -> CustomInsert<'a> ⓘ { ... }
}
Expand description
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§
Sourcefn splits(&self) -> Splits<'_> ⓘ
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);
Sourcefn transposes(&self) -> Transposes<'_> ⓘ
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");
Sourcefn replaces_ascii_lowercase(&self) -> CustomReplaces<'_> ⓘ
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)
Sourcefn inserts_ascii_lowercase(&self) -> CustomInsert<'_> ⓘ
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);
Sourcefn deletions(&self) -> Deletions<'_> ⓘ
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);
Sourcefn dist1edits<'a>(&'a self, alphabet: &'a [char]) -> Dist1Edits<'_> ⓘ
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
Sourcefn dist2edits<'a>(&'a self, alphabet: &'a [char]) -> Dist2Edits<'_> ⓘ
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
Sourcefn replaces<'a>(&'a self, alphabet: &'a [char]) -> 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(" "));
Sourcefn inserts<'a>(&'a self, alphabet: &'a [char]) -> 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>());