text_buffer/
buffer.rs

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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
#![warn(clippy::all, clippy::pedantic)]
#![expect(clippy::must_use_candidate)]
#![expect(clippy::missing_panics_doc)]
use crate::{
    metric::{BufferMetrics, Metric},
    Position,
};
use get_size2::GetSize;
use std::{
    cmp,
    fmt::{self, Debug, Display},
    ops::{Bound, Deref, Range, RangeBounds},
};
use str_indices::chars;

/// A Gap buffer. This represents the text of a buffer, and allows for
/// efficient insertion and deletion of text.
#[derive(Default, GetSize)]
pub struct Buffer {
    /// The buffer data
    data: Box<[u8]>,
    /// start of the gap. Both `gap_start` and `gap_end` are the same point, but
    /// `gap_start` is never a valid byte index, and `gap_end` is always used
    /// instead.
    gap_start: usize,
    /// The end of the gap in bytes
    gap_end: usize,
    /// The number of characters until the gap
    gap_chars: usize,
    /// The current cursor.
    cursor: GapMetric,
    total: Metric,
    /// A mapping between byte and character positions. Doesn't account for the gap.
    metrics: BufferMetrics,
    new_gap_size: usize,
}

impl Debug for Buffer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let start = self.to_str(..self.gap_start);
        let end = self.to_str(self.gap_end..);
        // repeat _ for the gap length
        let gap = "_".repeat(self.gap_len());
        f.debug_struct("Buffer")
            .field("data", &format!("{start}{gap}{end}"))
            .field("gap_start", &self.gap_start)
            .field("gap_end", &self.gap_end)
            .field("gap_chars", &self.gap_chars)
            .field("cursor", &self.cursor)
            .field("metrics", &self.metrics)
            .field("total_chars", &self.total.chars)
            .field("new_gap_size", &self.new_gap_size)
            .finish()
    }
}

impl Display for Buffer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let (s1, s2) = self.slice(..);
        write!(f, "{s1}")?;
        write!(f, "{s2}")
    }
}

const METRIC_SIZE: usize = crate::metric::MAX_LEAF;
struct MetricBuilder<'a> {
    slice: &'a str,
    start: usize,
    end: usize,
}

impl<'a> MetricBuilder<'a> {
    fn new(slice: &'a str) -> Self {
        Self { slice, start: 0, end: slice.len().min(METRIC_SIZE) }
    }
}

impl Iterator for MetricBuilder<'_> {
    type Item = Metric;

    fn next(&mut self) -> Option<Self::Item> {
        if self.start == self.slice.len() {
            return None;
        }
        let mut end = self.end;
        while !self.slice.is_char_boundary(end) {
            end -= 1;
        }
        let slice = &self.slice[self.start..end];
        self.start = end;
        self.end = cmp::min(self.end + METRIC_SIZE, self.slice.len());
        Some(metrics(slice))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.slice.len() - self.start;
        let extra = usize::from(len % METRIC_SIZE != 0);
        let size = len / METRIC_SIZE;
        (size + extra, None)
    }
}

/// Metric with gap buffer accounted for
#[derive(Debug, Default, Copy, Clone, Eq, GetSize)]
struct GapMetric {
    bytes: usize,
    chars: usize,
}

impl std::ops::Sub for GapMetric {
    type Output = Metric;

    fn sub(self, rhs: Self) -> Self::Output {
        Metric { bytes: self.bytes - rhs.bytes, chars: self.chars - rhs.chars }
    }
}

impl Display for GapMetric {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", Metric { bytes: self.bytes, chars: self.chars })
    }
}

impl PartialEq for GapMetric {
    fn eq(&self, other: &Self) -> bool {
        let eq = self.bytes == other.bytes;
        if eq {
            debug_assert_eq!(self.chars, other.chars);
        } else {
            debug_assert_ne!(self.chars, other.chars);
        }
        eq
    }
}

fn calc_start_gap_size(len: usize) -> usize {
    // The gap can be up to 5% of the total size, but at least 64 bytes
    cmp::max(len / 20, Buffer::GAP_SIZE)
}

