Skip to content

Commit

Permalink
Normal diff: compact ranges of lines where possible (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
oSoMoN committed Feb 7, 2024
1 parent a660f74 commit 32530af
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/normal_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ pub fn diff(expected: &[u8], actual: &[u8]) -> Vec<u8> {
line_number_actual - 1
)
.unwrap(),
(1, 1) => writeln!(
&mut output,
"{}c{}",
line_number_expected,
line_number_actual
)
.unwrap(),
(1, _) => writeln!(
&mut output,
"{}c{},{}",
line_number_expected,
line_number_actual,
actual_count + line_number_actual - 1
)
.unwrap(),
(_, 1) => writeln!(
&mut output,
"{},{}c{}",
line_number_expected,
expected_count + line_number_expected - 1,
line_number_actual
)
.unwrap(),
_ => writeln!(
&mut output,
"{},{}c{},{}",
Expand Down Expand Up @@ -173,6 +196,18 @@ pub fn diff(expected: &[u8], actual: &[u8]) -> Vec<u8> {
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_basic() {
let mut a = Vec::new();
a.write_all(b"a\n").unwrap();
let mut b = Vec::new();
b.write_all(b"b\n").unwrap();
let diff = diff(&a, &b);
let expected = b"1c1\n< a\n---\n> b\n".to_vec();
assert_eq!(diff, expected);
}

#[test]
fn test_permutations() {
let target = "target/normal-diff/";
Expand Down

0 comments on commit 32530af

Please sign in to comment.