[][src]Trait stringedits::Edit

pub trait Edit: AsRef<str> {
    fn splits(&self) -> Splits { ... }
fn transposes(&self) -> Transposes { ... }
fn replaces(&self) -> Replaces { ... }
fn inserts(&self) -> Inserts { ... }
fn deletions(&self) -> Deletions { ... }
fn dist1edits(&self) -> Dist1Edits { ... }
fn dist2edits(&self) -> Dist2Edits { ... } }

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
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().collect();
assert_eq!(want, got);

Important traits for 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");

Important traits for Replaces
fn replaces(&self) -> Replaces

create an iterator over all distance-1 replacements with a lowercase ASCII letter ('a'..='z')

let replaces = "ab".replaces().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 Inserts
fn inserts(&self) -> Inserts

create an iterator over all distance-1 insertions of a lowercase ASCII letter ('b'a..=b'z')

let a_inserts = "a".inserts().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
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
fn dist1edits(&self) -> 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().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
fn dist2edits(&self) -> Dist2Edits

create an iterator over all distance-2 edits of the string; that is, choose two (with replacement) from transposition, replacement with b'a..=b'z', insertion of b'a'..=b'z', and deletion

let got: HashSet<String> = "bar".dist2edits().collect();
assert!(got.contains("b")); // deletion 2
assert!(got.contains("bbra")); // addition and substitution
assert!(got.contains("rba")); // transposition 2
assert!(got.contains("bz")); // substitution and deletion
Loading content...

Implementors

impl<T: AsRef<str>> Edit for T[src]

Important traits for Splits
fn splits(&self) -> Splits[src]

Important traits for Transposes
fn transposes(&self) -> Transposes[src]

Important traits for Replaces
fn replaces(&self) -> Replaces[src]

Important traits for Inserts
fn inserts(&self) -> Inserts[src]

Important traits for Deletions
fn deletions(&self) -> Deletions[src]

Important traits for Dist1Edits
fn dist1edits(&self) -> Dist1Edits[src]

Important traits for Dist2Edits
fn dist2edits(&self) -> Dist2Edits[src]

Loading content...