jumprope 1.1.2

Simple, fast rope (fancy string) library built on top of Skiplists
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use crate::fast_str_tools::*;
#[cfg(feature = "line_conversion")]
use crate::utils::count_lines;
use crate::utils::str_chars_to_bytes_rev;

#[derive(Debug, Clone, Eq)]
pub struct GapBuffer<const LEN: usize> {
    data: [u8; LEN],

    pub(crate) gap_start_bytes: u16,
    pub(crate) gap_start_chars: u16,

    /// The number of UTF16 surrogate pairs before the gap.
    #[cfg(feature = "wchar_conversion")]
    pub(crate) gap_start_surrogate_pairs: u16,

    /// The number of lines before the gap
    #[cfg(feature = "line_conversion")]
    pub(crate) gap_start_lines: u16,

    pub(crate) gap_len: u16,
    all_ascii: bool,
}

#[inline]
unsafe fn slice_to_str(arr: &[u8]) -> &str {
    if cfg!(debug_assertions) {
        std::str::from_utf8(arr).unwrap()
    } else {
        std::str::from_utf8_unchecked(arr)
    }
}

impl<const LEN: usize> GapBuffer<LEN> {
    pub fn new() -> Self {
        Self {
            data: [0; LEN],
            gap_start_bytes: 0,
            gap_start_chars: 0,
            #[cfg(feature = "wchar_conversion")]
            gap_start_surrogate_pairs: 0,
            #[cfg(feature = "line_conversion")]
            gap_start_lines: 0,
            gap_len: LEN as u16,
            all_ascii: true,
        }
    }

    pub fn new_from_str(s: &str) -> Self {
        let mut val = Self::new();
        val.try_insert(0, s).unwrap();
        val
    }

    // #[allow(unused)]
    // pub fn len_space(&self) -> usize {
    //     self.gap_len as usize
    // }

    /// In bytes.
    pub fn len_bytes(&self) -> usize {
        LEN - self.gap_len as usize
    }

    // #[allow(unused)]
    // pub fn char_len(&self) -> usize {
    //     count_chars(self.start_as_str()) + count_chars(self.end_as_str())
    // }

    pub fn is_empty(&self) -> bool {
        self.gap_len as usize == LEN
    }

    fn count_internal_chars(&self, s: &str) -> usize {
        if self.all_ascii { s.len() } else { count_chars(s) }
    }

    #[cfg(feature = "wchar_conversion")]
    fn int_count_surrogate_pairs(&self, s: &str) -> usize {
        if self.all_ascii { 0 } else { count_utf16_surrogates(s) }
    }

    fn int_str_get_byte_offset(&self, s: &str, char_pos: usize) -> usize {
        if self.all_ascii { char_pos } else { char_to_byte_idx(s, char_pos) }
    }
    fn int_chars_to_bytes_backwards(&self, s: &str, char_len: usize) -> usize {
        if self.all_ascii { char_len } else { str_chars_to_bytes_rev(s, char_len) }
    }

    pub fn move_gap(&mut self, new_start_bytes: usize) {
        let current_start = self.gap_start_bytes as usize;

        if new_start_bytes != current_start {
            let len = self.gap_len as usize;
            debug_assert!(new_start_bytes <= LEN-len);

            #[allow(clippy::comparison_chain)]
            if new_start_bytes < current_start {
                // move characters to the right (gap to the left)
                let moved_chars = new_start_bytes..current_start;
                let s = unsafe { slice_to_str(&self.data[moved_chars.clone()]) };
                let char_len = self.count_internal_chars(s);

                #[cfg(feature = "wchar_conversion")] {
                    let surrogate_pairs = self.int_count_surrogate_pairs(s);
                    self.gap_start_surrogate_pairs -= surrogate_pairs as u16;
                }

                #[cfg(feature = "line_conversion")] {
                    self.gap_start_lines -= count_lines(s) as u16;
                }

                self.gap_start_chars -= char_len as u16;

                self.data.copy_within(moved_chars, new_start_bytes + len);
            } else if current_start < new_start_bytes {
                // Move characters to the left (gap to the right)
                let moved_chars = current_start+len..new_start_bytes +len;
                let s = unsafe { slice_to_str(&self.data[moved_chars.clone()]) };
                let char_len = self.count_internal_chars(s);

                #[cfg(feature = "wchar_conversion")] {
                    let surrogate_pairs = self.int_count_surrogate_pairs(s);
                    self.gap_start_surrogate_pairs += surrogate_pairs as u16;
                }

                #[cfg(feature = "line_conversion")] {
                    self.gap_start_lines += count_lines(s) as u16;
                }

                self.gap_start_chars += char_len as u16;

                self.data.copy_within(moved_chars, current_start);
            }

            if cfg!(debug_assertions) {
                // This is unnecessary but tidy, and makes debugging easier.
                self.data[new_start_bytes..new_start_bytes +len].fill(0);
            }

            self.gap_start_bytes = new_start_bytes as u16;
        }
    }

    /// Panics if there's no room. This inserts at the start of the gap (and moves the gap after the
    /// inserted text).
    pub fn insert_in_gap(&mut self, s: &str) {
        let len = s.len();
        let char_len = count_chars(s);
        assert!(len <= self.gap_len as usize);

        let start = self.gap_start_bytes as usize;
        self.data[start..start+len].copy_from_slice(s.as_bytes());
        self.gap_start_bytes += len as u16;
        self.gap_start_chars += char_len as u16;
        self.gap_len -= len as u16;

        #[cfg(feature = "wchar_conversion")]
        if len != char_len {
            self.gap_start_surrogate_pairs += count_utf16_surrogates(s) as u16;
        }

        #[cfg(feature = "line_conversion")] {
            self.gap_start_lines += count_lines(s) as u16;
        }

        if len != char_len { self.all_ascii = false; }
    }

    pub fn try_insert(&mut self, byte_pos: usize, s: &str) -> Result<(), ()> {
        let len = s.len();
        if len > self.gap_len as usize {
            // No space in this node!
            Result::Err(())
        } else {
            self.move_gap(byte_pos);
            self.insert_in_gap(s);
            Result::Ok(())
        }
    }

    /// Remove chars after the gap (ie, at gap .. gap+del_len)
    pub fn remove_after_gap(&mut self, del_bytes: usize) {
        if cfg!(debug_assertions) {
            // Zero out the deleted bytes in debug mode.
            self.data[
                (self.gap_start_bytes +self.gap_len) as usize..(self.gap_start_bytes +self.gap_len) as usize + del_bytes
                ].fill(0);
        }
        self.gap_len += del_bytes as u16;
    }

    // Returns the number of items actually removed.
    #[allow(unused)]
    pub fn remove(&mut self, pos: usize, del_len: usize) -> usize {
        let len = self.len_bytes();

        if pos >= len { return 0; }
        let del_len = del_len.min(len - pos);

        self.move_gap(pos);

        self.remove_after_gap(del_len);
        del_len
    }

    /// Returns the number of bytes removed.
    pub fn remove_chars(&mut self, pos: usize, mut del_len: usize) -> usize {
        // This function is longer than it needs to be; but having it be a bit longer makes the
        // code faster. I think the trade-off is worth it.
        // self.move_gap(self.count_bytes(pos));
        // let removed_bytes = str_get_byte_offset(s.end_as_str(), del_len);
        // self.remove_at_gap(removed_bytes);
        // removed_bytes

        if del_len == 0 { return 0; }
        debug_assert!(del_len <= self.len_bytes() - pos);
        let mut rm_start_bytes = 0;

        let gap_chars = self.gap_start_chars as usize;
        #[cfg(any(feature = "wchar_conversion", feature = "line_conversion"))]
        let gap_start_bytes = self.gap_start_bytes as usize;
        if pos <= gap_chars && pos+del_len >= gap_chars {
            if pos < gap_chars {
                // Delete the bit from pos..gap.
                // TODO: It would be better to count backwards here.
                // let pos_bytes = str_get_byte_offset(self.start_as_str(), pos) as u16;
                // rm_start_bytes = self.gap_start_bytes - pos_bytes;
                rm_start_bytes = self.int_chars_to_bytes_backwards(self.start_as_str(), gap_chars - pos);

                #[cfg(feature = "wchar_conversion")]
                if !self.all_ascii {
                    self.gap_start_surrogate_pairs -= unsafe {
                        count_utf16_surrogates_in_bytes(&self.data[gap_start_bytes - rm_start_bytes..gap_start_bytes]) as u16
                    }
                }

                #[cfg(feature = "line_conversion")] {
                    unsafe {
                        let s = std::str::from_utf8_unchecked(&self.data[gap_start_bytes - rm_start_bytes..gap_start_bytes]);
                        self.gap_start_lines -= count_lines(s) as u16;
                    }
                }

                del_len -= self.gap_start_chars as usize - pos;
                let rm_start_bytes = rm_start_bytes as u16;
                self.gap_len += rm_start_bytes;
                self.gap_start_chars = pos as u16;
                self.gap_start_bytes -= rm_start_bytes;
                // self.gap_start_bytes = pos_bytes;
                if del_len == 0 { return rm_start_bytes as usize; }
            }

            debug_assert!(del_len > 0);
            debug_assert!(pos >= self.gap_start_chars as usize);
        } else {
            // This is equivalent to self.count_bytes() (below), but for some reason manually
            // inlining it here results in both faster and smaller executables.
            let gap_bytes = if pos < gap_chars {
                self.int_str_get_byte_offset(self.start_as_str(), pos)
            } else {
                self.int_str_get_byte_offset(self.end_as_str(), pos - gap_chars) + self.gap_start_bytes as usize
            };
            self.move_gap(gap_bytes);
        }

        // At this point the gap is guaranteed to be directly after pos.
        let rm_end_bytes = self.int_str_get_byte_offset(self.end_as_str(), del_len);
        self.remove_after_gap(rm_end_bytes);
        rm_start_bytes as usize + rm_end_bytes
    }

    pub fn start_as_str(&self) -> &str {
        unsafe {
            slice_to_str(&self.data[0..self.gap_start_bytes as usize])
        }
    }
    pub fn end_as_str(&self) -> &str {
        unsafe {
            slice_to_str(&self.data[(self.gap_start_bytes +self.gap_len) as usize..LEN])
        }
    }

    pub fn count_bytes(&self, char_pos: usize) -> usize {
        if self.all_ascii { return char_pos; }

        let gap_chars = self.gap_start_chars as usize;
        let gap_bytes = self.gap_start_bytes as usize;
        // Clippy complains about this but if I swap to a match expression, performance drops by 1%.
        #[allow(clippy::comparison_chain)]
        if char_pos == gap_chars {
            gap_bytes
        } else if char_pos < gap_chars {
            self.int_str_get_byte_offset(self.start_as_str(), char_pos)
        } else { // char_pos > start_char_len.
            gap_bytes + self.int_str_get_byte_offset(self.end_as_str(), char_pos - gap_chars)
        }
    }

    /// Calculate & return the number of surrogate pairs in `[0..char_pos]`
    #[cfg(feature = "wchar_conversion")]
    pub(crate) fn count_chars_in_wchars(&self, wchar_pos: usize) -> usize {
        if self.all_ascii { wchar_pos }
        else {
            let gap_chars = self.gap_start_chars as usize;
            let gap_pairs = self.gap_start_surrogate_pairs as usize;
            let gap_wchars = gap_chars + gap_pairs;

            if wchar_pos == gap_wchars {
                gap_chars
            } else if wchar_pos < gap_wchars {
                // In start.
                if self.gap_start_surrogate_pairs == 0 { wchar_pos }
                else {
                    utf16_code_unit_to_char_idx(self.start_as_str(), wchar_pos)
                }
            } else {
                // In end.
                gap_chars + utf16_code_unit_to_char_idx(self.end_as_str(), wchar_pos - gap_wchars)
            }
        }
    }

    #[cfg(feature = "wchar_conversion")]
    pub(crate) fn count_surrogate_pairs(&self, char_pos: usize) -> usize {
        if self.all_ascii {
            0
        } else {
            let gap_chars = self.gap_start_chars as usize;
            if char_pos == gap_chars {
                self.gap_start_surrogate_pairs as usize
            } else if char_pos < gap_chars {
                if self.gap_start_surrogate_pairs == 0 { 0 }
                else {
                    let bytes = self.int_str_get_byte_offset(self.start_as_str(), char_pos);
                    unsafe { count_utf16_surrogates_in_bytes(&self.data[..bytes]) }
                }
            } else {
                // Right stuff.
                let bytes = self.int_str_get_byte_offset(self.end_as_str(), char_pos - gap_chars);
                let base = (self.gap_start_bytes + self.gap_len) as usize;
                let slice = &self.data[base..base + bytes];
                unsafe { self.gap_start_surrogate_pairs as usize + count_utf16_surrogates_in_bytes(slice) }
            }
        }
    }

    /// Take the remaining contents in the gap buffer. Mark them as deleted, but return them.
    /// This will leave those items non-zero, but that doesn't matter.
    pub fn take_rest(&mut self) -> &str {
        let last_idx = (self.gap_start_bytes + self.gap_len) as usize;
        self.gap_len = LEN as u16 - self.gap_start_bytes;
        unsafe { slice_to_str(&self.data[last_idx..LEN]) }
    }

    pub(crate) fn check(&self) {
        let char_len = count_chars(self.start_as_str());
        assert_eq!(char_len, self.gap_start_chars as usize);

        #[cfg(feature = "wchar_conversion")] {
            let pairs = count_utf16_surrogates(self.start_as_str());
            assert_eq!(pairs, self.gap_start_surrogate_pairs as usize);
        }

        #[cfg(feature = "line_conversion")] {
            let lines = count_lines(self.start_as_str());
            assert_eq!(lines, self.gap_start_lines as usize);
        }

        if self.all_ascii {
            assert_eq!(self.gap_start_bytes, self.gap_start_chars);
            #[cfg(feature = "wchar_conversion")] {
                assert_eq!(self.gap_start_surrogate_pairs, 0);
            }
        }
    }
}

