MCPcopy Index your code
hub / github.com/bytebase/bytebase / lineColumnToByteOffset

Function lineColumnToByteOffset

backend/plugin/parser/pg/completion.go:58–78  ·  view source on GitHub ↗

lineColumnToByteOffset converts 1-based line and 0-based column (character count) to a byte offset. Column is measured in runes (characters), not bytes, so that multi-byte UTF-8 characters are handled correctly.

(sql string, line, column int)

Source from the content-addressed store, hash-verified

56// to a byte offset. Column is measured in runes (characters), not bytes, so that
57// multi-byte UTF-8 characters are handled correctly.
58func lineColumnToByteOffset(sql string, line, column int) int {
59 currentLine := 1
60 for i := 0; i < len(sql); i++ {
61 if currentLine == line {
62 // Count 'column' runes from position i to get the byte offset.
63 pos := i
64 for c := 0; c < column && pos < len(sql); c++ {
65 _, size := utf8.DecodeRuneInString(sql[pos:])
66 pos += size
67 }
68 if pos > len(sql) {
69 return len(sql)
70 }
71 return pos
72 }
73 if sql[i] == '\n' {
74 currentLine++
75 }
76 }
77 return len(sql)
78}
79
80// isNoSeparatorRequired returns true if tokenType is a punctuation/operator token
81// that does not require whitespace separation from the next token.

Callers 2

computeSQLAndByteOffsetFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected