| 1 | // Copyright 2017 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 edit implements buffered position-based editing of byte slices. |
| 6 | package editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/ |
| 7 | |
| 8 | import ( |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 11 | ) |
| 12 | |
| 13 | // A Buffer is a queue of edits to apply to a given byte slice. |
| 14 | type Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer# struct { |
| 15 | oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old. []byte |
| 16 | qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q. editsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits# |
| 17 | } |
| 18 | |
| 19 | // An edit records a single text modification: change the bytes in [start,end) to new. |
| 20 | type editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit# struct { |
| 21 | startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start. int |
| 22 | endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#end. int |
| 23 | newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#new. string |
| 24 | } |
| 25 | |
| 26 | // An edits is a list of edits that is sortable by start offset, breaking ties by end offset. |
| 27 | type editsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits# []editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit# |
| 28 | |
| 29 | func (x editsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits#) Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits#Len().() int { return len(x) } |
| 30 | func (x editsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits#) Swapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits#Swap().(i, j int) { x[i], x[j] = x[j], x[i] } |
| 31 | func (x editsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits#) Lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edits#Less().(i, j int) bool { |
| 32 | if x[i].startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start. != x[j].startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start. { |
| 33 | return x[i].startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start. < x[j].startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start. |
| 34 | } |
| 35 | return x[i].endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#end. < x[j].endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#end. |
| 36 | } |
| 37 | |
| 38 | // NewBuffer returns a new buffer to accumulate changes to an initial data slice. |
| 39 | // The returned buffer maintains a reference to the data, so the caller must ensure |
| 40 | // the data is not modified until after the Buffer is done being used. |
| 41 | func NewBufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/NewBuffer().(old []byte) *Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer# { |
| 42 | return &Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#{oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old.: old} |
| 43 | } |
| 44 | |
| 45 | // Insert inserts the new string at old[pos:pos]. |
| 46 | func (b *Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#) Insertscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#Insert().(pos int, new string) { |
| 47 | if pos < 0 || pos > len(b.oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old.) { |
| 48 | panic("invalid edit position") |
| 49 | } |
| 50 | b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q. = append(b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q., editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#{pos, pos, new}) |
| 51 | } |
| 52 | |
| 53 | // Delete deletes the text old[start:end]. |
| 54 | func (b *Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#) Deletescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#Delete().(start, end int) { |
| 55 | if end < start || start < 0 || end > len(b.oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old.) { |
| 56 | panic("invalid edit position") |
| 57 | } |
| 58 | b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q. = append(b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q., editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#{start, end, ""}) |
| 59 | } |
| 60 | |
| 61 | // Replace replaces old[start:end] with new. |
| 62 | func (b *Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#) Replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#Replace().(start, end int, new string) { |
| 63 | if end < start || start < 0 || end > len(b.oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old.) { |
| 64 | panic("invalid edit position") |
| 65 | } |
| 66 | b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q. = append(b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q., editscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#{start, end, new}) |
| 67 | } |
| 68 | |
| 69 | // Bytes returns a new byte slice containing the original data |
| 70 | // with the queued edits applied. |
| 71 | func (b *Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#) Bytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#Bytes().() []byte { |
| 72 | // Sort edits by starting position and then by ending position. |
| 73 | // Breaking ties by ending position allows insertions at point x |
| 74 | // to be applied before a replacement of the text at [x, y). |
| 75 | 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().(b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q.) |
| 76 | |
| 77 | var new []byte |
| 78 | offset := 0 |
| 79 | for i, e := range b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q. { |
| 80 | if e.startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start. < offset { |
| 81 | e0 := b.qscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#q.[i-1] |
| 82 | panic(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().("overlapping edits: [%d,%d)->%q, [%d,%d)->%q", e0.startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start., e0.endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#end., e0.newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#new., e.startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start., e.endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#end., e.newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#new.)) |
| 83 | } |
| 84 | new = append(new, b.oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old.[offset:e.startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#start.]...) |
| 85 | offset = e.endscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#end. |
| 86 | new = append(new, e.newscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/edit#new....) |
| 87 | } |
| 88 | new = append(new, b.oldscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#old.[offset:]...) |
| 89 | return new |
| 90 | } |
| 91 | |
| 92 | // String returns a string containing the original data |
| 93 | // with the queued edits applied. |
| 94 | func (b *Bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#String().() string { |
| 95 | return string(b.Bytesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/edit`/Buffer#Bytes().()) |
| 96 | } |