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

Function byteOffsetToLineCol

backend/plugin/parser/doris/diagnose.go:46–65  ·  view source on GitHub ↗

byteOffsetToLineCol returns the 1-based line and 1-based column for a byte offset within the statement. Counts \n as line breaks. The 1-based column matches the storepb.Position convention that ConvertSyntaxErrorToDiagnostic expects (it subtracts 1 internally to land on the 0-based LSP offset).

(s string, offset int)

Source from the content-addressed store, hash-verified

44// matches the storepb.Position convention that ConvertSyntaxErrorToDiagnostic
45// expects (it subtracts 1 internally to land on the 0-based LSP offset).
46func byteOffsetToLineCol(s string, offset int) (int, int) {
47 if offset < 0 {
48 return 1, 1
49 }
50 if offset > len(s) {
51 offset = len(s)
52 }
53 line, col := 1, 1
54 for i := 0; i < offset; {
55 r, size := utf8.DecodeRuneInString(s[i:])
56 if r == '\n' {
57 line++
58 col = 1
59 } else {
60 col++
61 }
62 i += size
63 }
64 return line, col
65}

Callers 1

DiagnoseFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected