| 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package runes provides interfaces and utilities for working with runes. |
| 16 | package runes |
| 17 | |
| 18 | import ( |
| 19 | "strings" |
| 20 | "unicode/utf8" |
| 21 | ) |
| 22 | |
| 23 | // Buffer is an interface for accessing a contiguous array of code points. |
| 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 | // asciiBuffer is an implementation for an array of code points that contain code points only from |
| 50 | // the ASCII character set. |
| 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 | // basicBuffer is an implementation for an array of code points that contain code points from both |
| 70 | // the Latin-1 character set and Basic Multilingual Plane. |
| 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) // Worst case encoding size for 0xffff is 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 | // supplementalBuffer is an implementation for an array of code points that contain code points from |
| 95 | // the Latin-1 character set, Basic Multilingual Plane, or the Supplemental Multilingual Plane. |
| 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 | // NewBuffer returns an efficient implementation of Buffer for the given text based on the ranges of |
| 117 | // the encoded code points contained within. |
| 118 | // |
| 119 | // Code points are represented as an array of byte, uint16, or rune. This approach ensures that |
| 120 | // each index represents a code point by itself without needing to use an array of rune. At first |
| 121 | // we assume all code points are less than or equal to '\u007f'. If this holds true, the |
| 122 | // underlying storage is a byte array containing only ASCII characters. If we encountered a code |
| 123 | // point above this range but less than or equal to '\uffff' we allocate a uint16 array, copy the |
| 124 | // elements of previous byte array to the uint16 array, and continue. If this holds true, the |
| 125 | // underlying storage is a uint16 array containing only Unicode characters in the Basic Multilingual |
| 126 | // Plane. If we encounter a code point above '\uffff' we allocate an rune array, copy the previous |
| 127 | // elements of the byte or uint16 array, and continue. The underlying storage is an rune array |
| 128 | // containing any Unicode character. |
| 129 | func NewBuffer(data string) Buffer { |
| 130 | buf, _ := newBuffer(data, false) |
| 131 | return buf |
| 132 | } |
| 133 | |
| 134 | // NewBufferAndLineOffsets returns an efficient implementation of Buffer for the given text based on |
| 135 | // the ranges of the encoded code points contained within, as well as returning the line offsets. |
| 136 | // |
| 137 | // Code points are represented as an array of byte, uint16, or rune. This approach ensures that |
| 138 | // each index represents a code point by itself without needing to use an array of rune. At first |
| 139 | // we assume all code points are less than or equal to '\u007f'. If this holds true, the |
| 140 | // underlying storage is a byte array containing only ASCII characters. If we encountered a code |
| 141 | // point above this range but less than or equal to '\uffff' we allocate a uint16 array, copy the |
| 142 | // elements of previous byte array to the uint16 array, and continue. If this holds true, the |
| 143 | // underlying storage is a uint16 array containing only Unicode characters in the Basic Multilingual |
| 144 | // Plane. If we encounter a code point above '\uffff' we allocate an rune array, copy the previous |
| 145 | // elements of the byte or uint16 array, and continue. The underlying storage is an rune array |
| 146 | // containing any Unicode character. |
| 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 | } |