...

Source file src/github.com/bazelbuild/rules_go/go/tools/builders/edit.go

Documentation: github.com/bazelbuild/rules_go/go/tools/builders

     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.
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"sort"
    13  )
    14  
    15  // A Buffer is a queue of edits to apply to a given byte slice.
    16  type 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.
    22  type 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.
    29  type edits []edit
    30  
    31  func (x edits) Len() int      { return len(x) }
    32  func (x edits) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
    33  func (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.
    43  func NewBuffer(data []byte) *Buffer {
    44  	return &Buffer{old: data}
    45  }
    46  
    47  func (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  
    54  func (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  
    61  func (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.
    70  func (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.
    93  func (b *Buffer) String() string {
    94  	return string(b.Bytes())
    95  }
    96  

View as plain text