MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / LineCounts

Function LineCounts

internal/diff/diff.go:170–183  ·  view source on GitHub ↗

LineCounts returns how many lines a line-level diff of oldText → newText adds and removes (PLAN-2348 U2). It counts the way the web DiffView renders jsdiff's diffLines: each changed run is split on newlines after dropping one trailing newline, so a run "a\nb\n" is two lines. Both sides are Myers lin

(oldText, newText string)

Source from the content-addressed store, hash-verified

168// equally valid split on unusual inputs, in which case a count can differ from
169// the rendered diff by the lines those splits trade.
170func LineCounts(oldText, newText string) (added, removed int) {
171 a, b, lines := dmp.DiffLinesToChars(oldText, newText)
172 diffs := dmp.DiffCharsToLines(dmp.DiffMain(a, b, false), lines)
173 for _, d := range diffs {
174 n := strings.Count(strings.TrimSuffix(d.Text, "\n"), "\n") + 1
175 switch d.Type {
176 case difflib.DiffInsert:
177 added += n
178 case difflib.DiffDelete:
179 removed += n
180 }
181 }
182 return added, removed
183}

Calls 1

CountMethod · 0.45