lineOffsetToBytePos converts a 1-based line number and 0-based character (rune) offset to a 0-based byte position in the string.
(s string, line, offset int)
| 524 | // lineOffsetToBytePos converts a 1-based line number and 0-based character |
| 525 | // (rune) offset to a 0-based byte position in the string. |
| 526 | func lineOffsetToBytePos(s string, line, offset int) int { |
| 527 | pos := 0 |
| 528 | currentLine := 1 |
| 529 | for pos < len(s) && currentLine < line { |
| 530 | if s[pos] == '\n' { |
| 531 | currentLine++ |
| 532 | } |
| 533 | pos++ |
| 534 | } |
| 535 | for i := 0; i < offset && pos < len(s); i++ { |
| 536 | _, size := utf8.DecodeRuneInString(s[pos:]) |
| 537 | pos += size |
| 538 | } |
| 539 | if pos > len(s) { |
| 540 | return len(s) |
| 541 | } |
| 542 | return pos |
| 543 | } |
| 544 | |
| 545 | // currentStatement returns the statement containing byte position pos within |
| 546 | // statement, along with pos translated to an offset within that statement. It |