Position returns the 1-based line:column position for byteOffset.
(byteOffset int)
| 25 | |
| 26 | // Position returns the 1-based line:column position for byteOffset. |
| 27 | func (m *ByteOffsetPositionMapper) Position(byteOffset int) *storepb.Position { |
| 28 | if byteOffset < 0 { |
| 29 | byteOffset = 0 |
| 30 | } |
| 31 | if byteOffset > len(m.sql) { |
| 32 | byteOffset = len(m.sql) |
| 33 | } |
| 34 | if byteOffset < m.byteOffset { |
| 35 | return byteOffsetToRunePosition(m.sql, byteOffset) |
| 36 | } |
| 37 | |
| 38 | for m.byteOffset < byteOffset { |
| 39 | r, size := utf8.DecodeRuneInString(m.sql[m.byteOffset:]) |
| 40 | switch r { |
| 41 | case '\r': |
| 42 | m.line++ |
| 43 | m.runeCol = 0 |
| 44 | case '\n': |
| 45 | if m.byteOffset == 0 || m.sql[m.byteOffset-1] != '\r' { |
| 46 | m.line++ |
| 47 | m.runeCol = 0 |
| 48 | } |
| 49 | default: |
| 50 | m.runeCol++ |
| 51 | } |
| 52 | m.byteOffset += size |
| 53 | } |
| 54 | |
| 55 | return &storepb.Position{ |
| 56 | Line: m.line, |
| 57 | Column: m.runeCol + 1, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func byteOffsetToRunePosition(sql string, byteOffset int) *storepb.Position { |
| 62 | line := int32(1) |