impl<const LEN: usize> ToString for GapBuffer<LEN> {
    fn to_string(&self) -> String {
        let mut result = String::with_capacity(self.len_bytes());
        result.push_str(self.start_as_str());
        result.push_str(self.end_as_str());
        result
    }
}

impl<const LEN: usize> PartialEq for GapBuffer<LEN> {
    // Eq is interesting because we need to ignore where the gap is.
    fn eq(&self, other: &Self) -> bool {
        if self.gap_len != other.gap_len { return false; }
        // There's 3 sections to check:
        // - Before our gap
        // - The inter-gap part
        // - The last, common part.
        let (a, b) = if self.gap_start_bytes < other.gap_start_bytes {
            (self, other)
        } else {
            (other, self)
        };
        // a has its gap first (or the gaps are at the same time).
        let a_start = a.gap_start_bytes as usize;
        let b_start = b.gap_start_bytes as usize;
        let gap_len = a.gap_len as usize;

        // Section before the gaps
        if a.data[0..a_start] != b.data[0..a_start] { return false; }

        // Gappy bit
        if a.data[a_start+gap_len..b_start+gap_len] != b.data[a_start..b_start] { return false; }

        // Last bit
        let end_idx = b_start + gap_len;
        a.data[end_idx..LEN] == b.data[end_idx..LEN]
    }
}

