-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
165 lines (144 loc) · 3.21 KB
/
line.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package edit
import (
"unicode/utf8"
)
type ILine interface {
Len() int
InsertRune(r rune, c int) ILine
DeleteAt(c int) Line
MergeWith(l2 ILine) ILine
String() string
Iter(c int) LineIter
}
type LineIter interface {
Next() rune
HasNext() bool
}
type StyledLineIter interface {
Next() (rune, Style)
HasNext() bool
}
type constStyleLineIter struct {
LineIter
style Style
}
func NewConstStyleLineIter(iter LineIter, style Style) StyledLineIter {
return &constStyleLineIter{
LineIter: iter,
style: style,
}
}
var _ StyledLineIter = (*constStyleLineIter)(nil)
func (i *constStyleLineIter) Next() (rune, Style) {
return i.LineIter.Next(), i.style
}
type Line struct {
Runes []rune
Meta interface{}
}
type lineIter struct {
c int
runes []rune
}
func (i *lineIter) HasNext() bool {
return len(i.runes) > i.c
}
func (i *lineIter) Next() rune {
r := i.runes[i.c]
i.c++
return r
}
func NewLineFromString(s string, meta interface{}) Line {
runes := make([]rune, utf8.RuneCountInString(s))
i := 0
for _, r := range s {
runes[i] = r
i++
}
return Line{Runes: runes, Meta: meta}
}
func (l Line) Len() int {
return len(l.Runes)
}
func (l Line) SplitAt(c int) (Line, Line) {
switch {
case c <= 0:
return Line{Meta: l.Meta}, l
case c >= l.Len():
return l, Line{Meta: l.Meta}
default:
left := Line{Runes: append([]rune(nil), l.Runes[:c]...), Meta: l.Meta}
right := Line{Runes: append([]rune(nil), l.Runes[c:]...), Meta: l.Meta}
return left, right
}
}
func (l Line) InsertRune(r rune, c int) Line {
runes := l.Runes
switch {
case c < 0 || c > len(runes):
return l
case c == len(runes):
return Line{Runes: append(runes, r), Meta: l.Meta}
case cap(runes) > len(runes):
tail := runes[c:]
runes = runes[:len(runes)+1]
copy(runes[c+1:], tail)
runes[c] = r
return Line{Runes: runes, Meta: l.Meta}
default:
newRunes := make([]rune, len(runes)+1, len(runes)+10)
copy(newRunes[:c], runes[:c])
newRunes[c] = r
copy(newRunes[c+1:], runes[c:])
return Line{Runes: newRunes, Meta: l.Meta}
}
}
// s must not contain a '\n'
func (l Line) InsertString(s string, c int) Line {
runes := l.Runes
toInsert := []rune(s)
rsz := len(runes)
isz := len(toInsert)
switch {
case c < 0 || c > rsz:
return l
case c == rsz:
return Line{Runes: append(runes, toInsert...), Meta: l.Meta}
case cap(runes) >= rsz+isz:
tail := runes[c:]
runes = runes[:rsz+isz]
copy(runes[c+isz:], tail)
copy(runes[c:c+isz], toInsert)
return Line{Runes: runes, Meta: l.Meta}
default:
newRunes := make([]rune, rsz+isz, rsz+isz+10)
copy(newRunes[:c], runes[:c])
copy(newRunes[c:c+isz], toInsert)
copy(newRunes[c+isz:], runes[c:])
return Line{Runes: newRunes, Meta: l.Meta}
}
}
func (l Line) DeleteAt(c int) Line {
runes := l.Runes
switch {
case c < 0 || c >= len(runes):
return l
case c == len(runes)-1:
return Line{Runes: runes[:c], Meta: l.Meta}
default:
copy(runes[c:], runes[c+1:])
return Line{Runes: runes[:len(runes)-1], Meta: l.Meta}
}
}
func (l Line) MergeWith(l2 Line) Line {
return Line{Runes: append(l.Runes, l2.Runes...), Meta: l.Meta}
}
func (l Line) String() string {
return string(l.Runes)
}
func (l Line) Iter(c int) LineIter {
return &lineIter{
c: c,
runes: l.Runes,
}
}