...
1
2
3
4
5
6 package edit
7
8 import (
9 "fmt"
10 "sort"
11 )
12
13
14 type Buffer struct {
15 old []byte
16 q edits
17 }
18
19
20 type edit struct {
21 start int
22 end int
23 new string
24 }
25
26
27 type edits []edit
28
29 func (x edits) Len() int { return len(x) }
30 func (x edits) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
31 func (x edits) Less(i, j int) bool {
32 if x[i].start != x[j].start {
33 return x[i].start < x[j].start
34 }
35 return x[i].end < x[j].end
36 }
37
38
39
40
41 func NewBuffer(old []byte) *Buffer {
42 return &Buffer{old: old}
43 }
44
45
46 func (b *Buffer) Insert(pos int, new string) {
47 if pos < 0 || pos > len(b.old) {
48 panic("invalid edit position")
49 }
50 b.q = append(b.q, edit{pos, pos, new})
51 }
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
62 func (b *Buffer) Replace(start, end int, new string) {
63 if end < start || start < 0 || end > len(b.old) {
64 panic("invalid edit position")
65 }
66 b.q = append(b.q, edit{start, end, new})
67 }
68
69
70
71 func (b *Buffer) Bytes() []byte {
72
73
74
75 sort.Stable(b.q)
76
77 var new []byte
78 offset := 0
79 for i, e := range b.q {
80 if e.start < offset {
81 e0 := b.q[i-1]
82 panic(fmt.Sprintf("overlapping edits: [%d,%d)->%q, [%d,%d)->%q", e0.start, e0.end, e0.new, e.start, e.end, e.new))
83 }
84 new = append(new, b.old[offset:e.start]...)
85 offset = e.end
86 new = append(new, e.new...)
87 }
88 new = append(new, b.old[offset:]...)
89 return new
90 }
91
92
93
94 func (b *Buffer) String() string {
95 return string(b.Bytes())
96 }
97
View as plain text