| 1 | // Copyright 2022 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 diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 10 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/" |
| 11 | |
| 12 | "golang.org/x/tools/internal/diff/lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/" |
| 13 | ) |
| 14 | |
| 15 | // Lines computes differences between two strings. All edits are at line boundaries. |
| 16 | func Linesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Lines().(before, after string) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# { |
| 17 | beforeLines, bOffsets := splitLinesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/splitLines().(before) |
| 18 | afterLines, _ := splitLinesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/splitLines().(after) |
| 19 | diffs := lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/.DiffLinesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/DiffLines().(beforeLines, afterLines) |
| 20 | |
| 21 | // Convert from LCS diffs to Edits |
| 22 | res := make([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, len(diffs)) |
| 23 | for i, d := range diffs { |
| 24 | res[i] = Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#{ |
| 25 | Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#Start.: bOffsets[d.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#Start.], |
| 26 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#End.: bOffsets[d.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#End.], |
| 27 | Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#New.: stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(afterLines[d.ReplStartscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#ReplStart.:d.ReplEndscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#ReplEnd.], ""), |
| 28 | } |
| 29 | } |
| 30 | return res |
| 31 | } |
| 32 | |
| 33 | // Strings computes the differences between two strings. |
| 34 | // The resulting edits respect rune boundaries. |
| 35 | func Stringsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Strings().(before, after string) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# { |
| 36 | if before == after { |
| 37 | return nil // common case |
| 38 | } |
| 39 | |
| 40 | if isASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/isASCII().(before) && isASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/isASCII().(after) { |
| 41 | // TODO(adonovan): opt: specialize diffASCII for strings. |
| 42 | return diffASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/diffASCII().([]byte(before), []byte(after)) |
| 43 | } |
| 44 | return diffRunesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/diffRunes().([]rune(before), []rune(after)) |
| 45 | } |
| 46 | |
| 47 | // Bytes computes the differences between two byte slices. |
| 48 | // The resulting edits respect rune boundaries. |
| 49 | func Bytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Bytes().(before, after []byte) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# { |
| 50 | if bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.25.0 bytes/Equal().(before, after) { |
| 51 | return nil // common case |
| 52 | } |
| 53 | |
| 54 | if isASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/isASCII().(before) && isASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/isASCII().(after) { |
| 55 | return diffASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/diffASCII().(before, after) |
| 56 | } |
| 57 | return diffRunesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/diffRunes().(runesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/runes().(before), runesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/runes().(after)) |
| 58 | } |
| 59 | |
| 60 | func diffASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/diffASCII().(before, after []byte) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# { |
| 61 | diffs := lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/.DiffBytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/DiffBytes().(before, after) |
| 62 | |
| 63 | // Convert from LCS diffs. |
| 64 | res := make([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, len(diffs)) |
| 65 | for i, d := range diffs { |
| 66 | res[i] = Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#{d.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#Start., d.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#End., string(after[d.ReplStartscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#ReplStart.:d.ReplEndscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#ReplEnd.])} |
| 67 | } |
| 68 | return res |
| 69 | } |
| 70 | |
| 71 | func diffRunesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/diffRunes().(before, after []rune) []Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit# { |
| 72 | diffs := lcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/.DiffRunesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/DiffRunes().(before, after) |
| 73 | |
| 74 | // The diffs returned by the lcs package use indexes |
| 75 | // into whatever slice was passed in. |
| 76 | // Convert rune offsets to byte offsets. |
| 77 | res := make([]Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#, len(diffs)) |
| 78 | lastEnd := 0 |
| 79 | utf8Len := 0 |
| 80 | for i, d := range diffs { |
| 81 | utf8Len += runesLenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/runesLen().(before[lastEnd:d.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#Start.]) // text between edits |
| 82 | start := utf8Len |
| 83 | utf8Len += runesLenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/runesLen().(before[d.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#Start.:d.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#End.]) // text deleted by this edit |
| 84 | res[i] = Editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/Edit#{start, utf8Len, string(after[d.ReplStartscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#ReplStart.:d.ReplEndscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#ReplEnd.])} |
| 85 | lastEnd = d.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff/lcs`/Diff#End. |
| 86 | } |
| 87 | return res |
| 88 | } |
| 89 | |
| 90 | // runes is like []rune(string(bytes)) without the duplicate allocation. |
| 91 | func runesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/runes().(bytes []byte) []rune { |
| 92 | n := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.RuneCountscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/RuneCount().(bytes) |
| 93 | runes := make([]rune, n) |
| 94 | for i := range n { |
| 95 | r, sz := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.DecodeRunescip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/DecodeRune().(bytes) |
| 96 | bytes = bytes[sz:] |
| 97 | runes[i] = r |
| 98 | } |
| 99 | return runes |
| 100 | } |
| 101 | |
| 102 | // runesLen returns the length in bytes of the UTF-8 encoding of runes. |
| 103 | func runesLenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/runesLen().(runes []rune) (len int) { |
| 104 | for _, r := range runes { |
| 105 | len += utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.RuneLenscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/RuneLen().(r) |
| 106 | } |
| 107 | return len |
| 108 | } |
| 109 | |
| 110 | // isASCII reports whether s contains only ASCII. |
| 111 | func isASCIIscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/diff`/isASCII().[S string | []byte](s S) bool { |
| 112 | for i := 0; i < len(s); i++ { |
| 113 | if s[i] >= utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.RuneSelfscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/RuneSelf. { |
| 114 | return false |
| 115 | } |
| 116 | } |
| 117 | return true |
| 118 | } |