-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.rs
More file actions
32 lines (26 loc) · 956 Bytes
/
Copy pathdiff.rs
File metadata and controls
32 lines (26 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
pub fn side_by_side_diff(old: &str, new: &str) -> String {
let old_lines: Vec<&str> = old.lines().collect();
let new_lines: Vec<&str> = new.lines().collect();
let mut result = String::new();
// Simple line-by-line diff
let max_len = old_lines.len().max(new_lines.len());
for i in 0..max_len {
let old_line = old_lines.get(i).map_or("", |v| v);
let new_line = new_lines.get(i).map_or("", |v| v);
if old_line != new_line {
if !old_line.is_empty() {
result.push_str(&format!("- {}\n", old_line));
}
if !new_line.is_empty() {
result.push_str(&format!("+ {}\n", new_line));
}
} else if !old_line.is_empty() {
result.push_str(&format!(" {}\n", old_line));
}
}
if result.is_empty() {
"No differences found.".to_string()
} else {
format!("Differences:\n{}", result)
}
}