byteOffsetToPosition converts a 0-based byte offset to a 1-based line and 1-based column (in runes).
(s string, offset int)
| 142 | // byteOffsetToPosition converts a 0-based byte offset to a 1-based line and |
| 143 | // 1-based column (in runes). |
| 144 | func byteOffsetToPosition(s string, offset int) (line, col int) { |
| 145 | line = 1 |
| 146 | col = 1 |
| 147 | for i := 0; i < offset && i < len(s); { |
| 148 | r, size := utf8.DecodeRuneInString(s[i:]) |
| 149 | if r == '\n' { |
| 150 | line++ |
| 151 | col = 1 |
| 152 | } else { |
| 153 | col++ |
| 154 | } |
| 155 | i += size |
| 156 | } |
| 157 | return line, col |
| 158 | } |