Skip to content

Commit

Permalink
Fix longest_common_prefix
Browse files Browse the repository at this point in the history
* Correct `longest_common_prefix` with multi-byte character inputs
  • Loading branch information
murarth committed Nov 12, 2018
1 parent 3ca54e5 commit e7e5ada
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub fn longest_common_prefix<'a, I, S>(iter: I) -> Option<&'a str> where
let s = s.as_ref();

let n = pfx.chars().zip(s.chars())
.take_while(|&(a, b)| a == b).count();
.take_while(|&(a, b)| a == b)
.map(|(ch, _)| ch.len_utf8()).sum();

if n == 0 {
return None;
Expand Down Expand Up @@ -381,6 +382,9 @@ mod test {
Some("foo"));
assert_eq!(longest_common_prefix(["alpha", "alpaca", "alto"].iter()),
Some("al"));

assert_eq!(longest_common_prefix(["äöüx", "äöüy"].iter()),
Some("äöü"));
}

#[test]
Expand Down

0 comments on commit e7e5ada

Please sign in to comment.