impl From<String> for Buffer {
    #[inline]
    fn from(data: String) -> Self {
        // reuse the allocation from the string. This means we *might* have a
        // gap of 0
        let builder = MetricBuilder::new(&data);
        let metrics = BufferMetrics::build(builder);
        let (storage, len) = {
            let len = data.len();
            let mut vec: Vec<u8> = data.into_bytes();
            vec.resize(vec.capacity(), 0);
            debug_assert_eq!(vec.capacity(), vec.len());
            (vec.into_boxed_slice(), len)
        };
        let gap_len = storage.len() - len;
        let total = metrics.len();
        Self {
            data: storage,
            gap_start: len,
            gap_end: len + gap_len,
            gap_chars: total.chars,
            cursor: GapMetric::default(),
            total,
            metrics,
            new_gap_size: calc_start_gap_size(len),
        }
    }
}

impl From<&str> for Buffer {
    #[inline]
    fn from(data: &str) -> Self {
        let new_gap_size = calc_start_gap_size(data.len());
        let storage = {
            let capacity = data.len() + new_gap_size;
            let mut storage = Vec::with_capacity(capacity);
            storage.resize(new_gap_size, 0);
            storage.extend_from_slice(data.as_bytes());
            assert_eq!(storage.len(), capacity);
            storage.into_boxed_slice()
        };
        let builder = MetricBuilder::new(data);
        let metrics = BufferMetrics::build(builder);
        Self {
            data: storage,
            gap_start: 0,
            gap_end: new_gap_size,
            gap_chars: 0,
            cursor: GapMetric { bytes: new_gap_size, chars: 0 },
            total: metrics.len(),
            new_gap_size,
            metrics,
        }
    }
}

impl<T> PartialEq<T> for Buffer
where
    T: Deref<Target = str>,
{
    fn eq(&self, other: &T) -> bool {
        PartialEq::eq(self, Deref::deref(other))
    }
}

impl PartialEq<str> for Buffer {
    fn eq(&self, other: &str) -> bool {
        if self.len_bytes() != other.len() {
            return false;
        }
        self.to_str(..self.gap_start) == &other[..self.gap_start]
            && self.to_str(self.gap_end..) == &other[self.gap_start..]
    }
}

impl Buffer {
    #[cfg(not(test))]
    const GAP_SIZE: usize = 64;
    #[cfg(test)]
    const GAP_SIZE: usize = 5;
    const MAX_GAP: usize = 1024 * 8;

    #[must_use]
    pub fn new() -> Self {
        Self::with_gap(Self::GAP_SIZE)
    }

    #[must_use]
    pub fn with_gap(gap: usize) -> Self {
        Self { new_gap_size: gap, ..Self::default() }
    }

    /// Grow the buffer to accommodate the new slice. Moves the gap to the
    /// cursor position at the same time.
    fn grow(&mut self, slice: &str) {
        // If the string being inserted is large, we want to grow the gap faster
        if slice.len() >= self.new_gap_size {
            let len = slice.len() + self.len_bytes();
            self.new_gap_size =
                cmp::max(len / 20, cmp::min(slice.len().next_power_of_two(), Self::MAX_GAP));
        }
        let new_capacity = {
            let pre_gap = self.gap_start;
            let post_gap = self.data.len() - self.gap_end;
            pre_gap + slice.len() + self.new_gap_size + post_gap
        };
        let mut buffer = Vec::with_capacity(new_capacity);
        let bytes;
        #[expect(clippy::comparison_chain)]
        if self.cursor.chars < self.gap_chars {
            buffer.extend_from_slice(&self.data[..self.cursor.bytes]); // pre cursor
            buffer.extend_from_slice(slice.as_bytes()); // new text
            buffer.resize(buffer.len() + self.new_gap_size, 0); // new gap
            bytes = buffer.len();
            buffer.extend_from_slice(&self.data[self.cursor.bytes..self.gap_start]); // cursor to gap
            buffer.extend_from_slice(&self.data[self.gap_end..]); // post gap
        } else if self.cursor.chars > self.gap_chars {
            buffer.extend_from_slice(&self.data[..self.gap_start]); // pre gap
            buffer.extend_from_slice(&self.data[self.gap_end..self.cursor.bytes]); // gap to cursor
            buffer.extend_from_slice(slice.as_bytes()); // new text
            buffer.resize(buffer.len() + self.new_gap_size, 0); // new gap
            bytes = buffer.len();
            buffer.extend_from_slice(&self.data[self.cursor.bytes..]); // post cursor
        } else {
            // cursor is at gap
            buffer.extend_from_slice(&self.data[..self.gap_start]); // pre gap
            buffer.extend_from_slice(slice.as_bytes()); // new text
            buffer.resize(buffer.len() + self.new_gap_size, 0); // new gap
            bytes = buffer.len();
            buffer.extend_from_slice(&self.data[self.gap_end..]); // post gap
        }
        self.cursor.bytes = bytes;
        self.data = buffer.into_boxed_slice();
        debug_assert_eq!(self.data.len(), new_capacity);
        let new = metrics(slice);
        self.cursor.chars += new.chars;
        self.gap_chars = self.cursor.chars;
        self.gap_end = self.cursor.bytes;
        self.gap_start = self.gap_end - self.new_gap_size;
        self.total += new;
        self.new_gap_size =
            cmp::max(self.len_bytes() / 20, cmp::min(self.new_gap_size * 2, Self::MAX_GAP));
    }

    #[inline]
    pub fn insert_char(&mut self, chr: char) {
        let buf = &mut [0; 4];
        self.insert(chr.encode_utf8(buf));
    }

    #[inline]
    pub fn insert(&mut self, slice: &str) {
        if slice.is_empty() {
            return;
        }
        self.metrics.insert(self.to_abs_pos(self.cursor), MetricBuilder::new(slice));
        if self.gap_len() < slice.len() {
            self.grow(slice);
        } else {
            // if gap is not at cursor, move it there
            if self.gap_chars != self.cursor.chars {
                self.move_gap(self.cursor);
            }
            let new_slice = &mut self.data[self.gap_start..(self.gap_start + slice.len())];
            new_slice.copy_from_slice(slice.as_bytes());
            self.gap_start += slice.len();
            let new = metrics(slice);
            self.gap_chars += new.chars;
            self.cursor.chars += new.chars;
            self.total += new;
        }
    }

    #[inline]
    pub fn delete_backwards(&mut self, size: usize) {
        let size = size.min(self.cursor.chars);
        self.delete_range(self.cursor.chars - size, self.cursor.chars);
    }

    #[inline]
    pub fn delete_forwards(&mut self, size: usize) {
        self.delete_range(self.cursor.chars, self.cursor.chars + size);
    }

    #[inline]
    pub fn delete_range(&mut self, beg: usize, end: usize) {
        if beg == end {
            return;
        }
        let (mut beg_chars, mut end_chars) = (beg, end);
        if beg_chars > end_chars {
            (beg_chars, end_chars) = (end_chars, beg_chars);
        }
        end_chars = end_chars.min(self.total.chars);
        beg_chars = beg_chars.min(self.total.chars);
        let end_bytes = self.char_to_byte(end_chars);
        let beg_bytes = self.char_to_byte(beg_chars);
        if end_bytes != beg_bytes {
            let beg = GapMetric { bytes: beg_bytes, chars: beg_chars };
            let end = GapMetric { bytes: end_bytes, chars: end_chars };
            self.metrics.delete(self.to_abs_pos(beg), self.to_abs_pos(end));
            self.delete_byte_range(beg, end);
        }
    }

