-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring_iterator.go
89 lines (77 loc) · 2 KB
/
string_iterator.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
package etxt
import "unicode/utf8"
// Definitions of private types used to iterate strings and glyphs
// on Traverse* operations. Sometimes we iterate lines in reverse,
// so there's a bit of trickiness here and there.
type ltrStringIterator struct{ index int }
func (self *ltrStringIterator) Next(text string) rune {
if self.index < len(text) {
codePoint, runeSize := utf8.DecodeRuneInString(text[self.index:])
self.index += runeSize
return codePoint
} else {
return -1
}
}
func (self *ltrStringIterator) PeekNext(text string) rune {
if self.index < len(text) {
codePoint, _ := utf8.DecodeRuneInString(text[self.index:])
return codePoint
} else {
return -1
}
}
func (self *ltrStringIterator) Unroll(codePoint rune) {
self.index -= utf8.RuneLen(codePoint)
}
func (self *ltrStringIterator) StringLeft(text string) string {
if self.index >= len(text) {
return ""
}
return text[self.index:]
}
type rtlStringIterator struct{ head, tail, index int }
func (self *rtlStringIterator) Init(text string) {
self.tail = 0
self.head = 0
self.LineSlide(text)
}
func (self *rtlStringIterator) LineSlide(text string) {
self.tail = self.head
if self.head >= len(text) {
self.index = self.tail
} else {
if text[self.head] == '\n' {
self.head += 1
} else {
for self.head < len(text) { // find next line break or end of string
codePoint, runeSize := utf8.DecodeRuneInString(text[self.head:])
if codePoint == '\n' {
break
}
self.head += runeSize
}
}
self.index = self.head
}
}
func (self *rtlStringIterator) Next(text string) rune {
if self.index > self.tail {
codePoint, runeSize := utf8.DecodeLastRuneInString(text[:self.index])
self.index -= runeSize
if codePoint == '\n' || self.index <= self.tail {
self.LineSlide(text)
}
return codePoint
} else {
return -1
}
}
func (self *rtlStringIterator) PeekNext(text string) rune {
if self.index > self.tail {
codePoint, _ := utf8.DecodeLastRuneInString(text[:self.index])
return codePoint
} else {
return -1
}
}