| 1 |
package annotate |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
) |
| 8 |
|
| 9 |
type Annotation struct { |
| 10 |
|
| 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 |
|
| 22 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 45 |
|
| 46 |
var open []*Annotation |
| 47 |
|
| 48 |
for b := range src { |
| 49 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
open = append(open, a) |
| 65 |
} |
| 66 |
} else if a.Start > b { |
| 67 |
|
| 68 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
var toClose []*Annotation |
| 88 |
|
| 89 |
|
| 90 |
minStart := 0 |
| 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 |
|
| 103 |
|
| 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 |
|
| 133 |
|
| 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 |
} |