| 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package diff computes differences between text files or strings. |
| 6 | package diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/ |
| 7 | |
| 8 | import ( |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 11 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | ) |
| 14 | |
| 15 | // An Edit describes the replacement of a portion of a text file. |
| 16 | type Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# struct { |
| 17 | Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start., Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. int // byte offsets of the region to replace |
| 18 | Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. string // the replacement |
| 19 | } |
| 20 | |
| 21 | func (e Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#String().() string { |
| 22 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("{Start:%d,End:%d,New:%q}", e.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start., e.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End., e.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.) |
| 23 | } |
| 24 | |
| 25 | // Apply applies a sequence of edits to the src buffer and returns the |
| 26 | // result. Edits are applied in order of start offset; edits with the |
| 27 | // same start offset are applied in they order they were provided. |
| 28 | // |
| 29 | // Apply returns an error if any edit is out of bounds, |
| 30 | // or if any pair of edits is overlapping. |
| 31 | func Applyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Apply().(src string, edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) (string, error) { |
| 32 | edits, size, err := validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/validate().(src, edits) |
| 33 | if err != nil { |
| 34 | return "", err |
| 35 | } |
| 36 | |
| 37 | // Apply edits. |
| 38 | out := make([]byte, 0, size) |
| 39 | lastEnd := 0 |
| 40 | for _, edit := range edits { |
| 41 | if lastEnd < edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. { |
| 42 | out = append(out, src[lastEnd:edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.]...) |
| 43 | } |
| 44 | out = append(out, edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New....) |
| 45 | lastEnd = edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. |
| 46 | } |
| 47 | out = append(out, src[lastEnd:]...) |
| 48 | |
| 49 | if len(out) != size { |
| 50 | panic("wrong size") |
| 51 | } |
| 52 | |
| 53 | return string(out), nil |
| 54 | } |
| 55 | |
| 56 | // ApplyBytes is like Apply, but it accepts a byte slice. |
| 57 | // The result is always a new array. |
| 58 | func ApplyBytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/ApplyBytes().(src []byte, edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) ([]byte, error) { |
| 59 | res, err := Applyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Apply().(string(src), edits) |
| 60 | return []byte(res), err |
| 61 | } |
| 62 | |
| 63 | // validate checks that edits are consistent with src, |
| 64 | // and returns the size of the patched output. |
| 65 | // It may return a different slice. |
| 66 | func validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/validate().(src string, edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) ([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, int, error) { |
| 67 | if !sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.IsSortedscip-go gomod github.com/golang/go/src go1.25.0 sort/IsSorted().(editsSortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#(edits)) { |
| 68 | edits = slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.25.0 slices/Clone().(edits) |
| 69 | SortEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/SortEdits().(edits) |
| 70 | } |
| 71 | |
| 72 | // Check validity of edits and compute final size. |
| 73 | size := len(src) |
| 74 | lastEnd := 0 |
| 75 | for _, edit := range edits { |
| 76 | if !(0 <= edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. && edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. <= edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. && edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. <= len(src)) { |
| 77 | return nil, 0, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("diff has out-of-bounds edits") |
| 78 | } |
| 79 | if edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. < lastEnd { |
| 80 | return nil, 0, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("diff has overlapping edits") |
| 81 | } |
| 82 | size += len(edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.) + edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. - edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. |
| 83 | lastEnd = edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. |
| 84 | } |
| 85 | |
| 86 | return edits, size, nil |
| 87 | } |
| 88 | |
| 89 | // SortEdits orders a slice of Edits by (start, end) offset. |
| 90 | // This ordering puts insertions (end = start) before deletions |
| 91 | // (end > start) at the same point, but uses a stable sort to preserve |
| 92 | // the order of multiple insertions at the same point. |
| 93 | // (Apply detects multiple deletions at the same point as an error.) |
| 94 | func SortEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/SortEdits().(edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) { |
| 95 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stablescip-go gomod github.com/golang/go/src go1.25.0 sort/Stable().(editsSortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#(edits)) |
| 96 | } |
| 97 | |
| 98 | type editsSortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort# []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# |
| 99 | |
| 100 | func (a editsSortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#) Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#Len().() int { return len(a) } |
| 101 | func (a editsSortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#) Lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#Less().(i, j int) bool { |
| 102 | if cmp := a[i].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. - a[j].Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.; cmp != 0 { |
| 103 | return cmp < 0 |
| 104 | } |
| 105 | return a[i].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. < a[j].Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. |
| 106 | } |
| 107 | func (a editsSortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#) Swapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/editsSort#Swap().(i, j int) { a[i], a[j] = a[j], a[i] } |
| 108 | |
| 109 | // lineEdits expands and merges a sequence of edits so that each |
| 110 | // resulting edit replaces one or more complete lines. |
| 111 | // See ApplyEdits for preconditions. |
| 112 | func lineEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/lineEdits().(src string, edits []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#) ([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, error) { |
| 113 | edits, _, err := validatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/validate().(src, edits) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | |
| 118 | // Do all deletions begin and end at the start of a line, |
| 119 | // and all insertions end with a newline? |
| 120 | // (This is merely a fast path.) |
| 121 | for _, edit := range edits { |
| 122 | if edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. >= len(src) || // insertion at EOF |
| 123 | edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. > 0 && src[edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.-1] != '\n' || // not at line start |
| 124 | edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. > 0 && src[edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End.-1] != '\n' || // not at line start |
| 125 | edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. != "" && edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.[len(edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.)-1] != '\n' { // partial insert |
| 126 | goto expand // slow path |
| 127 | } |
| 128 | } |
| 129 | return edits, nil // aligned |
| 130 | |
| 131 | expand: |
| 132 | if len(edits) == 0 { |
| 133 | return edits, nil // no edits (unreachable due to fast path) |
| 134 | } |
| 135 | expanded := make([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, 0, len(edits)) // a guess |
| 136 | prev := edits[0] |
| 137 | // TODO(adonovan): opt: start from the first misaligned edit. |
| 138 | // TODO(adonovan): opt: avoid quadratic cost of string += string. |
| 139 | for _, edit := range edits[1:] { |
| 140 | between := src[prev.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End.:edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.] |
| 141 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(between, "\n") { |
| 142 | // overlapping lines: combine with previous edit. |
| 143 | prev.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. += between + edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. |
| 144 | prev.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. = edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. |
| 145 | } else { |
| 146 | // non-overlapping lines: flush previous edit. |
| 147 | expanded = append(expanded, expandEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/expandEdit().(prev, src)) |
| 148 | prev = edit |
| 149 | } |
| 150 | } |
| 151 | return append(expanded, expandEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/expandEdit().(prev, src)), nil // flush final edit |
| 152 | } |
| 153 | |
| 154 | // expandEdit returns edit expanded to complete whole lines. |
| 155 | func expandEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/expandEdit().(edit Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, src string) Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# { |
| 156 | // Expand start left to start of line. |
| 157 | // (delta is the zero-based column number of start.) |
| 158 | start := edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. |
| 159 | if delta := start - 1 - stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(src[:start], "\n"); delta > 0 { |
| 160 | edit.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start. -= delta |
| 161 | edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. = src[start-delta:start] + edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. |
| 162 | } |
| 163 | |
| 164 | // Expand end right to end of line. |
| 165 | end := edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. |
| 166 | if end > 0 && src[end-1] != '\n' || |
| 167 | edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. != "" && edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.[len(edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.)-1] != '\n' { |
| 168 | if nl := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 strings/IndexByte().(src[end:], '\n'); nl < 0 { |
| 169 | edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. = len(src) // extend to EOF |
| 170 | } else { |
| 171 | edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End. = end + nl + 1 // extend beyond \n |
| 172 | } |
| 173 | } |
| 174 | edit.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New. += src[end:edit.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End.] |
| 175 | |
| 176 | return edit |
| 177 | } |