Debian Codegraph @

annotate.go

raw symbols SCIP
1 package annotate
2
3 import (
4 "bytes"
5 "errors"
6 "io"
7 )
8
9 type Annotation struct {
10 // Start and End byte offsets (not rune offsets).
11 Start, End int
12
13 Left, Right []byte
14 WantInner int
15 }
16
17 type Annotations []*Annotation
18
19 func (a Annotations) Len() int { return len(a) }
20 func (a Annotations) Less(i, j int) bool {
21 // Sort by start position, breaking ties by preferring longer
22 // matches.
23 ai, aj := a[i], a[j]
24 if ai.Start == aj.Start {
25 if ai.End == aj.End {
26 return ai.WantInner < aj.WantInner
27 }
28 return ai.End > aj.End
29 } else {
30 return ai.Start < aj.Start
31 }
32 }
33 func (a Annotations) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
34
35 // Annotates src with annotations in anns.
36 //
37 // Annotating an empty byte array always returns an empty byte array.
38 //
39 // Assumes anns is sorted (using sort.Sort(anns)).
40 func Annotate(src []byte, anns Annotations, writeContent func(io.Writer, []byte)) ([]byte, error) {
41 out := bytes.NewBuffer(make([]byte, 0, len(src)+20*len(anns)))
42 var err error
43
44 // Keep a stack of open annotations (i.e., that have been opened and not yet
45 // closed).
46 var open []*Annotation
47
48 for b := range src {
49 // Open annotations that begin here.
50 for i, a := range anns {
51 if a.Start < 0 || a.Start == b {
52 if a.Start < 0 {
53 err = ErrStartOutOfBounds
54 }
55
56 out.Write(a.Left)
57
58 if a.Start == a.End {
59 out.Write(a.Right)
60 } else {
61 // Put this annotation on the stack of annotations that will need
62 // to be closed. We remove it from anns at the end of the loop
63 // (to avoid modifying anns while we're iterating over it).
64 open = append(open, a)
65 }
66 } else if a.Start > b {
67 // Remove all annotations that we opened (we already put them on the
68 // stack of annotations that will need to be closed).
69 anns = anns[i:]
70 break
71 }
72 }
73
74 if writeContent == nil {
75 out.Write(src[b : b+1])
76 } else {
77 writeContent(out, src[b:b+1])
78 }
79
80 // Close annotations that end after this byte, handling overlapping
81 // elements as described below. Elements of open are ordered by their
82 // annotation start position.
83
84 // We need to close all annotatations ending after this byte, as well as
85 // annotations that overlap this annotation's end and should reopen
86 // after it closes.
87 var toClose []*Annotation
88
89 // Find annotations ending after this byte.
90 minStart := 0 // start of the leftmost annotation closing here
91 for i := len(open) - 1; i >= 0; i-- {
92 a := open[i]
93 if a.End == b+1 {
94 toClose = append(toClose, a)
95 if minStart == 0 || a.Start < minStart {
96 minStart = a.Start
97 }
98 open = append(open[:i], open[i+1:]...)
99 }
100 }
101
102 // Find annotations that overlap annotations closing after this and
103 // that should reopen after it closes.
104 if toClose != nil {
105 for i := len(open) - 1; i >= 0; i-- {
106 if a := open[i]; a.Start > minStart {
107 out.Write(a.Right)
108 }
109 }
110 }
111
112 for _, a := range toClose {
113 out.Write(a.Right)
114 }
115
116 if toClose != nil {
117 for _, a := range open {
118 if a.Start > minStart {
119 out.Write(a.Left)
120 }
121 }
122 }
123 }
124
125 if len(open) > 0 {
126 if err == ErrStartOutOfBounds {
127 err = ErrStartAndEndOutOfBounds
128 } else {
129 err = ErrEndOutOfBounds
130 }
131
132 // Clean up by closing unclosed annotations, in the order they would
133 // have been closed in.
134 for i := len(open) - 1; i >= 0; i-- {
135 a := open[i]
136 out.Write(a.Right)
137 }
138 }
139
140 return out.Bytes(), err
141 }
142
143 var (
144 ErrStartOutOfBounds = errors.New("annotation start out of bounds")
145 ErrEndOutOfBounds = errors.New("annotation end out of bounds")
146 ErrStartAndEndOutOfBounds = errors.New("annotations start and end out of bounds")
147 )
148
149 func IsOutOfBounds(err error) bool {
150 return err == ErrStartOutOfBounds || err == ErrEndOutOfBounds || err == ErrStartAndEndOutOfBounds
151 }
152
153 func annLefts(as []*Annotation) []string {
154 ls := make([]string, len(as))
155 for i, a := range as {
156 ls[i] = string(a.Left)
157 }
158 return ls
159 }