MCPcopy Create free account
hub / github.com/anomalyco/opencode / utf8ByteOffsetToStringIndex

Function utf8ByteOffsetToStringIndex

packages/tui/src/editor-zed.ts:228–243  ·  view source on GitHub ↗
(text: string, byteOffset: number)

Source from the content-addressed store, hash-verified

226}
227
228function 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
245function offsetsToSelection(text: string, startOffset: number, endOffset: number) {
246 const start = Math.max(0, Math.min(startOffset, text.length))

Callers 2

resolveZedSelectionFunction · 0.85
offsetToPositionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected