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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use super::DocString;
use crate::doc::*;
#[derive(Clone, Debug, PartialEq)]
pub struct CharCursor<S: Schema> {
left_string: DocElement<S>,
right_string: DocElement<S>,
index: usize,
str_len: usize,
}
impl<S: Schema> CharCursor<S> {
#[inline(always)]
pub fn from_docstring(text: &DocString, styles: S::CharsProperties) -> CharCursor<S> {
let mut left_string = text.clone();
let mut right_string = text.clone();
unsafe {
left_string.byte_range_mut().end = right_string.byte_range_mut().start;
}
CharCursor {
left_string: DocElement::DocText(styles.clone(), left_string),
right_string: DocElement::DocText(styles, right_string),
index: 0,
str_len: text.char_len(),
}
}
#[inline(always)]
pub fn from_docstring_end(text: &DocString, styles: S::CharsProperties) -> CharCursor<S> {
let left_string = text.clone();
let mut right_string = text.clone();
unsafe {
right_string.byte_range_mut().start = right_string.byte_range_mut().end;
}
CharCursor {
left_string: DocElement::DocText(styles.clone(), left_string),
right_string: DocElement::DocText(styles, right_string),
index: text.char_len(),
str_len: text.char_len(),
}
}
pub fn update_from_docstring(&mut self, text: &DocString, styles: S::CharsProperties) {
self.left_string = DocElement::DocText(styles.clone(), text.clone());
self.right_string = DocElement::DocText(styles, text.clone());
self.index = text.char_len();
}
pub fn style<'a>(&'a self) -> &'a S::CharsProperties {
if let DocElement::DocText(ref styles, _) = &self.left_string {
styles
} else {
unreachable!();
}
}
pub fn left<'a>(&'a self) -> Option<&'a DocString> {
if let DocElement::DocText(_, ref left_text) = &self.left_string {
if unsafe { left_text.try_byte_range().unwrap().len() == 0 } {
None
} else {
Some(left_text)
}
} else {
unreachable!();
}
}
pub fn right<'a>(&'a self) -> Option<&'a DocString> {
if let DocElement::DocText(_, ref right_text) = &self.right_string {
if unsafe { right_text.try_byte_range().unwrap().len() == 0 } {
None
} else {
Some(right_text)
}
} else {
unreachable!();
}
}
pub fn left_element<'a>(&'a self) -> Option<&'a DocElement<S>> {
if let DocElement::DocText(_, ref left_text) = &self.left_string {
if unsafe { left_text.try_byte_range().unwrap().len() == 0 } {
None
} else {
Some(&self.left_string)
}
} else {
unreachable!();
}
}
pub fn right_element<'a>(&'a self) -> Option<&'a DocElement<S>> {
if let DocElement::DocText(_, ref right_text) = &self.right_string {
if unsafe { right_text.try_byte_range().unwrap().len() == 0 } {
None
} else {
Some(&self.right_string)
}
} else {
unreachable!();
}
}
fn left_text_mut<'a>(&'a mut self) -> &'a mut DocString {
if let DocElement::DocText(_, ref mut left_text) = self.left_string {
left_text
} else {
unreachable!();
}
}
fn right_text_mut<'a>(&'a mut self) -> &'a mut DocString {
if let DocElement::DocText(_, ref mut right_text) = self.right_string {
right_text
} else {
unreachable!();
}
}
pub fn value(&self) -> usize {
self.index
}
pub fn index_from_end(&self) -> usize {
self.str_len - self.index
}
pub fn value_add(&mut self, add: usize) {
self.index += add;
unsafe {
self.right_text_mut().seek_start_forward(add);
self.left_text_mut().byte_range_mut().end =
self.right_text_mut().byte_range_mut().start;
}
}
pub fn value_sub(&mut self, sub: usize) {
self.index -= sub;
unsafe {
self.right_text_mut().seek_start_backward(sub);
self.left_text_mut().byte_range_mut().end =
self.right_text_mut().byte_range_mut().start;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rtf::*;
#[test]
fn basic() {
let mut ds = CharCursor::<RtfSchema>::from_docstring(
&DocString::from_str("Welcome!"),
StyleSet::new(),
);
ds.value_add(6);
assert_eq!(ds.right().unwrap().as_str(), "e!");
}
#[test]
#[should_panic]
fn seek_too_far() {
let mut ds = CharCursor::<RtfSchema>::from_docstring(
&DocString::from_str("Welcome!"),
StyleSet::new(),
);
ds.value_add(11);
}
#[test]
#[should_panic]
fn seek_negative() {
let mut ds = CharCursor::<RtfSchema>::from_docstring(
&DocString::from_str("Welcome!"),
StyleSet::new(),
);
ds.value_add(4);
ds.value_sub(10);
}
#[test]
fn option_ends() {
let mut ds = CharCursor::<RtfSchema>::from_docstring(
&DocString::from_str("Welcome!"),
StyleSet::new(),
);
assert_eq!(ds.left(), None);
assert_eq!(ds.right().is_some(), true);
ds.value_add("Welcome!".len());
assert_eq!(ds.left().is_some(), true);
assert_eq!(ds.right(), None);
ds.value_sub(1);
assert_eq!(ds.left().is_some(), true);
assert_eq!(ds.right().is_some(), true);
}
}