mirror of
https://github.com/encounter/objdiff.git
synced 2025-12-11 06:27:55 +00:00
Remove alternate diff algorithms, only keep Patience
This commit is contained in:
@@ -8,10 +8,7 @@ use anyhow::Result;
|
||||
use similar::{capture_diff_slices_deadline, Algorithm};
|
||||
|
||||
use crate::{
|
||||
diff::{
|
||||
editops::{editops_find, LevEditType},
|
||||
DiffAlg, DiffObjConfig, ProcessCodeResult,
|
||||
},
|
||||
diff::{DiffObjConfig, ProcessCodeResult},
|
||||
obj,
|
||||
obj::{
|
||||
ObjArchitecture, ObjInfo, ObjInsArg, ObjInsArgDiff, ObjInsBranchFrom, ObjInsBranchTo,
|
||||
@@ -101,45 +98,7 @@ pub fn diff_code(
|
||||
|
||||
let mut left_diff = Vec::<ObjInsDiff>::new();
|
||||
let mut right_diff = Vec::<ObjInsDiff>::new();
|
||||
match config.code_alg {
|
||||
DiffAlg::Levenshtein => {
|
||||
diff_instructions_lev(
|
||||
&mut left_diff,
|
||||
&mut right_diff,
|
||||
left_symbol,
|
||||
right_symbol,
|
||||
&left_out,
|
||||
&right_out,
|
||||
)?;
|
||||
}
|
||||
DiffAlg::Lcs => {
|
||||
diff_instructions_similar(
|
||||
Algorithm::Lcs,
|
||||
&mut left_diff,
|
||||
&mut right_diff,
|
||||
&left_out,
|
||||
&right_out,
|
||||
)?;
|
||||
}
|
||||
DiffAlg::Myers => {
|
||||
diff_instructions_similar(
|
||||
Algorithm::Myers,
|
||||
&mut left_diff,
|
||||
&mut right_diff,
|
||||
&left_out,
|
||||
&right_out,
|
||||
)?;
|
||||
}
|
||||
DiffAlg::Patience => {
|
||||
diff_instructions_similar(
|
||||
Algorithm::Patience,
|
||||
&mut left_diff,
|
||||
&mut right_diff,
|
||||
&left_out,
|
||||
&right_out,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
diff_instructions(&mut left_diff, &mut right_diff, &left_out, &right_out)?;
|
||||
|
||||
resolve_branches(&mut left_diff);
|
||||
resolve_branches(&mut right_diff);
|
||||
@@ -168,15 +127,19 @@ pub fn diff_code(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn diff_instructions_similar(
|
||||
alg: Algorithm,
|
||||
fn diff_instructions(
|
||||
left_diff: &mut Vec<ObjInsDiff>,
|
||||
right_diff: &mut Vec<ObjInsDiff>,
|
||||
left_code: &ProcessCodeResult,
|
||||
right_code: &ProcessCodeResult,
|
||||
) -> Result<()> {
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let ops = capture_diff_slices_deadline(alg, &left_code.ops, &right_code.ops, Some(deadline));
|
||||
let ops = capture_diff_slices_deadline(
|
||||
Algorithm::Patience,
|
||||
&left_code.ops,
|
||||
&right_code.ops,
|
||||
Some(deadline),
|
||||
);
|
||||
if ops.is_empty() {
|
||||
left_diff.extend(
|
||||
left_code
|
||||
@@ -217,75 +180,6 @@ fn diff_instructions_similar(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn diff_instructions_lev(
|
||||
left_diff: &mut Vec<ObjInsDiff>,
|
||||
right_diff: &mut Vec<ObjInsDiff>,
|
||||
left_symbol: &ObjSymbol,
|
||||
right_symbol: &ObjSymbol,
|
||||
left_code: &ProcessCodeResult,
|
||||
right_code: &ProcessCodeResult,
|
||||
) -> Result<()> {
|
||||
let edit_ops = editops_find(&left_code.ops, &right_code.ops);
|
||||
|
||||
let mut op_iter = edit_ops.iter();
|
||||
let mut left_iter = left_code.insts.iter();
|
||||
let mut right_iter = right_code.insts.iter();
|
||||
let mut cur_op = op_iter.next();
|
||||
let mut cur_left = left_iter.next();
|
||||
let mut cur_right = right_iter.next();
|
||||
while let Some(op) = cur_op {
|
||||
let left_addr = op.first_start as u32 * 4;
|
||||
let right_addr = op.second_start as u32 * 4;
|
||||
while let (Some(left), Some(right)) = (cur_left, cur_right) {
|
||||
if (left.address - left_symbol.address as u32) < left_addr {
|
||||
left_diff.push(ObjInsDiff { ins: Some(left.clone()), ..ObjInsDiff::default() });
|
||||
right_diff.push(ObjInsDiff { ins: Some(right.clone()), ..ObjInsDiff::default() });
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
cur_left = left_iter.next();
|
||||
cur_right = right_iter.next();
|
||||
}
|
||||
if let (Some(left), Some(right)) = (cur_left, cur_right) {
|
||||
debug_assert_eq!(left.address - left_symbol.address as u32, left_addr);
|
||||
debug_assert_eq!(right.address - right_symbol.address as u32, right_addr);
|
||||
match op.op_type {
|
||||
LevEditType::Replace => {
|
||||
left_diff.push(ObjInsDiff { ins: Some(left.clone()), ..ObjInsDiff::default() });
|
||||
right_diff
|
||||
.push(ObjInsDiff { ins: Some(right.clone()), ..ObjInsDiff::default() });
|
||||
cur_left = left_iter.next();
|
||||
cur_right = right_iter.next();
|
||||
}
|
||||
LevEditType::Insert => {
|
||||
left_diff.push(ObjInsDiff::default());
|
||||
right_diff
|
||||
.push(ObjInsDiff { ins: Some(right.clone()), ..ObjInsDiff::default() });
|
||||
cur_right = right_iter.next();
|
||||
}
|
||||
LevEditType::Delete => {
|
||||
left_diff.push(ObjInsDiff { ins: Some(left.clone()), ..ObjInsDiff::default() });
|
||||
right_diff.push(ObjInsDiff::default());
|
||||
cur_left = left_iter.next();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
cur_op = op_iter.next();
|
||||
}
|
||||
|
||||
// Finalize
|
||||
while cur_left.is_some() || cur_right.is_some() {
|
||||
left_diff.push(ObjInsDiff { ins: cur_left.cloned(), ..ObjInsDiff::default() });
|
||||
right_diff.push(ObjInsDiff { ins: cur_right.cloned(), ..ObjInsDiff::default() });
|
||||
cur_left = left_iter.next();
|
||||
cur_right = right_iter.next();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_branches(vec: &mut [ObjInsDiff]) {
|
||||
let mut branch_idx = 0usize;
|
||||
// Map addresses to indices
|
||||
|
||||
@@ -1,28 +1,12 @@
|
||||
use std::{
|
||||
cmp::{max, min, Ordering},
|
||||
mem::take,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use anyhow::{ensure, Result};
|
||||
use anyhow::Result;
|
||||
use similar::{capture_diff_slices_deadline, Algorithm};
|
||||
|
||||
use crate::{
|
||||
diff::{
|
||||
editops::{editops_find, LevEditType},
|
||||
DiffAlg,
|
||||
},
|
||||
obj::{ObjDataDiff, ObjDataDiffKind, ObjSection, ObjSymbol},
|
||||
};
|
||||
|
||||
pub fn diff_data(alg: DiffAlg, left: &mut ObjSection, right: &mut ObjSection) -> Result<()> {
|
||||
match alg {
|
||||
DiffAlg::Levenshtein => diff_data_lev(left, right),
|
||||
DiffAlg::Lcs => diff_data_similar(Algorithm::Lcs, left, right),
|
||||
DiffAlg::Myers => diff_data_similar(Algorithm::Myers, left, right),
|
||||
DiffAlg::Patience => diff_data_similar(Algorithm::Patience, left, right),
|
||||
}
|
||||
}
|
||||
use crate::obj::{ObjDataDiff, ObjDataDiffKind, ObjSection, ObjSymbol};
|
||||
|
||||
pub fn diff_bss_symbols(
|
||||
left_symbols: &mut [ObjSymbol],
|
||||
@@ -40,67 +24,10 @@ pub fn diff_bss_symbols(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// WIP diff-by-symbol
|
||||
#[allow(dead_code)]
|
||||
pub fn diff_data_symbols(left: &mut ObjSection, right: &mut ObjSection) -> Result<()> {
|
||||
let mut left_ops = Vec::<u32>::with_capacity(left.symbols.len());
|
||||
let mut right_ops = Vec::<u32>::with_capacity(right.symbols.len());
|
||||
for left_symbol in &left.symbols {
|
||||
let data = &left.data
|
||||
[left_symbol.address as usize..(left_symbol.address + left_symbol.size) as usize];
|
||||
let hash = twox_hash::xxh3::hash64(data);
|
||||
left_ops.push(hash as u32);
|
||||
}
|
||||
for symbol in &right.symbols {
|
||||
let data = &right.data[symbol.address as usize..(symbol.address + symbol.size) as usize];
|
||||
let hash = twox_hash::xxh3::hash64(data);
|
||||
right_ops.push(hash as u32);
|
||||
}
|
||||
|
||||
let edit_ops = editops_find(&left_ops, &right_ops);
|
||||
if edit_ops.is_empty() && !left.data.is_empty() {
|
||||
let mut left_iter = left.symbols.iter_mut();
|
||||
let mut right_iter = right.symbols.iter_mut();
|
||||
loop {
|
||||
let (left_symbol, right_symbol) = match (left_iter.next(), right_iter.next()) {
|
||||
(Some(l), Some(r)) => (l, r),
|
||||
(None, None) => break,
|
||||
_ => return Err(anyhow::Error::msg("L/R mismatch in diff_data_symbols")),
|
||||
};
|
||||
let left_data = &left.data
|
||||
[left_symbol.address as usize..(left_symbol.address + left_symbol.size) as usize];
|
||||
let right_data = &right.data[right_symbol.address as usize
|
||||
..(right_symbol.address + right_symbol.size) as usize];
|
||||
|
||||
left.data_diff.push(ObjDataDiff {
|
||||
data: left_data.to_vec(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: left_symbol.size as usize,
|
||||
symbol: left_symbol.name.clone(),
|
||||
});
|
||||
right.data_diff.push(ObjDataDiff {
|
||||
data: right_data.to_vec(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: right_symbol.size as usize,
|
||||
symbol: right_symbol.name.clone(),
|
||||
});
|
||||
left_symbol.diff_symbol = Some(right_symbol.name.clone());
|
||||
left_symbol.match_percent = Some(100.0);
|
||||
right_symbol.diff_symbol = Some(left_symbol.name.clone());
|
||||
right_symbol.match_percent = Some(100.0);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn diff_data_similar(
|
||||
alg: Algorithm,
|
||||
left: &mut ObjSection,
|
||||
right: &mut ObjSection,
|
||||
) -> Result<()> {
|
||||
pub fn diff_data(left: &mut ObjSection, right: &mut ObjSection) -> Result<()> {
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let ops = capture_diff_slices_deadline(alg, &left.data, &right.data, Some(deadline));
|
||||
let ops =
|
||||
capture_diff_slices_deadline(Algorithm::Patience, &left.data, &right.data, Some(deadline));
|
||||
|
||||
let mut left_diff = Vec::<ObjDataDiff>::new();
|
||||
let mut right_diff = Vec::<ObjDataDiff>::new();
|
||||
@@ -175,226 +102,6 @@ pub fn diff_data_similar(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn diff_data_lev(left: &mut ObjSection, right: &mut ObjSection) -> Result<()> {
|
||||
let matrix_size = (left.data.len() as u64).saturating_mul(right.data.len() as u64);
|
||||
ensure!(
|
||||
matrix_size < 1_000_000_000,
|
||||
"Data section {} too large for Levenshtein diff ({} * {} = {})",
|
||||
left.name,
|
||||
left.data.len(),
|
||||
right.data.len(),
|
||||
matrix_size
|
||||
);
|
||||
|
||||
let edit_ops = editops_find(&left.data, &right.data);
|
||||
if edit_ops.is_empty() && !left.data.is_empty() {
|
||||
left.data_diff = vec![ObjDataDiff {
|
||||
data: left.data.clone(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: left.data.len(),
|
||||
symbol: String::new(),
|
||||
}];
|
||||
right.data_diff = vec![ObjDataDiff {
|
||||
data: right.data.clone(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: right.data.len(),
|
||||
symbol: String::new(),
|
||||
}];
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut left_diff = Vec::<ObjDataDiff>::new();
|
||||
let mut right_diff = Vec::<ObjDataDiff>::new();
|
||||
let mut left_cur = 0usize;
|
||||
let mut right_cur = 0usize;
|
||||
let mut cur_op = LevEditType::Replace;
|
||||
let mut cur_left_data = Vec::<u8>::new();
|
||||
let mut cur_right_data = Vec::<u8>::new();
|
||||
for op in edit_ops {
|
||||
if cur_op != op.op_type || left_cur < op.first_start || right_cur < op.second_start {
|
||||
match cur_op {
|
||||
LevEditType::Replace => {
|
||||
let left_data = take(&mut cur_left_data);
|
||||
let right_data = take(&mut cur_right_data);
|
||||
let left_data_len = left_data.len();
|
||||
let right_data_len = right_data.len();
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: left_data,
|
||||
kind: ObjDataDiffKind::Replace,
|
||||
len: left_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: right_data,
|
||||
kind: ObjDataDiffKind::Replace,
|
||||
len: right_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
LevEditType::Insert => {
|
||||
let right_data = take(&mut cur_right_data);
|
||||
let right_data_len = right_data.len();
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: vec![],
|
||||
kind: ObjDataDiffKind::Insert,
|
||||
len: right_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: right_data,
|
||||
kind: ObjDataDiffKind::Insert,
|
||||
len: right_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
LevEditType::Delete => {
|
||||
let left_data = take(&mut cur_left_data);
|
||||
let left_data_len = left_data.len();
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: left_data,
|
||||
kind: ObjDataDiffKind::Delete,
|
||||
len: left_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: vec![],
|
||||
kind: ObjDataDiffKind::Delete,
|
||||
len: left_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if left_cur < op.first_start {
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: left.data[left_cur..op.first_start].to_vec(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: op.first_start - left_cur,
|
||||
symbol: String::new(),
|
||||
});
|
||||
left_cur = op.first_start;
|
||||
}
|
||||
if right_cur < op.second_start {
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: right.data[right_cur..op.second_start].to_vec(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: op.second_start - right_cur,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_cur = op.second_start;
|
||||
}
|
||||
match op.op_type {
|
||||
LevEditType::Replace => {
|
||||
cur_left_data.push(left.data[left_cur]);
|
||||
cur_right_data.push(right.data[right_cur]);
|
||||
left_cur += 1;
|
||||
right_cur += 1;
|
||||
}
|
||||
LevEditType::Insert => {
|
||||
cur_right_data.push(right.data[right_cur]);
|
||||
right_cur += 1;
|
||||
}
|
||||
LevEditType::Delete => {
|
||||
cur_left_data.push(left.data[left_cur]);
|
||||
left_cur += 1;
|
||||
}
|
||||
}
|
||||
cur_op = op.op_type;
|
||||
}
|
||||
// if left_cur < left.data.len() {
|
||||
// let len = left.data.len() - left_cur;
|
||||
// left_diff.push(ObjDataDiff {
|
||||
// data: left.data[left_cur..].to_vec(),
|
||||
// kind: ObjDataDiffKind::Delete,
|
||||
// len,
|
||||
// });
|
||||
// right_diff.push(ObjDataDiff { data: vec![], kind: ObjDataDiffKind::Delete, len });
|
||||
// } else if right_cur < right.data.len() {
|
||||
// let len = right.data.len() - right_cur;
|
||||
// left_diff.push(ObjDataDiff { data: vec![], kind: ObjDataDiffKind::Insert, len });
|
||||
// right_diff.push(ObjDataDiff {
|
||||
// data: right.data[right_cur..].to_vec(),
|
||||
// kind: ObjDataDiffKind::Insert,
|
||||
// len,
|
||||
// });
|
||||
// }
|
||||
|
||||
// TODO: merge with above
|
||||
match cur_op {
|
||||
LevEditType::Replace => {
|
||||
let left_data = take(&mut cur_left_data);
|
||||
let right_data = take(&mut cur_right_data);
|
||||
let left_data_len = left_data.len();
|
||||
let right_data_len = right_data.len();
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: left_data,
|
||||
kind: ObjDataDiffKind::Replace,
|
||||
len: left_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: right_data,
|
||||
kind: ObjDataDiffKind::Replace,
|
||||
len: right_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
LevEditType::Insert => {
|
||||
let right_data = take(&mut cur_right_data);
|
||||
let right_data_len = right_data.len();
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: vec![],
|
||||
kind: ObjDataDiffKind::Insert,
|
||||
len: right_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: right_data,
|
||||
kind: ObjDataDiffKind::Insert,
|
||||
len: right_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
LevEditType::Delete => {
|
||||
let left_data = take(&mut cur_left_data);
|
||||
let left_data_len = left_data.len();
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: left_data,
|
||||
kind: ObjDataDiffKind::Delete,
|
||||
len: left_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: vec![],
|
||||
kind: ObjDataDiffKind::Delete,
|
||||
len: left_data_len,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if left_cur < left.data.len() {
|
||||
left_diff.push(ObjDataDiff {
|
||||
data: left.data[left_cur..].to_vec(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: left.data.len() - left_cur,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
if right_cur < right.data.len() {
|
||||
right_diff.push(ObjDataDiff {
|
||||
data: right.data[right_cur..].to_vec(),
|
||||
kind: ObjDataDiffKind::None,
|
||||
len: right.data.len() - right_cur,
|
||||
symbol: String::new(),
|
||||
});
|
||||
}
|
||||
|
||||
left.data_diff = left_diff;
|
||||
right.data_diff = right_diff;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn no_diff_data(section: &mut ObjSection) {
|
||||
section.data_diff = vec![ObjDataDiff {
|
||||
data: section.data.clone(),
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
/// Adapted from https://crates.io/crates/rapidfuzz
|
||||
// Copyright 2020 maxbachmann
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any
|
||||
// person obtaining a copy of this software and associated
|
||||
// documentation files (the "Software"), to deal in the
|
||||
// Software without restriction, including without
|
||||
// limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software
|
||||
// is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice
|
||||
// shall be included in all copies or substantial portions
|
||||
// of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum LevEditType {
|
||||
Replace,
|
||||
Insert,
|
||||
Delete,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct LevEditOp {
|
||||
pub op_type: LevEditType, /* editing operation type */
|
||||
pub first_start: usize, /* source block position */
|
||||
pub second_start: usize, /* destination position */
|
||||
}
|
||||
|
||||
pub fn editops_find<T>(query: &[T], choice: &[T]) -> Vec<LevEditOp>
|
||||
where T: PartialEq {
|
||||
let Affix { prefix_len, suffix_len } = Affix::find(query, choice);
|
||||
|
||||
let first_string = &query[prefix_len..query.len() - suffix_len];
|
||||
let second_string = &choice[prefix_len..choice.len() - suffix_len];
|
||||
|
||||
let matrix_columns = first_string.len() + 1;
|
||||
let matrix_rows = second_string.len() + 1;
|
||||
|
||||
// TODO maybe use an actual matrix for readability
|
||||
let mut cache_matrix: Vec<usize> = vec![0; matrix_rows * matrix_columns];
|
||||
for (i, elem) in cache_matrix.iter_mut().enumerate().take(matrix_rows) {
|
||||
*elem = i;
|
||||
}
|
||||
for i in 1..matrix_columns {
|
||||
cache_matrix[matrix_rows * i] = i;
|
||||
}
|
||||
|
||||
for (i, char1) in first_string.iter().enumerate() {
|
||||
let mut prev = i * matrix_rows;
|
||||
let current = prev + matrix_rows;
|
||||
let mut x = i + 1;
|
||||
for (p, char2p) in second_string.iter().enumerate() {
|
||||
let mut c3 = cache_matrix[prev] + (char1 != char2p) as usize;
|
||||
prev += 1;
|
||||
x += 1;
|
||||
if x >= c3 {
|
||||
x = c3;
|
||||
}
|
||||
c3 = cache_matrix[prev] + 1;
|
||||
if x > c3 {
|
||||
x = c3;
|
||||
}
|
||||
cache_matrix[current + 1 + p] = x;
|
||||
}
|
||||
}
|
||||
editops_from_cost_matrix(matrix_columns, matrix_rows, prefix_len, cache_matrix)
|
||||
}
|
||||
|
||||
fn editops_from_cost_matrix(
|
||||
len1: usize,
|
||||
len2: usize,
|
||||
prefix_len: usize,
|
||||
cache_matrix: Vec<usize>,
|
||||
) -> Vec<LevEditOp> {
|
||||
let mut ops = Vec::with_capacity(cache_matrix[len1 * len2 - 1]);
|
||||
let mut dir = 0;
|
||||
let mut i = len1 - 1;
|
||||
let mut j = len2 - 1;
|
||||
let mut p = len1 * len2 - 1;
|
||||
|
||||
//TODO this is still pretty ugly
|
||||
while i > 0 || j > 0 {
|
||||
let current_value = cache_matrix[p];
|
||||
|
||||
// More than one operation can be possible at a time. We use `dir` to
|
||||
// decide when ambiguous.
|
||||
let is_insert = j > 0 && current_value == cache_matrix[p - 1] + 1;
|
||||
let is_delete = i > 0 && current_value == cache_matrix[p - len2] + 1;
|
||||
let is_replace = i > 0 && j > 0 && current_value == cache_matrix[p - len2 - 1] + 1;
|
||||
|
||||
let (op_type, new_dir) = match (dir, is_insert, is_delete, is_replace) {
|
||||
(_, false, false, false) => (None, 0),
|
||||
(-1, true, _, _) => (Some(LevEditType::Insert), -1),
|
||||
(1, _, true, _) => (Some(LevEditType::Delete), 1),
|
||||
(_, _, _, true) => (Some(LevEditType::Replace), 0),
|
||||
(0, true, _, _) => (Some(LevEditType::Insert), -1),
|
||||
(0, _, true, _) => (Some(LevEditType::Delete), 1),
|
||||
_ => panic!("something went terribly wrong"),
|
||||
};
|
||||
|
||||
match new_dir {
|
||||
-1 => {
|
||||
j -= 1;
|
||||
p -= 1;
|
||||
}
|
||||
1 => {
|
||||
i -= 1;
|
||||
p -= len2;
|
||||
}
|
||||
0 => {
|
||||
i -= 1;
|
||||
j -= 1;
|
||||
p -= len2 + 1;
|
||||
}
|
||||
_ => panic!("something went terribly wrong"),
|
||||
};
|
||||
dir = new_dir;
|
||||
|
||||
if let Some(op_type) = op_type {
|
||||
ops.insert(0, LevEditOp {
|
||||
op_type,
|
||||
first_start: i + prefix_len,
|
||||
second_start: j + prefix_len,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ops
|
||||
}
|
||||
|
||||
pub struct Affix {
|
||||
pub prefix_len: usize,
|
||||
pub suffix_len: usize,
|
||||
}
|
||||
|
||||
impl Affix {
|
||||
pub fn find<T>(s1: &[T], s2: &[T]) -> Affix
|
||||
where T: PartialEq {
|
||||
let prefix_len = s1.iter().zip(s2.iter()).take_while(|t| t.0 == t.1).count();
|
||||
let suffix_len = s1[prefix_len..]
|
||||
.iter()
|
||||
.rev()
|
||||
.zip(s2[prefix_len..].iter().rev())
|
||||
.take_while(|t| t.0 == t.1)
|
||||
.count();
|
||||
|
||||
Affix { prefix_len, suffix_len }
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
pub mod code;
|
||||
pub mod data;
|
||||
pub mod display;
|
||||
pub mod editops;
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
diff::{
|
||||
@@ -14,19 +12,8 @@ use crate::{
|
||||
obj::{ObjInfo, ObjIns, ObjSectionKind},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub enum DiffAlg {
|
||||
#[default]
|
||||
Patience,
|
||||
Levenshtein,
|
||||
Myers,
|
||||
Lcs,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
||||
pub struct DiffObjConfig {
|
||||
pub code_alg: DiffAlg,
|
||||
pub data_alg: DiffAlg,
|
||||
pub relax_reloc_diffs: bool,
|
||||
}
|
||||
|
||||
@@ -80,7 +67,7 @@ pub fn diff_objs(
|
||||
.and_then(|obj| obj.sections.iter_mut().find(|s| s.name == left_section.name))
|
||||
{
|
||||
if left_section.kind == ObjSectionKind::Data {
|
||||
diff_data(config.data_alg, left_section, right_section)?;
|
||||
diff_data(left_section, right_section)?;
|
||||
} else if left_section.kind == ObjSectionKind::Bss {
|
||||
diff_bss_symbols(&mut left_section.symbols, &mut right_section.symbols)?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user