-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.go
69 lines (61 loc) · 1.39 KB
/
position.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package peg
import (
"fmt"
"strings"
"unicode/utf8"
)
// Position is a record of byte offset and line-column numbers
// counting from zero.
type Position struct {
Offest int
Line int
Column int
}
func (pos *Position) String() string {
return fmt.Sprintf("%d:%d+%d", pos.Line+1, pos.Column+1, pos.Offest)
}
// Simple utf-8 string line column calculator.
type positionCalculator struct {
text string
cached int // cached to where
lnends []int // found "\r"|"\n"|"\r\n" line endings
}
func (calc *positionCalculator) calculate(offset int) Position {
ln, lnstart := calc.search(offset)
col := utf8.RuneCountInString(calc.text[lnstart:offset])
return Position{
Offest: offset,
Line: ln,
Column: col,
}
}
func (calc *positionCalculator) search(offset int) (ln, lnstart int) {
calc.caching(offset)
if len(calc.lnends) == 0 {
return 0, 0
}
i, j := 0, len(calc.lnends)
for i < j {
m := i + (j-i)/2
if offset > calc.lnends[m] {
i = m + 1
} else if offset < calc.lnends[m] {
j = m
} else {
return m + 1, offset
}
}
return i, calc.lnends[i-1]
}
func (calc *positionCalculator) caching(to int) {
for ; calc.cached < to; calc.cached++ {
switch calc.text[calc.cached] {
case '\n':
calc.lnends = append(calc.lnends, calc.cached+1)
case '\r':
if !strings.HasPrefix(calc.text[calc.cached+1:], "\n") {
calc.lnends = append(calc.lnends, calc.cached+1)
}
}
}
}