levenshtein-1.0.5/.cargo_vcs_info.json0000644000000001120000000000000133540ustar { "git": { "sha1": "1b1132a97498e9f1bb34a7f461e042fc5cd50603" } } levenshtein-1.0.5/Cargo.lock0000644000000002160000000000000113340ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "levenshtein" version = "1.0.5" levenshtein-1.0.5/Cargo.toml0000644000000016230000000000000113620ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "levenshtein" version = "1.0.5" authors = ["Titus Wormer "] include = ["src/lib.rs", "Cargo.toml"] description = "Levenshtein algorithm" readme = "readme.md" keywords = ["Vladimir", "Levenshtein", "algorithm", "edit", "distance"] license = "MIT" repository = "https://github.com/wooorm/levenshtein-rs" [lib] name = "levenshtein" path = "src/lib.rs" levenshtein-1.0.5/Cargo.toml.orig000064400000000000000000000006100000000000000150140ustar 00000000000000[package] name = "levenshtein" version = "1.0.5" authors = ["Titus Wormer "] description = "Levenshtein algorithm" repository = "https://github.com/wooorm/levenshtein-rs" readme = "readme.md" keywords = ["Vladimir", "Levenshtein", "algorithm", "edit", "distance"] license = "MIT" include = ["src/lib.rs", "Cargo.toml"] [lib] name = "levenshtein" path = "src/lib.rs" levenshtein-1.0.5/src/lib.rs000064400000000000000000000027500000000000000140370ustar 00000000000000/** * `levenshtein-rs` - levenshtein * * MIT licensed. * * Copyright (c) 2016 Titus Wormer */ #[must_use] pub fn levenshtein(a: &str, b: &str) -> usize { let mut result = 0; /* Shortcut optimizations / degenerate cases. */ if a == b { return result; } let length_a = a.chars().count(); let length_b = b.chars().count(); if length_a == 0 { return length_b; } if length_b == 0 { return length_a; } /* Initialize the vector. * * This is why it’s fast, normally a matrix is used, * here we use a single vector. */ let mut cache: Vec = (1..).take(length_a).collect(); let mut distance_a; let mut distance_b; /* Loop. */ for (index_b, code_b) in b.chars().enumerate() { result = index_b; distance_a = index_b; for (index_a, code_a) in a.chars().enumerate() { distance_b = if code_a == code_b { distance_a } else { distance_a + 1 }; distance_a = cache[index_a]; result = if distance_a > result { if distance_b > result { result + 1 } else { distance_b } } else if distance_b > distance_a { distance_a + 1 } else { distance_b }; cache[index_a] = result; } } result }