Skip to content

Instantly share code, notes, and snippets.

@Meyermagic
Last active August 29, 2015 14:02
Show Gist options
  • Save Meyermagic/0399fc1a73c09c671262 to your computer and use it in GitHub Desktop.
Save Meyermagic/0399fc1a73c09c671262 to your computer and use it in GitHub Desktop.
//Counts the number of (non-overlapping) elements matching at the start and end of each slice
fn common_affix<E: TotalEq>(old: &[E], new: &[E]) -> (uint, uint) {
let shorter = min(old.len(), new.len());
let mut common_prefix = 0;
for i in range(0, shorter) {
if old.get(i) != new.get(i) {
break;
}
common_prefix += 1;
}
let mut common_suffix = 0;
for i in range(0, shorter - common_prefix) {
if old.get((old.len() - 1) - i) != new.get((new.len() - 1) - i) {
break;
}
common_suffix += 1;
}
return (common_prefix, common_suffix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment