MCPcopy Create free account
hub / github.com/bytebase/bytebase / ByteOffsetToRunePosition

Function ByteOffsetToRunePosition

backend/plugin/parser/mysql/omni.go:54–77  ·  view source on GitHub ↗

ByteOffsetToRunePosition converts a byte offset in sql to a 1-based line:column where column is measured in Unicode code points (runes), matching storepb.Position semantics.

(sql string, byteOffset int)

Source from the content-addressed store, hash-verified

52// ByteOffsetToRunePosition converts a byte offset in sql to a 1-based line:column
53// where column is measured in Unicode code points (runes), matching storepb.Position semantics.
54func ByteOffsetToRunePosition(sql string, byteOffset int) *storepb.Position {
55 if byteOffset > len(sql) {
56 byteOffset = len(sql)
57 }
58
59 line := int32(1)
60 runeCol := int32(0) // 0-based rune count on current line
61 i := 0
62 for i < byteOffset {
63 r, size := utf8.DecodeRuneInString(sql[i:])
64 if r == '\n' {
65 line++
66 runeCol = 0
67 } else {
68 runeCol++
69 }
70 i += size
71 }
72
73 return &storepb.Position{
74 Line: line,
75 Column: runeCol + 1, // convert to 1-based
76 }
77}

Callers 2

parseMySQLStatementFunction · 0.70
convertOmniErrorFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected