-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow.go
155 lines (131 loc) · 3.17 KB
/
row.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
package main
import (
"strings"
)
type Row struct {
src []byte
}
func ConstructRow(s string) Row {
return Row{
src: []byte(strings.ReplaceAll(s, "\t", " ")),
}
}
// SplitAt a given rendered index into a row. Creates two new rows, original unchanged.
func (r Row) SplitAt(i uint) (*Row, *Row) {
a := Row{src: make([]byte, i)}
b := Row{src: make([]byte, uint(len(r.src))-i)}
copy(a.src, r.src[:i])
copy(b.src, r.src[i:])
return &a, &b
}
func (r Row) Export() []byte {
return append(r.src)
}
// RenderWithin from constraints of, starting from offset index, and being no larger than max.
func (r Row) RenderWithin(offset, max uint) string {
l := r.Render()
if offset > uint(len(l)) {
return ""
}
if uint(len(l)) > max+offset {
return l[offset : offset+max]
} else {
return l[offset:]
}
}
func (r Row) Render() string {
srcStr := string(r.src)
return strings.ReplaceAll(srcStr, "\t", " ")
}
func (r *Row) getSrcIndex(renderI uint) int {
j := uint(0)
for i := 0; i < len(r.src); i++ {
if j >= renderI {
return i
}
if r.src[i] == '\t' {
j += 4
} else {
j++
}
}
// Accomodate adding to end of line.
if j >= renderI {
return len(r.src)
}
return 0
}
func (r *Row) RemoveCharAt(renderI uint) {
j := r.getSrcIndex(renderI)
if j == 0 {
// TODO: add current row to row above
return
} else if j-1 >= len(r.src) {
// Delete last character in row, no characters to append
r.src = r.src[:j-1]
} else {
r.src = append(r.src[:j-1], r.src[j:]...)
}
}
func (r *Row) AddCharAt(renderI uint, b byte) {
r.AddCharsAt(renderI, string(b))
}
func (r *Row) AddCharsAt(renderI uint, s string) {
j := r.getSrcIndex(renderI)
x := r.src[:j]
y := r.src[j:]
z := strings.Join([]string{string(x), string(y)}, s)
r.src = []byte(z)
}
func (r *Row) GetCharAt(renderI uint) byte {
return r.src[r.getSrcIndex(renderI)]
}
func (r *Row) SetCharAt(renderI uint, b byte) {
j := r.getSrcIndex(renderI)
r.src[j] = b
}
// RenderLen returns the length of the rendered row.
func (r Row) RenderLen() uint {
return uint(len(r.Render()))
}
// GetNextWordFrom the current render index. Returns the index of the space in front of the next word. parameter nextWordRight
// Determined if next word left (false), or right (true).
func (r Row) GetNextWordFrom(renderI uint, nextWordRight bool) uint {
if len(r.src) == 0 {
return 0
}
// TODO: [BUG] Indexes src index not render index. Therefore we need to address possibility
// r.src[i] == '/t' and thus would shift by more than one.
j := r.getSrcIndex(renderI)
if nextWordRight {
for i := j + 1; i < len(r.src); i++ {
if r.src[i] == ' ' {
return uint(i)
}
}
return uint(len(r.src) - 1)
} else {
for i := j - 1; i >= 0; i-- {
if r.src[i] == ' ' {
return uint(i)
}
}
return 0
}
}
// RenderIndexOf string within row starting `from` . Returns -1 if not found, or render index from
// start of row (not from `from` index).
func (r *Row) RenderIndexOf(s string, from int) int {
if from >= len(r.Render()) {
return -1
}
y := strings.Index(r.Render()[from:], s)
if y == -1 {
return -1
}
return from + y
}
// Append Row ro, into Row.
func (r *Row) Append(ro *Row) {
r.src = append(r.src, ro.src...)
}