    fn delete_byte_range(&mut self, beg: GapMetric, end: GapMetric) {
        // TODO: optimize this so that we count the chars deleted when calculating position
        assert!(beg.bytes <= end.bytes, "beg ({beg}) is greater then end ({end})");
        assert!(end.bytes <= self.data.len(), "end out of bounds");
        self.assert_char_boundary(beg.bytes);
        self.assert_char_boundary(end.bytes);
        if end.bytes < self.gap_start {
            // delete before gap
            //
            // hello New York City||||||||||
            //     ^      ^       ^         ^
            //     beg    end     gap_start gap_end
            //
            // shift end..gap_start to the right
            //
            // hell|||||||||||||||||ork City
            //     ^                ^
            //     gap_start        gap_end

            // update character count
            let deleted = end - beg;
            let delete_offset_chars = self.gap_chars - end.chars;
            self.gap_chars -= deleted.chars + delete_offset_chars;
            self.total -= deleted;
            let new_end = self.gap_end - (self.gap_start - end.bytes);
            // shift data
            self.data.copy_within(end.bytes..self.gap_start, new_end);
            // update cursor
            self.update_cursor_chars(beg.bytes, end.bytes, deleted.chars);
            if self.cursor.bytes < self.gap_start {
                if self.cursor.bytes > end.bytes {
                    self.cursor.bytes += self.gap_len();
                } else if self.cursor.bytes >= beg.bytes {
                    self.cursor.bytes = new_end;
                }
            }
            // update gap position
            self.gap_end = new_end;
            self.gap_start = beg.bytes;
        } else if beg.bytes >= self.gap_end {
            // delete after gap
            //
            // ||||||||||hello New York City
            // ^         ^         ^   ^
            // gap_start gap_end   beg end
            //
            // shift gap_end..beg to the left
            //
            // hello New |||||||||||||| City
            //           ^             ^
            //           gap_start     gap_end

            // update character count

            let deleted = end - beg;
            self.total -= deleted;
            self.gap_chars += beg.chars - self.gap_chars;
            // shift data
            self.data.copy_within(self.gap_end..beg.bytes, self.gap_start);
            // update cursor
            self.update_cursor_chars(beg.bytes, end.bytes, deleted.chars);
            if self.cursor.bytes >= self.gap_end {
                if self.cursor.bytes < beg.bytes {
                    self.cursor.bytes -= self.gap_len();
                } else if self.cursor.bytes < end.bytes {
                    self.cursor.bytes = end.bytes;
                }
            }
            // update gap position
            self.gap_start += beg.bytes - self.gap_end;
            self.gap_end = end.bytes;
        } else if beg.bytes < self.gap_start && end.bytes >= self.gap_end {
            // delete spans gap
            //
            // hello|||||||||| New York City
            //  ^   ^         ^       ^
            //  beg gap_start gap_end end
            //
            // update start and end of gap
            //
            // h||||||||||||||||||||||k City
            //  ^                     ^
            //  gap_start             gap_end

            // update character count
            let gap_start = GapMetric { bytes: self.gap_start, chars: self.gap_chars };
            let before = gap_start - beg;
            let gap_end = GapMetric { bytes: self.gap_end, chars: self.gap_chars };
            let after = end - gap_end;
            self.gap_chars -= before.chars;
            self.total -= before + after;
            // update gap position
            self.gap_start = beg.bytes;
            self.gap_end = end.bytes;
            self.update_cursor_chars(beg.bytes, end.bytes, before.chars + after.chars);
            if (beg.bytes..end.bytes).contains(&self.cursor.bytes) {
                self.cursor.bytes = end.bytes;
            }
        } else {
            unreachable!(
                "delete region inside gap -- gap: {}-{}, span: {beg}-{end}",
                self.gap_start, self.gap_end
            );
        }
    }

    fn update_cursor_chars(&mut self, beg: usize, end: usize, size: usize) {
        if self.cursor.bytes > beg {
            if self.cursor.bytes > end {
                self.cursor.chars -= size;
            } else {
                self.cursor.chars = self.gap_chars;
            }
        }
    }

    #[inline]
    pub fn cursor(&self) -> Position {
        Position::new(self.to_abs_pos(self.cursor))
    }

    #[inline]
    pub fn char_at(&self, pos: usize) -> Option<char> {
        if pos == self.len_chars() {
            return None;
        }
        let byte = self.char_to_byte(pos);
        let mut end = byte + 1;
        // UTF-8 character can only be 4 bytes long
        for _ in 0..3 {
            if self.is_char_boundary(end) {
                break;
            }
            end += 1;
        }
        debug_assert!(self.is_char_boundary(end));
        self.to_str(byte..end).chars().next()
    }

    #[inline]
    pub fn move_gap_out_of(&mut self, range: impl RangeBounds<usize>) {
        if !range.contains(&self.gap_chars)
            || range.start_bound() == Bound::Included(&self.gap_chars)
        {
            return;
        }

        let Range { start, end } = Self::bounds_to_range(range, self.len_bytes());

        let pos = if self.gap_chars - start < end - self.gap_chars {
            GapMetric { bytes: self.char_to_byte(start), chars: start }
        } else {
            GapMetric { bytes: self.char_to_byte(end), chars: end }
        };
        self.move_gap(pos);
    }

    #[inline]
    fn bounds_to_range(bounds: impl RangeBounds<usize>, max: usize) -> Range<usize> {
        let start = match bounds.start_bound() {
            Bound::Included(x) => *x,
            Bound::Excluded(_) => unreachable!(),
            Bound::Unbounded => 0,
        };

        let end = match bounds.end_bound() {
            Bound::Included(_) => unimplemented!("inclusive end bound not supported"),
            Bound::Excluded(x) => *x,
            Bound::Unbounded => max,
        };

        start..end
    }

    #[doc(hidden)]
    #[inline]
    pub fn benchmark_move_gap(&mut self) {
        if self.gap_chars == 0 {
            self.move_gap(GapMetric { bytes: self.data.len(), chars: self.len_chars() });
        } else {
            self.move_gap(GapMetric::default());
        }
    }

