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