1
1
mirror of https://github.com/wader/fq.git synced 2024-10-04 23:48:00 +03:00
fq/internal/pos/pos.go
Mattias Wadman 970465996c Init
2021-09-12 13:08:42 +02:00

35 lines
529 B
Go

package pos
import (
"fmt"
"strings"
)
func offsetToLineColumn(s string, offset int) (int, int) {
co := 0
line := 1
for {
no := strings.Index(s[co:], "\n")
if no == -1 || co+no >= offset {
return line, offset - co
}
co += no + 1
line++
}
}
type Pos struct {
S string
Line int
Column int
}
func (p Pos) String() string {
return fmt.Sprintf("%d:%d", p.Line, p.Column)
}
func NewFromOffset(s string, offset int) Pos {
l, c := offsetToLineColumn(s, offset)
return Pos{S: s, Line: l, Column: c}
}