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)
| 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. |
| 54 | func 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 | } |
no outgoing calls
no test coverage detected