MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / count_line_changes

Function count_line_changes

crates/opencode-session/src/revert.rs:302–326  ·  view source on GitHub ↗

Count line-level additions and deletions between two strings. Uses a simple longest-common-subsequence (LCS) diff on lines: lines present only in `new` are additions, lines present only in `old` are deletions.

(old: &str, new: &str)

Source from the content-addressed store, hash-verified

300/// lines present only in `new` are additions, lines present only in `old`
301/// are deletions.
302fn count_line_changes(old: &str, new: &str) -> (u64, u64) {
303 if old == new {
304 return (0, 0);
305 }
306
307 let old_lines: Vec<&str> = old.lines().collect();
308 let new_lines: Vec<&str> = new.lines().collect();
309
310 // Fast path: entirely new content (old was empty)
311 if old_lines.is_empty() {
312 return (new_lines.len() as u64, 0);
313 }
314 // Fast path: content deleted entirely
315 if new_lines.is_empty() {
316 return (0, old_lines.len() as u64);
317 }
318
319 // Myers-style simple diff via LCS length.
320 // We only need counts, not the actual edit script.
321 let lcs_len = lcs_length(&old_lines, &new_lines);
322 let deletions = old_lines.len() as u64 - lcs_len;
323 let additions = new_lines.len() as u64 - lcs_len;
324
325 (additions, deletions)
326}
327
328/// Compute the length of the longest common subsequence of two line slices.
329/// Uses the classic DP approach with O(min(m,n)) space.

Callers 1

compute_diffMethod · 0.85

Calls 2

lcs_lengthFunction · 0.85
is_emptyMethod · 0.80

Tested by

no test coverage detected