xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/builders/edit.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1*9bb1b549SSpandan Das// Copyright 2017 The Go Authors. All rights reserved.
2*9bb1b549SSpandan Das// Use of this source code is governed by a BSD-style
3*9bb1b549SSpandan Das// license that can be found in the LICENSE file.
4*9bb1b549SSpandan Das
5*9bb1b549SSpandan Das// Copied from go1.17 tree: //src/cmd/internal/edit/edit.go
6*9bb1b549SSpandan Das
7*9bb1b549SSpandan Das// Package edit implements buffered position-based editing of byte slices.
8*9bb1b549SSpandan Daspackage main
9*9bb1b549SSpandan Das
10*9bb1b549SSpandan Dasimport (
11*9bb1b549SSpandan Das	"fmt"
12*9bb1b549SSpandan Das	"sort"
13*9bb1b549SSpandan Das)
14*9bb1b549SSpandan Das
15*9bb1b549SSpandan Das// A Buffer is a queue of edits to apply to a given byte slice.
16*9bb1b549SSpandan Dastype Buffer struct {
17*9bb1b549SSpandan Das	old []byte
18*9bb1b549SSpandan Das	q   edits
19*9bb1b549SSpandan Das}
20*9bb1b549SSpandan Das
21*9bb1b549SSpandan Das// An edit records a single text modification: change the bytes in [start,end) to new.
22*9bb1b549SSpandan Dastype edit struct {
23*9bb1b549SSpandan Das	start int
24*9bb1b549SSpandan Das	end   int
25*9bb1b549SSpandan Das	new   string
26*9bb1b549SSpandan Das}
27*9bb1b549SSpandan Das
28*9bb1b549SSpandan Das// An edits is a list of edits that is sortable by start offset, breaking ties by end offset.
29*9bb1b549SSpandan Dastype edits []edit
30*9bb1b549SSpandan Das
31*9bb1b549SSpandan Dasfunc (x edits) Len() int      { return len(x) }
32*9bb1b549SSpandan Dasfunc (x edits) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
33*9bb1b549SSpandan Dasfunc (x edits) Less(i, j int) bool {
34*9bb1b549SSpandan Das	if x[i].start != x[j].start {
35*9bb1b549SSpandan Das		return x[i].start < x[j].start
36*9bb1b549SSpandan Das	}
37*9bb1b549SSpandan Das	return x[i].end < x[j].end
38*9bb1b549SSpandan Das}
39*9bb1b549SSpandan Das
40*9bb1b549SSpandan Das// NewBuffer returns a new buffer to accumulate changes to an initial data slice.
41*9bb1b549SSpandan Das// The returned buffer maintains a reference to the data, so the caller must ensure
42*9bb1b549SSpandan Das// the data is not modified until after the Buffer is done being used.
43*9bb1b549SSpandan Dasfunc NewBuffer(data []byte) *Buffer {
44*9bb1b549SSpandan Das	return &Buffer{old: data}
45*9bb1b549SSpandan Das}
46*9bb1b549SSpandan Das
47*9bb1b549SSpandan Dasfunc (b *Buffer) Insert(pos int, new string) {
48*9bb1b549SSpandan Das	if pos < 0 || pos > len(b.old) {
49*9bb1b549SSpandan Das		panic("invalid edit position")
50*9bb1b549SSpandan Das	}
51*9bb1b549SSpandan Das	b.q = append(b.q, edit{pos, pos, new})
52*9bb1b549SSpandan Das}
53*9bb1b549SSpandan Das
54*9bb1b549SSpandan Dasfunc (b *Buffer) Delete(start, end int) {
55*9bb1b549SSpandan Das	if end < start || start < 0 || end > len(b.old) {
56*9bb1b549SSpandan Das		panic("invalid edit position")
57*9bb1b549SSpandan Das	}
58*9bb1b549SSpandan Das	b.q = append(b.q, edit{start, end, ""})
59*9bb1b549SSpandan Das}
60*9bb1b549SSpandan Das
61*9bb1b549SSpandan Dasfunc (b *Buffer) Replace(start, end int, new string) {
62*9bb1b549SSpandan Das	if end < start || start < 0 || end > len(b.old) {
63*9bb1b549SSpandan Das		panic("invalid edit position")
64*9bb1b549SSpandan Das	}
65*9bb1b549SSpandan Das	b.q = append(b.q, edit{start, end, new})
66*9bb1b549SSpandan Das}
67*9bb1b549SSpandan Das
68*9bb1b549SSpandan Das// Bytes returns a new byte slice containing the original data
69*9bb1b549SSpandan Das// with the queued edits applied.
70*9bb1b549SSpandan Dasfunc (b *Buffer) Bytes() []byte {
71*9bb1b549SSpandan Das	// Sort edits by starting position and then by ending position.
72*9bb1b549SSpandan Das	// Breaking ties by ending position allows insertions at point x
73*9bb1b549SSpandan Das	// to be applied before a replacement of the text at [x, y).
74*9bb1b549SSpandan Das	sort.Stable(b.q)
75*9bb1b549SSpandan Das
76*9bb1b549SSpandan Das	var new []byte
77*9bb1b549SSpandan Das	offset := 0
78*9bb1b549SSpandan Das	for i, e := range b.q {
79*9bb1b549SSpandan Das		if e.start < offset {
80*9bb1b549SSpandan Das			e0 := b.q[i-1]
81*9bb1b549SSpandan Das			panic(fmt.Sprintf("overlapping edits: [%d,%d)->%q, [%d,%d)->%q", e0.start, e0.end, e0.new, e.start, e.end, e.new))
82*9bb1b549SSpandan Das		}
83*9bb1b549SSpandan Das		new = append(new, b.old[offset:e.start]...)
84*9bb1b549SSpandan Das		offset = e.end
85*9bb1b549SSpandan Das		new = append(new, e.new...)
86*9bb1b549SSpandan Das	}
87*9bb1b549SSpandan Das	new = append(new, b.old[offset:]...)
88*9bb1b549SSpandan Das	return new
89*9bb1b549SSpandan Das}
90*9bb1b549SSpandan Das
91*9bb1b549SSpandan Das// String returns a string containing the original data
92*9bb1b549SSpandan Das// with the queued edits applied.
93*9bb1b549SSpandan Dasfunc (b *Buffer) String() string {
94*9bb1b549SSpandan Das	return string(b.Bytes())
95*9bb1b549SSpandan Das}
96