diff/
utils.rs

1use std::cmp::{max, min};
2
3/// Finds the closest-to-starting element present in both slices.
4/// Returns whether an equal element was found in each slice, the index of the element in a, the index in b.
5/// If no match was found between a and b, returns the indeces of the end of each slice, respectively.
6pub fn find_match<T: PartialEq>(a: &[T], b: &[T]) -> (bool, usize, usize) {
7    let (mut x, mut y) = (0, 0);
8    let mut found_match = false;
9    if !a.is_empty() && !b.is_empty() {
10        let max_depth = a.len() + b.len() - 1;
11        for depth in 0..max_depth {
12            let x_lower_bound = max(depth as isize - b.len() as isize + 1, 0) as usize;
13            x = min(depth, a.len() - 1);
14            loop {
15                y = depth - x;
16                if a[x] == b[y] {
17                    found_match = true;
18                    break;
19                }
20                if x > x_lower_bound {
21                    x -= 1;
22                } else {
23                    break;
24                }
25            }
26
27            if found_match {
28                break;
29            }
30        }
31    }
32    if !found_match {
33        x = a.len();
34        y = b.len();
35    }
36    (found_match, x, y)
37}