    #[doc(hidden)]
    #[inline]
    pub fn benchmark_build_metrics(string: &str) -> usize {
        let builder = MetricBuilder::new(string);
        let metrics = BufferMetrics::build(builder);
        metrics.len().bytes
    }

    fn move_gap(&mut self, pos: GapMetric) {
        assert!(pos.bytes <= self.data.len(), "attempt to move gap out of bounds");
        self.assert_char_boundary(pos.bytes);
        if pos.bytes < self.gap_start {
            // move gap backwards
            let shift = GapMetric { bytes: self.gap_start, chars: self.gap_chars } - pos;
            self.gap_chars -= shift.chars;

            self.data.copy_within(pos.bytes..self.gap_start, self.gap_end - shift.bytes);
            // if gap moves across cursor, update cursor position
            if self.cursor.bytes < self.gap_start && self.cursor.bytes >= pos.bytes {
                self.cursor.bytes += self.gap_len();
            }
            self.gap_start = pos.bytes;
            self.gap_end -= shift.bytes;
        } else if pos.bytes >= self.gap_end {
            // move gap forwards
            self.gap_chars += pos.chars - self.gap_chars;
            self.data.copy_within(self.gap_end..pos.bytes, self.gap_start);
            let size = pos.bytes - self.gap_end;
            // if gap moves across cursor, update cursor position
            if self.cursor.bytes >= self.gap_end && self.cursor.bytes < pos.bytes {
                self.cursor.bytes -= self.gap_len();
            }
            self.gap_start += size;
            self.gap_end = pos.bytes;
        } else {
            panic!(
                "move gap position byte: ({pos}) inside gap ({}-{})",
                self.gap_start, self.gap_end
            );
        }
    }

    #[inline]
    pub fn set_cursor(&mut self, pos: usize) {
        let pos = pos.min(self.total.chars);
        let byte_pos = self.char_to_byte(pos);
        self.cursor = GapMetric { bytes: byte_pos, chars: pos };
    }

    fn to_abs_pos(&self, pos: GapMetric) -> Metric {
        let chars = pos.chars;
        let bytes = if pos.bytes < self.gap_start {
            pos.bytes
        } else if pos.bytes >= self.gap_end {
            pos.bytes - self.gap_len()
        } else {
            unreachable!()
        };
        Metric { bytes, chars }
    }

    fn to_gapped_pos(&self, pos: Metric) -> GapMetric {
        let chars = pos.chars;
        let bytes = if pos.bytes < self.gap_start {
            pos.bytes
        } else if pos.bytes >= self.gap_start {
            pos.bytes + self.gap_len()
        } else {
            unreachable!()
        };
        GapMetric { bytes, chars }
    }

    #[inline]
    pub fn len_bytes(&self) -> usize {
        debug_assert_eq!(self.total.bytes + self.gap_len(), self.data.len());
        self.total.bytes
    }

