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,37 +59,12 @@ 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 {
old: old_start..old_end,
new: new_start..new_end,
});
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 new_end = new_start + new_edit.new.len() as u32;
composed.push(Edit {
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 { if old_edit.new.start < 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;
@ -117,8 +102,8 @@ impl Patch {
} }
if old_edit.new.end > new_edit.old.end { if old_edit.new.end > new_edit.old.end {
let old_end = old_start let old_end =
+ cmp::min(old_edit.old.len() as u32, new_edit.old.len() as u32); 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; let new_end = new_start + new_edit.new.len() as u32;
composed.push(Edit { composed.push(Edit {
old: old_start..old_end, old: old_start..old_end,
@ -132,8 +117,8 @@ impl Patch {
new_edits_iter.next(); new_edits_iter.next();
} else { } else {
let old_end = old_start + old_edit.old.len() as u32; let old_end = old_start + old_edit.old.len() as u32;
let new_end = new_start let new_end =
+ cmp::min(old_edit.new.len() as u32, new_edit.new.len() as u32); new_start + cmp::min(old_edit.new.len() as u32, new_edit.new.len() as u32);
composed.push(Edit { composed.push(Edit {
old: old_start..old_end, old: old_start..old_end,
new: new_start..new_end, new: new_start..new_end,
@ -145,8 +130,8 @@ impl Patch {
new_start = new_end; new_start = new_end;
old_edits_iter.next(); old_edits_iter.next();
} }
} } else {
} break;
} }
} }