Skip to content

Commit fc2b385

Browse files
diff: write a one-line add or delete range as a single number
The normal format printed a single added or deleted line as a range, "1a2,2" and "2,2d1", where GNU diff and POSIX write "1a2" and "2d1", and -e printed "2,2d" for "2d". Changed lines already had this (see #10); do the same for added and deleted ones.
1 parent 0019867 commit fc2b385

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎src/ed_diff.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result<Vec<u8>,
126126
match (expected_count, actual_count) {
127127
(0, 0) => unreachable!(),
128128
(0, _) => writeln!(&mut output, "{}a", line_number_expected - 1).unwrap(),
129+
(1, 0) => writeln!(&mut output, "{line_number_expected}d").unwrap(),
129130
(_, 0) => writeln!(
130131
&mut output,
131132
"{},{}d",
@@ -180,6 +181,12 @@ mod tests {
180181
assert_eq!(diff, expected.as_bytes());
181182
}
182183

184+
#[test]
185+
fn test_single_line_deleted() {
186+
let diff = diff(b"a\nb\n", b"a\n", &Params::default()).unwrap();
187+
assert_eq!(diff, b"2d\n");
188+
}
189+
183190
#[test]
184191
fn test_permutations() {
185192
let target = "target/ed-diff/";

‎src/normal_diff.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
133133
let actual_count = result.actual.len();
134134
match (expected_count, actual_count) {
135135
(0, 0) => unreachable!(),
136+
(0, 1) => writeln!(
137+
// exactly one line added
138+
&mut output,
139+
"{}a{line_number_actual}",
140+
line_number_expected - 1
141+
)
142+
.unwrap(),
136143
(0, _) => writeln!(
137144
// 'a' stands for "Add lines"
138145
&mut output,
@@ -142,6 +149,13 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
142149
line_number_actual + actual_count - 1
143150
)
144151
.unwrap(),
152+
(1, 0) => writeln!(
153+
// exactly one line deleted
154+
&mut output,
155+
"{line_number_expected}d{}",
156+
line_number_actual - 1
157+
)
158+
.unwrap(),
145159
(_, 0) => writeln!(
146160
// 'd' stands for "Delete lines"
147161
&mut output,
@@ -228,6 +242,15 @@ mod tests {
228242
assert_eq!(diff, expected);
229243
}
230244

245+
#[test]
246+
fn test_single_line_added_or_deleted() {
247+
// A one-line range is written as a single number, as for c.
248+
let added = diff(b"a\n", b"a\nb\n", &Params::default());
249+
assert_eq!(added, b"1a2\n> b\n".to_vec());
250+
let deleted = diff(b"a\nb\n", b"a\n", &Params::default());
251+
assert_eq!(deleted, b"2d1\n< b\n".to_vec());
252+
}
253+
231254
#[test]
232255
fn test_permutations() {
233256
let target = "target/normal-diff/";

0 commit comments

Comments
 (0)