Remove redundant code path in Patch::compose

This commit is contained in:
Antonio Scandurra 2021-11-12 17:24:25 +01:00
parent 3f11b8af56
commit d721c2ba4b

View file

@ -18,9 +18,13 @@ impl Patch {
let mut old_start = 0; let mut old_start = 0;
let mut new_start = 0; let mut new_start = 0;
loop { loop {
match (old_edits_iter.peek_mut(), new_edits_iter.peek_mut()) { let old_edit = old_edits_iter.peek_mut();
(None, None) => break, let new_edit = new_edits_iter.peek_mut();
(Some(old_edit), None) => {
// Push the old edit if its new end is before the new edit's old start.
if let Some(old_edit) = old_edit.as_ref() {
let new_edit = new_edit.as_ref();
if new_edit.map_or(true, |new_edit| old_edit.new.end < new_edit.old.start) {
let catchup = old_edit.old.start - old_start; let catchup = old_edit.old.start - old_start;
old_start += catchup; old_start += catchup;
new_start += catchup; new_start += catchup;
@ -34,8 +38,14 @@ impl Patch {
old_start = old_end; old_start = old_end;
new_start = new_end; new_start = new_end;
old_edits_iter.next(); old_edits_iter.next();
continue;
} }
(None, Some(new_edit)) => { }
// Push the new edit if its old end is before the old edit's new start.
if let Some(new_edit) = new_edit.as_ref() {
let old_edit = old_edit.as_ref();
if old_edit.map_or(true, |old_edit| new_edit.old.end < old_edit.new.start) {
let catchup = new_edit.new.start - new_start; let catchup = new_edit.new.start - new_start;
old_start += catchup; old_start += catchup;
new_start += catchup; new_start += catchup;
@ -49,104 +59,79 @@ impl Patch {
old_start = old_end; old_start = old_end;
new_start = new_end; new_start = new_end;
new_edits_iter.next(); new_edits_iter.next();
continue;
} }
(Some(old_edit), Some(new_edit)) => { }
if old_edit.new.end < new_edit.old.start {
let catchup = old_edit.old.start - old_start;
old_start += catchup;
new_start += catchup;
let old_end = old_start + old_edit.old.len() as u32; // If we still have edits by this point then they must intersect, so we compose them.
let new_end = new_start + old_edit.new.len() as u32; if let Some((old_edit, new_edit)) = old_edit.zip(new_edit) {
composed.push(Edit { if old_edit.new.start < new_edit.old.start {
old: old_start..old_end, let catchup = old_edit.old.start - old_start;
new: new_start..new_end, old_start += catchup;
}); new_start += catchup;
old_start = old_end;
new_start = new_end;
old_edits_iter.next();
} else if new_edit.old.end < old_edit.new.start {
let catchup = new_edit.new.start - new_start;
old_start += catchup;
new_start += catchup;
let old_end = old_start + new_edit.old.len() as u32; let overshoot = new_edit.old.start - old_edit.new.start;
let new_end = new_start + new_edit.new.len() as u32; let old_end = cmp::min(old_start + overshoot, old_edit.old.end);
composed.push(Edit { let new_end = new_start + overshoot;
old: old_start..old_end, composed.push(Edit {
new: new_start..new_end, old: old_start..old_end,
}); new: new_start..new_end,
old_start = old_end; });
new_start = new_end;
new_edits_iter.next();
} else {
if old_edit.new.start < new_edit.old.start {
let catchup = old_edit.old.start - old_start;
old_start += catchup;
new_start += catchup;
let overshoot = new_edit.old.start - old_edit.new.start; old_edit.old.start += overshoot;
let old_end = cmp::min(old_start + overshoot, old_edit.old.end); old_edit.new.start += overshoot;
let new_end = new_start + overshoot; old_start = old_end;
composed.push(Edit { new_start = new_end;
old: old_start..old_end, } else {
new: new_start..new_end, let catchup = new_edit.new.start - new_start;
}); old_start += catchup;
new_start += catchup;
old_edit.old.start += overshoot; let overshoot = old_edit.new.start - new_edit.old.start;
old_edit.new.start += overshoot; let old_end = old_start + overshoot;
old_start = old_end; let new_end = cmp::min(new_start + overshoot, new_edit.new.end);
new_start = new_end; composed.push(Edit {
} else { old: old_start..old_end,
let catchup = new_edit.new.start - new_start; new: new_start..new_end,
old_start += catchup; });
new_start += catchup;
let overshoot = old_edit.new.start - new_edit.old.start; new_edit.old.start += overshoot;
let old_end = old_start + overshoot; new_edit.new.start += overshoot;
let new_end = cmp::min(new_start + overshoot, new_edit.new.end); old_start = old_end;
composed.push(Edit { new_start = new_end;
old: old_start..old_end,
new: new_start..new_end,
});
new_edit.old.start += overshoot;
new_edit.new.start += overshoot;
old_start = old_end;
new_start = new_end;
}
if old_edit.new.end > new_edit.old.end {
let old_end = old_start
+ cmp::min(old_edit.old.len() as u32, new_edit.old.len() as u32);
let new_end = new_start + new_edit.new.len() as u32;
composed.push(Edit {
old: old_start..old_end,
new: new_start..new_end,
});
old_edit.old.start = old_end;
old_edit.new.start = new_edit.old.end;
old_start = old_end;
new_start = new_end;
new_edits_iter.next();
} else {
let old_end = old_start + old_edit.old.len() as u32;
let new_end = new_start
+ cmp::min(old_edit.new.len() as u32, new_edit.new.len() as u32);
composed.push(Edit {
old: old_start..old_end,
new: new_start..new_end,
});
new_edit.old.start = old_edit.new.end;
new_edit.new.start = new_end;
old_start = old_end;
new_start = new_end;
old_edits_iter.next();
}
}
} }
if old_edit.new.end > new_edit.old.end {
let old_end =
old_start + cmp::min(old_edit.old.len() as u32, new_edit.old.len() as u32);
let new_end = new_start + new_edit.new.len() as u32;
composed.push(Edit {
old: old_start..old_end,
new: new_start..new_end,
});
old_edit.old.start = old_end;
old_edit.new.start = new_edit.old.end;
old_start = old_end;
new_start = new_end;
new_edits_iter.next();
} else {
let old_end = old_start + old_edit.old.len() as u32;
let new_end =
new_start + cmp::min(old_edit.new.len() as u32, new_edit.new.len() as u32);
composed.push(Edit {
old: old_start..old_end,
new: new_start..new_end,
});
new_edit.old.start = old_edit.new.end;
new_edit.new.start = new_end;
old_start = old_end;
new_start = new_end;
old_edits_iter.next();
}
} else {
break;
} }
} }