#[cfg(test)]
mod test {
    use crate::gapbuffer::GapBuffer;

    fn check_eq<const LEN: usize>(b: &GapBuffer<LEN>, s: &str) {
        assert_eq!(b.to_string(), s);
        assert_eq!(b.len_bytes(), s.len());
        assert_eq!(s.is_empty(), b.is_empty());
    }

    #[test]
    fn smoke_test() {
        let mut b = GapBuffer::<5>::new();

        b.try_insert(0, "hi").unwrap();
        b.try_insert(0, "x").unwrap(); // 'xhi'
        // b.move_gap(2);
        b.try_insert(2, "x").unwrap(); // 'xhxi'
        check_eq(&b, "xhxi");
    }

    #[test]
    fn remove() {
        let mut b = GapBuffer::<5>::new_from_str("hi");
        assert_eq!(b.remove(2, 2), 0);
        check_eq(&b, "hi");

        assert_eq!(b.remove(0, 1), 1);
        check_eq(&b, "i");

        assert_eq!(b.remove(0, 1000), 1);
        check_eq(&b, "");
    }

    #[test]
    fn eq() {
        let hi = GapBuffer::<5>::new_from_str("hi");
        let yo = GapBuffer::<5>::new_from_str("yo");
        assert_ne!(hi, yo);
        assert_eq!(hi, hi);

        let mut hi2 = GapBuffer::<5>::new_from_str("hi");
        hi2.move_gap(1);
        assert_eq!(hi, hi2);

        hi2.move_gap(0);
        assert_eq!(hi, hi2);
    }
}