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
#![deny(missing_docs)]
#[derive(Clone)]
pub struct TextLine {
text: String,
indices: Vec<usize>,
}
impl TextLine {
pub fn new() -> Self {
Self {
text: String::new(),
indices: Vec::new(),
}
}
fn refresh_indices(&mut self) {
if !self.text.is_ascii() {
let mut index = 0;
for c in self.text.chars() {
index += c.len_utf8();
self.indices.push(index);
}
}
}
pub fn from_string(text: String) -> Self {
let mut result = Self {
text,
indices: Vec::new(),
};
result.refresh_indices();
result
}
pub fn from_str(text: &str) -> Self {
Self::from_string(text.to_string())
}
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
pub fn len(&self) -> usize {
if self.indices.is_empty() {
self.text.len()
} else {
self.indices.len()
}
}
pub fn to_string(&self) -> String {
self.text.clone()
}
pub fn as_str(&self) -> &str {
&self.text
}
pub fn string_index(&self, index: usize) -> usize {
if !self.indices.is_empty() && index > 0 {
self.indices[index - 1]
} else {
index
}
}
pub fn char_at(&self, at: usize) -> char {
self.char_at_checked(at).unwrap()
}
fn char_at_checked(&self, at: usize) -> Option<char> {
self.text[self.string_index(at)..].chars().next()
}
pub fn insert(&mut self, index: usize, c: char) {
self.text.insert(self.string_index(index), c);
self.indices = Vec::new();
self.refresh_indices();
}
pub fn remove(&mut self, index: usize) -> char {
let result = self.text.remove(self.string_index(index));
self.indices = Vec::new();
self.refresh_indices();
result
}
pub fn split(&mut self, index: usize) -> Self {
let mut result = Self {
text: self.text.split_off(self.string_index(index)),
indices: Vec::new(),
};
self.indices = Vec::new();
self.refresh_indices();
result.refresh_indices();
result
}
pub fn join(&mut self, other: Self) {
self.text.extend(other.text.chars());
self.indices = Vec::new();
self.refresh_indices();
}
}