| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package runes |
| 17 |
|
| 18 |
import ( |
| 19 |
"strings" |
| 20 |
"unicode/utf8" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
type Buffer interface { |
| 25 |
Get(i int) rune |
| 26 |
Slice(i, j int) string |
| 27 |
Len() int |
| 28 |
} |
| 29 |
|
| 30 |
type emptyBuffer struct{} |
| 31 |
|
| 32 |
func (e *emptyBuffer) Get(i int) rune { |
| 33 |
panic("slice index out of bounds") |
| 34 |
} |
| 35 |
|
| 36 |
func (e *emptyBuffer) Slice(i, j int) string { |
| 37 |
if i != 0 || i != j { |
| 38 |
panic("slice index out of bounds") |
| 39 |
} |
| 40 |
return "" |
| 41 |
} |
| 42 |
|
| 43 |
func (e *emptyBuffer) Len() int { |
| 44 |
return 0 |
| 45 |
} |
| 46 |
|
| 47 |
var _ Buffer = &emptyBuffer{} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
type asciiBuffer struct { |
| 52 |
arr []byte |
| 53 |
} |
| 54 |
|
| 55 |
func (a *asciiBuffer) Get(i int) rune { |
| 56 |
return rune(uint32(a.arr[i])) |
| 57 |
} |
| 58 |
|
| 59 |
func (a *asciiBuffer) Slice(i, j int) string { |
| 60 |
return string(a.arr[i:j]) |
| 61 |
} |
| 62 |
|
| 63 |
func (a *asciiBuffer) Len() int { |
| 64 |
return len(a.arr) |
| 65 |
} |
| 66 |
|
| 67 |
var _ Buffer = &asciiBuffer{} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
type basicBuffer struct { |
| 72 |
arr []uint16 |
| 73 |
} |
| 74 |
|
| 75 |
func (b *basicBuffer) Get(i int) rune { |
| 76 |
return rune(uint32(b.arr[i])) |
| 77 |
} |
| 78 |
|
| 79 |
func (b *basicBuffer) Slice(i, j int) string { |
| 80 |
var str strings.Builder |
| 81 |
str.Grow((j - i) * 3) |
| 82 |
for ; i < j; i++ { |
| 83 |
str.WriteRune(rune(uint32(b.arr[i]))) |
| 84 |
} |
| 85 |
return str.String() |
| 86 |
} |
| 87 |
|
| 88 |
func (b *basicBuffer) Len() int { |
| 89 |
return len(b.arr) |
| 90 |
} |
| 91 |
|
| 92 |
var _ Buffer = &basicBuffer{} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type supplementalBuffer struct { |
| 97 |
arr []rune |
| 98 |
} |
| 99 |
|
| 100 |
func (s *supplementalBuffer) Get(i int) rune { |
| 101 |
return rune(uint32(s.arr[i])) |
| 102 |
} |
| 103 |
|
| 104 |
func (s *supplementalBuffer) Slice(i, j int) string { |
| 105 |
return string(s.arr[i:j]) |
| 106 |
} |
| 107 |
|
| 108 |
func (s *supplementalBuffer) Len() int { |
| 109 |
return len(s.arr) |
| 110 |
} |
| 111 |
|
| 112 |
var _ Buffer = &supplementalBuffer{} |
| 113 |
|
| 114 |
var nilBuffer = &emptyBuffer{} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
func NewBuffer(data string) Buffer { |
| 130 |
buf, _ := newBuffer(data, false) |
| 131 |
return buf |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
func NewBufferAndLineOffsets(data string) (Buffer, []int32) { |
| 148 |
return newBuffer(data, true) |
| 149 |
} |
| 150 |
|
| 151 |
func newBuffer(data string, lines bool) (Buffer, []int32) { |
| 152 |
if len(data) == 0 { |
| 153 |
return nilBuffer, []int32{0} |
| 154 |
} |
| 155 |
var ( |
| 156 |
idx = 0 |
| 157 |
off int32 = 0 |
| 158 |
buf8 = make([]byte, 0, len(data)) |
| 159 |
buf16 []uint16 |
| 160 |
buf32 []rune |
| 161 |
offs []int32 |
| 162 |
) |
| 163 |
for idx < len(data) { |
| 164 |
r, s := utf8.DecodeRuneInString(data[idx:]) |
| 165 |
idx += s |
| 166 |
if lines && r == '\n' { |
| 167 |
offs = append(offs, off+1) |
| 168 |
} |
| 169 |
if r < utf8.RuneSelf { |
| 170 |
buf8 = append(buf8, byte(r)) |
| 171 |
off++ |
| 172 |
continue |
| 173 |
} |
| 174 |
if r <= 0xffff { |
| 175 |
buf16 = make([]uint16, len(buf8), len(data)) |
| 176 |
for i, v := range buf8 { |
| 177 |
buf16[i] = uint16(v) |
| 178 |
} |
| 179 |
buf8 = nil |
| 180 |
buf16 = append(buf16, uint16(r)) |
| 181 |
off++ |
| 182 |
goto copy16 |
| 183 |
} |
| 184 |
buf32 = make([]rune, len(buf8), len(data)) |
| 185 |
for i, v := range buf8 { |
| 186 |
buf32[i] = rune(uint32(v)) |
| 187 |
} |
| 188 |
buf8 = nil |
| 189 |
buf32 = append(buf32, r) |
| 190 |
off++ |
| 191 |
goto copy32 |
| 192 |
} |
| 193 |
if lines { |
| 194 |
offs = append(offs, off+1) |
| 195 |
} |
| 196 |
return &asciiBuffer{ |
| 197 |
arr: buf8, |
| 198 |
}, offs |
| 199 |
copy16: |
| 200 |
for idx < len(data) { |
| 201 |
r, s := utf8.DecodeRuneInString(data[idx:]) |
| 202 |
idx += s |
| 203 |
if lines && r == '\n' { |
| 204 |
offs = append(offs, off+1) |
| 205 |
} |
| 206 |
if r <= 0xffff { |
| 207 |
buf16 = append(buf16, uint16(r)) |
| 208 |
off++ |
| 209 |
continue |
| 210 |
} |
| 211 |
buf32 = make([]rune, len(buf16), len(data)) |
| 212 |
for i, v := range buf16 { |
| 213 |
buf32[i] = rune(uint32(v)) |
| 214 |
} |
| 215 |
buf16 = nil |
| 216 |
buf32 = append(buf32, r) |
| 217 |
off++ |
| 218 |
goto copy32 |
| 219 |
} |
| 220 |
if lines { |
| 221 |
offs = append(offs, off+1) |
| 222 |
} |
| 223 |
return &basicBuffer{ |
| 224 |
arr: buf16, |
| 225 |
}, offs |
| 226 |
copy32: |
| 227 |
for idx < len(data) { |
| 228 |
r, s := utf8.DecodeRuneInString(data[idx:]) |
| 229 |
idx += s |
| 230 |
if lines && r == '\n' { |
| 231 |
offs = append(offs, off+1) |
| 232 |
} |
| 233 |
buf32 = append(buf32, r) |
| 234 |
off++ |
| 235 |
} |
| 236 |
if lines { |
| 237 |
offs = append(offs, off+1) |
| 238 |
} |
| 239 |
return &supplementalBuffer{ |
| 240 |
arr: buf32, |
| 241 |
}, offs |
| 242 |
} |