Auto merge of #9954 - gilescope:lev_tighten, r=ehuss

nit: Allocated slightly bigger vec than needed

I think this is slightly more correct as the rest of the algorithm is focusing on chars not bytes, so this is more consistent.
This commit is contained in:
bors 2021-10-02 15:52:22 +00:00
commit a821e2cb24

View File

@ -1,14 +1,15 @@
use std::cmp; use std::cmp;
pub fn lev_distance(me: &str, t: &str) -> usize { pub fn lev_distance(me: &str, t: &str) -> usize {
let t_len = t.chars().count();
if me.is_empty() { if me.is_empty() {
return t.chars().count(); return t_len;
} }
if t.is_empty() { if t.is_empty() {
return me.chars().count(); return me.chars().count();
} }
let mut dcol = (0..=t.len()).collect::<Vec<_>>(); let mut dcol = (0..=t_len).collect::<Vec<_>>();
let mut t_last = 0; let mut t_last = 0;
for (i, sc) in me.chars().enumerate() { for (i, sc) in me.chars().enumerate() {