From ab7501be34032fb9e5c48726a68ae90a917af9eb Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 4 Mar 2026 08:26:56 +0800 Subject: *: Lint --- diff/lines/diff.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'diff/lines/diff.go') diff --git a/diff/lines/diff.go b/diff/lines/diff.go index bdcb4d93..ca34f371 100644 --- a/diff/lines/diff.go +++ b/diff/lines/diff.go @@ -5,7 +5,7 @@ import "bytes" // Diff performs a line-based diff. // Lines are bytes up to and including '\n' (final line may lack '\n'). -func Diff(oldB, newB []byte) ([]Chunk, error) { +func Diff(oldB, newB []byte) ([]Chunk, error) { //nolint:maintidx type lineRef struct { base []byte start int @@ -16,17 +16,22 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { if len(b) == 0 { return nil } + var res []lineRef + start := 0 + for i := range b { if b[i] == '\n' { res = append(res, lineRef{base: b, start: start, end: i + 1}) start = i + 1 } } + if start < len(b) { res = append(res, lineRef{base: b, start: start, end: len(b)}) } + return res } @@ -34,6 +39,7 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { newLines := split(newB) n := len(oldLines) + m := len(newLines) if n == 0 && m == 0 { return nil, nil @@ -42,25 +48,32 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { idOf := make(map[string]int) nextID := 0 oldIDs := make([]int, n) + for i, ln := range oldLines { key := string(ln.base[ln.start:ln.end]) + id, ok := idOf[key] if !ok { id = nextID idOf[key] = id nextID++ } + oldIDs[i] = id } + newIDs := make([]int, m) + for i, ln := range newLines { key := string(ln.base[ln.start:ln.end]) + id, ok := idOf[key] if !ok { id = nextID idOf[key] = id nextID++ } + newIDs[i] = id } @@ -74,11 +87,13 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { } x0 := 0 + y0 := 0 for x0 < n && y0 < m && oldIDs[x0] == newIDs[y0] { x0++ y0++ } + Vprev[offset+0] = x0 trace = append(trace, append([]int(nil), Vprev...)) @@ -97,17 +112,20 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { } else { x = Vprev[offset+(k-1)] + 1 } + y := x - k for x < n && y < m && oldIDs[x] == newIDs[y] { x++ y++ } + V[offset+k] = x if x >= n && y >= m { trace = append(trace, V) found = true + break } } @@ -122,9 +140,11 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { kind ChunkKind lineref lineRef } + revEdits := make([]edit, 0, n+m) x := n + y := m for D := len(trace) - 1; D >= 0; D-- { k := x - y @@ -134,6 +154,7 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { prevX int prevY int ) + if D > 0 { prevV := trace[D-1] if k == -D || (k != D && prevV[offset+(k-1)] < prevV[offset+(k+1)]) { @@ -141,6 +162,7 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { } else { prevK = k - 1 } + prevX = prevV[offset+prevK] prevY = prevX - prevK } @@ -148,6 +170,7 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { for x > prevX && y > prevY { x-- y-- + revEdits = append(revEdits, edit{kind: ChunkKindUnchanged, lineref: oldLines[x]}) } @@ -169,11 +192,13 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { } var out []Chunk + type meta struct { base []byte start int end int } + var metas []meta for _, e := range revEdits { @@ -184,6 +209,7 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { if len(out) == 0 || out[len(out)-1].Kind != e.kind { out = append(out, Chunk{Kind: e.kind, Data: curBase[curStart:curEnd]}) metas = append(metas, meta{base: curBase, start: curStart, end: curEnd}) + continue } @@ -193,6 +219,7 @@ func Diff(oldB, newB []byte) ([]Chunk, error) { if bytes.Equal(lastMeta.base, curBase) && lastMeta.end == curStart { metas[lastIdx].end = curEnd out[lastIdx].Data = curBase[metas[lastIdx].start:metas[lastIdx].end] + continue } -- cgit v1.3.1-10-gc9f91