(text: string, byteOffset: number)
| 226 | } |
| 227 | |
| 228 | function utf8ByteOffsetToStringIndex(text: string, byteOffset: number) { |
| 229 | if (byteOffset <= 0) return 0 |
| 230 | |
| 231 | let bytes = 0 |
| 232 | for (let index = 0; index < text.length; ) { |
| 233 | const codePoint = text.codePointAt(index) |
| 234 | if (codePoint === undefined) return text.length |
| 235 | |
| 236 | const nextIndex = index + (codePoint > 0xffff ? 2 : 1) |
| 237 | bytes += utf8.encode(text.slice(index, nextIndex)).length |
| 238 | if (bytes >= byteOffset) return nextIndex |
| 239 | index = nextIndex |
| 240 | } |
| 241 | |
| 242 | return text.length |
| 243 | } |
| 244 | |
| 245 | function offsetsToSelection(text: string, startOffset: number, endOffset: number) { |
| 246 | const start = Math.max(0, Math.min(startOffset, text.length)) |
no outgoing calls
no test coverage detected