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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use std::{iter::FusedIterator, num::NonZeroUsize};
use super::lines::FastEOL;
#[derive(Debug, PartialEq, Eq)]
pub struct EolIndexes(pub Vec<usize>);
impl Default for EolIndexes {
fn default() -> Self {
Self(vec![0])
}
}
impl Clone for EolIndexes {
fn clone(&self) -> Self {
Self(self.0.clone())
}
// The derived impl does not add this, and instead creates a new Vec instead of reusing the
// allocation.
fn clone_from(&mut self, source: &Self) {
self.0.clone_from(&source.0);
}
}
// Mainly used to remove duplicate code in tests.
impl<S: AsRef<[usize]>> PartialEq<S> for EolIndexes {
fn eq(&self, other: &S) -> bool {
self.0 == other.as_ref()
}
}
impl EolIndexes {
#[inline]
pub fn new(s: &str) -> Self {
let mut e = Self(vec![]);
e.repopulate(s);
e
}
/// Repopulates the EOL indexes using the existing buffer
///
/// Other than using the existing buffer, this is the equivalent to calling [`Self::new`].
#[inline]
pub fn repopulate(&mut self, s: &str) {
let iter = FastEOL::new(s);
self.0.clear();
self.0.push(0);
self.0.extend(iter);
}
/// The index to the first byte in the row.
///
/// Returns None if the nth row does not exist.
#[inline(always)]
pub fn row_start(&self, row: usize) -> Option<usize> {
// we increment by one if it is not zero since the index points to a break line,
// and the first row should start at zero.
self.0.get(row).map(|rs| rs + (row != 0) as usize)
}
/// Inserts the provided indexes at the provided position.
///
/// Returns a range to get a slice of the inserted indexes.
#[inline]
pub fn insert_indexes<I: Iterator<Item = usize>>(
&mut self,
at: usize,
indexes: I,
) -> std::ops::Range<usize> {
// A slightly more efficient way to insert multiple values in a Vec.
// Can be thought of as inserting using Vec::splice with optimal cases.
let old_len = self.row_count().get();
self.0.extend(indexes);
let new_len = self.row_count().get();
self.0[at..].rotate_right(new_len - old_len);
at..at + (new_len - old_len)
}
/// Insert the provided index at the position.
pub fn insert_index(&mut self, at: usize, index: usize) {
self.0.insert(at, index);
}
/// Removes the indexes between start and end, not including start, but including end.
///
/// Does nothing if start + 1 > end.
#[inline]
pub fn remove_indexes(&mut self, start: usize, end: usize) {
if start + 1 > end {
return;
}
self.0.drain(start + 1..=end);
}
/// Replace the indexes excluding start and including end.
///
/// Internally is similar to [`Vec::splice`], but with ideal cases and some other optimizations
/// since we are dealing with integers.
///
/// In case use in unsafe code, the uninitialized portion may contain arbitrary values, as the
/// uninitialized section is used as scratch memory. This ofcourse does not concern any safe
/// code.
///
/// # Panics
///
/// Panics if start > end or end > row_count.
#[inline]
pub fn replace_indexes<I>(
&mut self,
start: usize,
end: usize,
mut replacement: I,
) -> std::ops::Range<usize>
where
I: Iterator<Item = usize> + FusedIterator,
{
assert!(start <= end);
assert!(end <= self.row_count().get());
// replace as many the existing values in the range as possible
let replacing_len = end - start;
let i = self.0[start + 1..end + 1]
.iter_mut()
.zip(replacement.by_ref())
.map(|(old, new)| *old = new)
.count();
// calculate the slice start bound that will be rotated
let rotate_start = if i < replacing_len {
end - (replacing_len - i) + 1
} else {
end + 1
};
let cur_len = self.row_count().get();
// add any remaining value to the end
// these will be rotated to their correct position below
// we do this to avoid shifting the values multiple times
// with this we end up shifting only once
self.0.extend(replacement);
let insert_count = self.row_count().get() - cur_len;
// no values were appended to the end, meaning we either have fully filled the replacing
// range, or we have values we need to remove
if insert_count == 0 {
// i is always <= replacing_len
self.0[start + 1 + i..].rotate_left(replacing_len - i);
let new_len = self.row_count().get() - (replacing_len - i);
// the set len below should never grow the vec
// debug_assert is probably better but better be safe than sorry
assert!(new_len <= self.0.len());
// SAFETY: safety requirements of set_len require that the range is initialized which is already
// done. This branch should never grow the vec, the assertion above checks that
//
// this is slightly faster than truncating as no checks or drops need to be performed.
// instead all is dealt with when the vec is dropped.
unsafe {
self.0.set_len(new_len);
}
} else {
self.0[rotate_start..].rotate_right(insert_count);
}
start + 1..start + 1 + insert_count
}
/// Add an offset to all rows after the provided row number excluding itself.
///
/// If the row > row_count the function returns early.
#[inline(always)]
pub(crate) fn add_offsets(&mut self, row: usize, by: usize) {
if row >= self.row_count().get() {
return;
}
self.0[row + 1..].iter_mut().for_each(|bi| *bi += by);
}
/// Sub an offset to all rows after the provided row number excluding itself.
///
/// If the row > row_count the function returns early.
#[inline(always)]
pub(crate) fn sub_offsets(&mut self, row: usize, by: usize) {
if row >= self.row_count().get() {
return;
}
self.0[row + 1..].iter_mut().for_each(|bi| *bi -= by);
}
/// Returns true if the provided row index is for the last row.
///
/// # Panics
///
/// When the buffer contains less than 1 element.
#[inline(always)]
pub fn is_last_row(&self, row: usize) -> bool {
let len = self.row_count();
len.get() - 1 == row
}
/// Get the number of rows present.
///
/// # Panics
///
/// When the buffer contains less than 1 element.
#[inline(always)]
pub fn row_count(&self) -> NonZeroUsize {
let len = self.0.len();
let Some(len) = NonZeroUsize::new(len) else {
no_row();
};
len
}
/// Get the first byte index of the last row.
///
/// # Panics
///
/// When the buffer contains less than 1 element.
#[inline(always)]
pub fn last_row_start(&self) -> usize {
self.row_start(self.row_count().get() - 1).unwrap()
}
}
#[cold]
#[inline(never)]
#[track_caller]
fn no_row() -> ! {
panic!("the row count should never be less than one")
}
#[cfg(test)]
mod tests {
use crate::core::eol_indexes::EolIndexes;
const S: &str = "ads\nasdas\n\n\nasdad\n\nasdasd\nasd\na\n";
#[test]
fn new() {
let br = EolIndexes::new(S);
assert_eq!(br.0, [0, 3, 9, 10, 11, 17, 18, 25, 29, 31]);
}
#[test]
fn repopulate() {
let mut br = EolIndexes::new("Hello\n\n\n\nBye");
br.repopulate(S);
assert_eq!(EolIndexes::new(S), br);
}
#[test]
fn row_start() {
let br = EolIndexes::new(S);
assert_eq!(br.row_start(0), Some(0));
assert_eq!(br.row_start(1), Some(4));
assert_eq!(br.row_start(2), Some(10));
assert_eq!(br.row_start(3), Some(11));
assert_eq!(br.row_start(4), Some(12));
assert_eq!(br.row_start(5), Some(18));
assert_eq!(br.row_start(6), Some(19));
assert_eq!(br.row_start(7), Some(26));
assert_eq!(br.row_start(8), Some(30));
assert_eq!(br.row_start(9), Some(32));
assert_eq!(br.row_start(10), None);
}
#[test]
fn remove_indexes_all() {
let mut br = EolIndexes::new(S);
br.remove_indexes(0, 9);
assert_eq!(br, [0]);
}
#[test]
fn remove_indexes_from_middle() {
let mut br = EolIndexes::new(S);
br.remove_indexes(1, 9);
assert_eq!(br, [0, 3]);
let mut br = EolIndexes::new(S);
br.remove_indexes(3, 5);
assert_eq!(br, [0, 3, 9, 10, 18, 25, 29, 31]);
let mut br = EolIndexes::new(S);
br.remove_indexes(6, 7);
assert_eq!(br, [0, 3, 9, 10, 11, 17, 18, 29, 31]);
}
#[test]
fn remove_indexes_same_row() {
let mut br = EolIndexes::new(S);
br.remove_indexes(0, 0);
assert_eq!(br, [0, 3, 9, 10, 11, 17, 18, 25, 29, 31]);
let mut br = EolIndexes::new(S);
br.remove_indexes(5, 5);
assert_eq!(br, [0, 3, 9, 10, 11, 17, 18, 25, 29, 31]);
let mut br = EolIndexes::new(S);
br.remove_indexes(9, 9);
assert_eq!(br, [0, 3, 9, 10, 11, 17, 18, 25, 29, 31]);
}
#[test]
fn remove_indexes_last_row() {
let mut br = EolIndexes::new(S);
br.remove_indexes(4, 9);
assert_eq!(br, [0, 3, 9, 10, 11]);
let mut br = EolIndexes::new(S);
br.remove_indexes(0, 9);
assert_eq!(br, [0]);
}
#[test]
fn add_offsets() {
let mut br = EolIndexes::new(S);
br.add_offsets(3, 10);
assert_eq!(br.0, [0, 3, 9, 10, 21, 27, 28, 35, 39, 41]);
}
#[test]
fn sub_offsets() {
let mut br = EolIndexes::new(S);
br.sub_offsets(0, 2);
assert_eq!(br.0, [0, 1, 7, 8, 9, 15, 16, 23, 27, 29]);
}
#[test]
fn is_last_row() {
let br = EolIndexes::new(S);
assert!(!br.is_last_row(0));
assert!(!br.is_last_row(1));
assert!(!br.is_last_row(2));
assert!(br.is_last_row(9));
}
#[test]
#[should_panic]
fn is_last_row_oob() {
let br = EolIndexes::new(S);
assert!(br.is_last_row(10));
}
}