Skip to content

Commit

Permalink
fix(diff): Safe numeric type conversions
Browse files Browse the repository at this point in the history
Implements safe type conversions to address clippy lints.
  • Loading branch information
afnanenayet committed Jan 7, 2023
1 parent 0e367da commit 0db3f61
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ where
pub new: T,
}

/// Convert a range with into another numeric type.
///
/// This will panic if the values cannot be converted to the target type. This is better than using
/// `as` because it will explicitly panic instead of silently wrapping around.
fn convert_range<FromType, IntoType>(range: Range<FromType>) -> Range<IntoType>
where
FromType: TryInto<IntoType>,
<FromType as TryInto<IntoType>>::Error: std::fmt::Debug,
{
range.start.try_into().unwrap()..range.end.try_into().unwrap()
}

/// Find the length of the common suffix between the ranges specified for `a` and `b`.
/// The ranges are assumed to be [inclusive, exclusive).
fn common_suffix_len<T: PartialEq>(
Expand All @@ -54,17 +66,19 @@ fn common_suffix_len<T: PartialEq>(
b: &[T],
b_range: Range<usize>,
) -> usize {
let mut l = 1;

let mut l: isize = 1;
let a_range: Range<isize> = convert_range(a_range);
let b_range: Range<isize> = convert_range(b_range);
unsafe {
while (a_range.end as isize) - (l as isize) >= a_range.start as isize
&& (b_range.end as isize) - (l as isize) >= b_range.start as isize
&& a.get_unchecked(a_range.end - l) == b.get_unchecked(b_range.end - l)
while a_range.end - l >= a_range.start
&& b_range.end - l >= b_range.start
&& a.get_unchecked::<usize>((a_range.end - l).try_into().unwrap())
== b.get_unchecked::<usize>((b_range.end - l).try_into().unwrap())
{
l += 1;
}
}
l - 1
(l - 1).try_into().unwrap()
}

/// The edit information representing a line
Expand Down

0 comments on commit 0db3f61

Please # to comment.