    #[inline]
    pub const fn len_chars(&self) -> usize {
        self.total.chars
    }

    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.total.chars == 0
    }

    #[inline]
    const fn gap_len(&self) -> usize {
        self.gap_end - self.gap_start
    }

    #[inline]
    pub fn char_to_byte(&self, pos: usize) -> usize {
        if pos == self.gap_chars {
            return self.gap_end;
        }

        if pos + 1 == self.gap_chars {
            for i in 1..=4 {
                let pos = self.gap_start - i;
                if self.is_char_boundary(pos) {
                    return pos;
                }
            }
            unreachable!("couldn't find char boundary");
        }

        let (base, offset) = self.metrics.search_char(pos);
        debug_assert_eq!(base.chars + offset, pos);

        let base = self.to_gapped_pos(base);

        if offset == 0 {
            return base.bytes;
        }

        self.assert_char_boundary(base.bytes);

        if base.chars < self.gap_chars {
            if pos < self.gap_chars {
                let string = self.to_str(base.bytes..self.gap_start);
                chars::to_byte_idx(string, offset) + base.bytes
            } else {
                // the char crosses the gap
                let string = self.to_str(self.gap_end..);
                self.gap_end + chars::to_byte_idx(string, pos - self.gap_chars)
            }
        } else {
            let string = self.to_str(base.bytes..);
            chars::to_byte_idx(string, offset) + base.bytes
        }
    }

    #[inline]
    pub fn byte_to_char(&self, pos: usize) -> usize {
        if pos == self.gap_end {
            return self.gap_chars;
        }

        if pos == self.gap_start {
            return self.gap_chars;
        }

        assert!(pos >= self.gap_end || pos < self.gap_start, "position ({pos}) inside gap");

        let raw_pos = if pos > self.gap_end { pos - self.gap_len() } else { pos };

        let (base, offset) = self.metrics.search_byte(raw_pos);
        debug_assert_eq!(base.bytes + offset, raw_pos);

        let base = self.to_gapped_pos(base);

        if offset == 0 {
            return base.chars;
        }

        self.assert_char_boundary(base.bytes);

        if base.bytes < self.gap_start {
            if pos < self.gap_start {
                let string = self.to_str(base.bytes..self.gap_start);
                base.chars + chars::count(&string[..offset])
            } else {
                // the char crosses the gap
                let string = self.to_str(self.gap_end..);
                self.gap_chars + chars::count(&string[..pos - self.gap_end])
            }
        } else {
            let string = self.to_str(base.bytes..);
            base.chars + chars::count(&string[..offset])
        }
    }

    #[inline]
    fn to_str(&self, range: impl std::slice::SliceIndex<[u8], Output = [u8]>) -> &str {
        if cfg!(debug_assertions) {
            std::str::from_utf8(&self.data[range]).unwrap()
        } else {
            unsafe { std::str::from_utf8_unchecked(&self.data[range]) }
        }
    }

    #[inline]
    pub fn as_str(&mut self) -> &str {
        self.move_gap_out_of(..);
        let slice = if self.gap_start == 0 {
            self.to_str(self.gap_end..)
        } else {
            self.to_str(..self.gap_start)
        };
        assert_eq!(slice.len(), self.len_bytes());
        slice
    }

    #[inline]
    pub fn slice(&self, bounds: impl RangeBounds<usize>) -> (&str, &str) {
        let mut range = Self::bounds_to_range(bounds, self.total.chars);
        range.end = self.char_to_byte(range.end);
        range.start = self.char_to_byte(range.start);
        // the range straddles the gap, so we need to copy the two halves
        if range.start < self.gap_start && self.gap_start < range.end {
            let before = self.to_str(range.start..self.gap_start);
            let after = self.to_str(self.gap_end..range.end);
            (before, after)
        } else {
            (self.to_str(range), "")
        }
    }

    fn assert_char_boundary(&self, pos: usize) {
        if cfg!(debug_assertions) {
            if pos == self.gap_start {
                return;
            }
            assert!(self.is_char_boundary(pos), "position ({pos}) not on utf8 boundary");
        }
    }

    fn is_char_boundary(&self, pos: usize) -> bool {
        match self.data.get(pos) {
            Some(byte) => is_char_boundary(*byte),
            None => pos == self.data.len(),
        }
    }
}

fn metrics(slice: &str) -> Metric {
    let chars = chars::count(slice);
    Metric { bytes: slice.len(), chars }
}

#[expect(clippy::cast_possible_wrap)]
const fn is_char_boundary(byte: u8) -> bool {
    // This is bit magic equivalent to: b < 128 || b >= 192
    (byte as i8) >= -0x40
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn create() {
        let string = "hello buffer";
        let buffer = Buffer::from(string);
        assert_eq!(buffer.data.len(), string.len() + Buffer::GAP_SIZE);
        assert_eq!(buffer.gap_end, Buffer::GAP_SIZE);
        assert_eq!(buffer.gap_start, 0);

        let string = String::from("hello buffer");
        let buffer = Buffer::from(string);
        assert_eq!(buffer, "hello buffer");
    }

    #[test]
    fn test_empty() {
        let mut buffer = Buffer::new();
        assert_eq!(buffer.data.len(), 0);
        assert_eq!(buffer.gap_len(), 0);
        assert_eq!(buffer, "");
        buffer.insert("hello");
        assert_eq!(buffer, "hello");

        let mut buffer = Buffer::new();
        buffer.delete_range(0, 0);
        assert_eq!(buffer, "");
        buffer.delete_range(0, 5);
        assert_eq!(buffer, "");
    }

    #[test]
    fn insert() {
        let string = "hello buffer";
        let mut buffer = Buffer::from(string);
        buffer.len_bytes();
        buffer.insert_char('x');
        buffer.len_bytes();
        assert_eq!(buffer.data.len(), string.len() + Buffer::GAP_SIZE);
        assert_eq!(buffer.gap_end, Buffer::GAP_SIZE);
        assert_eq!(buffer.gap_start, 1);
        assert_eq!(buffer, "xhello buffer");
    }

    #[test]
    fn insert_slice() {
        let string = String::from("world");
        let new_string = "hi ";
        let mut buffer = Buffer::from(string);
        buffer.insert(new_string);
        buffer.move_gap_out_of(..);
        assert_eq!(buffer, "hi world");
        buffer.insert("starting Θ text ");
        assert_eq!(buffer, "hi starting Θ text world");
        buffer.set_cursor(21);
        buffer.insert("x");
        assert_eq!(buffer, "hi starting Θ text woxrld");
    }

    #[test]
    fn empty() {
        let mut buffer = Buffer::from("");
        assert_eq!(buffer, "");
        buffer.delete_range(1, 2);
        assert_eq!(buffer, "");
    }

    #[test]
    fn test_delete() {
        let world = "world";
        let hello = "hello ";
        let mut buffer = Buffer::from(world);
        buffer.insert(hello);
        buffer.delete_backwards(4);
        assert_eq!(buffer.gap_start, hello.len() - 4);
        buffer.move_gap_out_of(..);
        buffer.move_gap_out_of(..);
        buffer.move_gap(GapMetric { bytes: buffer.char_to_byte(7), chars: 7 });
        buffer.move_gap_out_of(..);
        assert_eq!(buffer, "heworld");
    }

    #[test]
    fn delete_forwards() {
        let world = "world";
        let hello = "hello ";
        let mut buffer = Buffer::from(world);
        buffer.insert(hello);
        buffer.delete_forwards(4);
        buffer.move_gap_out_of(..);
        assert_eq!(buffer, "hello d");
    }

    #[test]
    fn test_delete_region() {
        let mut buffer = Buffer::from("world");
        buffer.insert("hello ");
        buffer.delete_range(1, 3);
        buffer.move_gap_out_of(..);
        assert_eq!(buffer, "hlo world");
        buffer.delete_range(4, 6);
        buffer.move_gap_out_of(..);
        assert_eq!(buffer, "hlo rld");
    }

    #[test]
    fn test_char_at() {
        let buffer = Buffer::from("aµ福");
        assert_eq!(buffer.char_at(0), Some('a'));
        assert_eq!(buffer.char_at(1), Some('µ'));
        assert_eq!(buffer.char_at(2), Some('福'));
        assert_eq!(buffer.char_at(3), None);
    }

    #[test]
    fn test_delete_nothing() {
        let mut buffer = Buffer::from("world");
        buffer.insert("hello ");
        buffer.delete_range(3, 3);
        assert_eq!(buffer, "hello world");
    }

    // cases found during fuzzing
    #[test]
    fn edge_cases() {
        let mut buffer = Buffer::from(":?abdix7");
        assert_eq!(buffer.len_bytes(), 8);
        buffer.delete_range(2, 5);
        assert_eq!(buffer.len_bytes(), 5);
        buffer.delete_range(5, 4);
        assert_eq!(buffer.len_bytes(), 4);
        buffer.delete_range(0, 3);

        let mut buffer = Buffer::from("xyz");
        buffer.insert("abc");
        buffer.set_cursor(2);
        buffer.delete_range(1, 4);
        assert_eq!(buffer, "ayz");
        buffer.insert("b");
        assert_eq!(buffer, "abyz");

        let mut buffer = Buffer::from("ƽaejcoeuz");
        buffer.delete_range(5, 6);
        buffer.delete_range(1, 8);
        assert_eq!(buffer, "ƽ");
    }

    // from reference implementation
    #[test]
    fn test_delete_to_gap() {
        let mut buffer = Buffer::from("\n\n\n\nAutomerge is too");
        buffer.insert("per. Some graduate students in ");
        buffer.set_cursor(10);
        buffer.delete_forwards(21);
        assert_eq!(buffer, "per. Some \n\n\n\nAutomerge is too");
    }

    // fuzzing
    #[test]
    fn test_bounds() {
        let mut buffer = Buffer::from("world");
        buffer.insert("hello ");
        buffer.delete_range(3, 100);
        assert_eq!(buffer, "hel");
        buffer.delete_range(10, 1);
        assert_eq!(buffer, "h");

        let mut buffer = Buffer::from(",skeobg x");
        buffer.delete_range(10, 10);

        let mut buffer = Buffer::from("+skeocptv'eigp");
        buffer.delete_range(30, 6);
    }

    #[test]
    fn resize() {
        let world = "world";
        let hello = "hello ";
        let mut buffer = Buffer::from(world);
        buffer.insert(hello);
        assert_eq!(buffer.gap_start, hello.len());
        assert_eq!(buffer, "hello world");
    }

    #[test]
    fn cursor() {
        let string = "world";
        let new_string = "hi ";
        let mut buffer = Buffer::from(string);
        buffer.insert(new_string);
        assert_eq!(buffer.gap_chars, new_string.len());
    }

    #[test]
    fn test_slice() {
        let mut buffer = Buffer::from("hello world");
        assert_eq!(buffer.slice(..), ("hello world", ""));
        buffer.set_cursor(5);
        buffer.insert("\u{B5}");
        assert_eq!(buffer.slice(0..0), ("", ""));
        assert_eq!(buffer.slice(..6), ("hello\u{B5}", ""));
        assert_eq!(buffer.slice(6..), (" world", ""));
        assert_eq!(buffer.slice(6..6), ("", ""));
        assert_eq!(buffer.slice(5..11), ("\u{B5}", " worl"));
        assert_eq!(buffer.slice(5..11), ("\u{B5}", " worl"));
        assert_eq!(buffer.slice(4..6), ("o\u{B5}", ""));
        assert_eq!(buffer.slice(..), ("hello\u{B5}", " world"));
    }

    #[test]
    fn test_byte_to_char_conversion() {
        let buffer = Buffer::from("hello world");
        for i in 0..buffer.len_chars() {
            assert_eq!(buffer.byte_to_char(buffer.char_to_byte(i)), i);
        }
        let buffer = Buffer::from("hello\u{B5} world");
        for i in 0..buffer.len_chars() {
            assert_eq!(buffer.byte_to_char(buffer.char_to_byte(i)), i);
        }
        let mut buffer = Buffer::from("\u{B5}\u{45}hello world");
        buffer.set_cursor(5);
        buffer.insert("\u{B9}");
        buffer.set_cursor(10);
        buffer.insert("\u{A0}x");
        for i in 0..buffer.len_chars() {
            assert_eq!(buffer.byte_to_char(buffer.char_to_byte(i)), i);
        }
    }

    #[test]
    fn test_build_unicode() {
        let string = "aaaaaaaaaՂaaaaaaaaa";
        let _ = Buffer::from(string);
    }

    #[test]
    fn test_append() {
        let mut buffer = Buffer::from("aa");
        buffer.set_cursor(3);
        let string = "B\u{1b}BBBBBB\u{1b}\0\0\0\0\0\0BB\u{1b}\u{1b}\u{1b}\u{1b}\u{1b}\u{1b}B\u{7}BBBBBBBBBBB\u{1b}\u{1b}\u{1b}B\u{7}BBBBBBBBBBBB\u{1b}\u{1b}B\u{7}BBBBBBBBB";
        buffer.insert(string);
    }

    #[test]
    fn test_fuzzer() {
        let mut buffer = Buffer::new();
        buffer.set_cursor(1);
        buffer.insert("Ղ\u{2}\u{2}\0\0\0");
        buffer.set_cursor(4);
        buffer.insert("&\0''''''''''''''''''''%'''''&\0''''''''''''''''''''%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@''''''''''");
        buffer.set_cursor(39);
        buffer.insert("'\u{2}&\0''''''''''''''''''''%''''''''''''''''''''''''''''");
        buffer.delete_range(184, 169);
        buffer.set_cursor(127);
        buffer.insert("00000000061288823:*********");
        buffer.set_cursor(132);
        buffer.insert("5''''''''''''''\0\0\0\0\0'''''''");
        buffer.set_cursor(97);
        buffer.insert("''?????????????????????z?????????????????????'''''\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{1d}\u{10}");
        buffer.delete_range(13, 138);
        buffer.set_cursor(25);
        buffer
            .insert("yyyyyyyyyyyyyy\u{2}\0\u{2}\0\0\u{1}\u{17}H\u{17}\u{17}\u{17}\u{17}\u{17}\0\0");
        buffer.set_cursor(138);
        buffer.insert("\u{17}?\u{17}\u{17}\u{17}\u{17}\u{17}\u{17}\u{17}\u{17}\u{17}\0\0\0\0\0\0\u{3}\0\0\0''''''''");
        buffer.set_cursor(39);
        buffer.insert("\0\0\0''''''''''");
        buffer.delete_range(247, 45);
    }

    #[test]
    fn test_pos() {
        let mut buffer = Buffer::new();
        buffer.set_cursor(1);
        buffer.insert("AAAAAAAAAAAAAAAAAAA");
        buffer.set_cursor(10);
        buffer.insert("AAAAAA\0\0AAAAAA");
        buffer.set_cursor(26);
    }
}