neovm-core 0.0.1

Core runtime structures for NeoVM
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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! A UTF-8 aware gap buffer for efficient text editing.
//!
//! The gap buffer stores text in a contiguous `Vec<u8>` with a movable "gap"
//! (unused region) that makes insertions and deletions near the gap O(1)
//! amortized. The gap is relocated to the edit site before each mutation so
//! that sequential edits in the same neighborhood avoid large copies.
//!
//! All public positions are **byte** positions into the logical text (i.e. the
//! text with the gap removed) unless a parameter is explicitly named
//! `char_pos`. The caller is responsible for ensuring byte positions fall on
//! UTF-8 character boundaries.

use std::fmt;

/// Default initial gap size in bytes.
const DEFAULT_GAP_SIZE: usize = 64;

/// Growth factor when the gap must be expanded — the new gap will be at least
/// this many bytes, or the requested size, whichever is larger.
const MIN_GAP_GROW: usize = 64;

/// A gap buffer holding UTF-8 encoded text.
///
/// Internally the backing store looks like:
///
/// ```text
///  [ text-before-gap | gap (unused) | text-after-gap ]
///    0..gap_start      gap_start..gap_end  gap_end..buf.len()
/// ```
///
/// The *logical* text is the concatenation of `buf[..gap_start]` and
/// `buf[gap_end..]`.
#[derive(Clone)]
pub struct GapBuffer {
    /// Raw backing store.
    buf: Vec<u8>,
    /// Byte index where the gap begins (first unused byte).
    gap_start: usize,
    /// Byte index one past the last gap byte (first byte of text after gap).
    gap_end: usize,
}

// ---------------------------------------------------------------------------
// Construction
// ---------------------------------------------------------------------------

impl GapBuffer {
    /// Create an empty gap buffer with a default-sized gap.
    pub fn new() -> Self {
        Self {
            buf: vec![0u8; DEFAULT_GAP_SIZE],
            gap_start: 0,
            gap_end: DEFAULT_GAP_SIZE,
        }
    }

    /// Create a gap buffer pre-loaded with the contents of `s`.
    pub fn from_str(s: &str) -> Self {
        let text = s.as_bytes();
        let gap = DEFAULT_GAP_SIZE;
        let mut buf = Vec::with_capacity(text.len() + gap);
        buf.extend_from_slice(text);
        buf.resize(text.len() + gap, 0);
        // Place gap at the end so that appending is cheap.
        Self {
            buf,
            gap_start: text.len(),
            gap_end: text.len() + gap,
        }
    }

    // -----------------------------------------------------------------------
    // Queries
    // -----------------------------------------------------------------------

    /// Total length of the logical text in **bytes** (excluding the gap).
    #[inline]
    pub fn len(&self) -> usize {
        self.buf.len() - self.gap_size()
    }

    /// Whether the buffer contains no text.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Number of Unicode scalar values (chars) in the buffer.
    pub fn char_count(&self) -> usize {
        // Count chars in the two text segments on either side of the gap.
        let before = std::str::from_utf8(&self.buf[..self.gap_start])
            .expect("pre-gap text is not valid UTF-8");
        let after = std::str::from_utf8(&self.buf[self.gap_end..])
            .expect("post-gap text is not valid UTF-8");
        before.chars().count() + after.chars().count()
    }

    /// Size of the gap in bytes.
    #[inline]
    pub fn gap_size(&self) -> usize {
        self.gap_end - self.gap_start
    }

    // -----------------------------------------------------------------------
    // Single-element access
    // -----------------------------------------------------------------------

    /// Return the byte at logical position `pos`.
    ///
    /// # Panics
    ///
    /// Panics if `pos >= self.len()`.
    pub fn byte_at(&self, pos: usize) -> u8 {
        assert!(
            pos < self.len(),
            "byte_at: position {pos} out of range (len {})",
            self.len()
        );
        if pos < self.gap_start {
            self.buf[pos]
        } else {
            self.buf[pos + self.gap_size()]
        }
    }

    /// Return the `char` whose first byte starts at logical byte position `pos`,
    /// or `None` if `pos >= self.len()`.
    ///
    /// # Panics
    ///
    /// Panics if `pos` does not lie on a UTF-8 character boundary.
    pub fn char_at(&self, pos: usize) -> Option<char> {
        if pos >= self.len() {
            return None;
        }
        // Decode the character spanning up to 4 bytes starting at `pos`.
        let first = self.byte_at(pos);
        let char_len = utf8_char_len(first);
        assert!(
            pos + char_len <= self.len(),
            "char_at: incomplete UTF-8 sequence at byte position {pos}"
        );
        let mut tmp = [0u8; 4];
        for i in 0..char_len {
            tmp[i] = self.byte_at(pos + i);
        }
        let s = std::str::from_utf8(&tmp[..char_len]).expect("char_at: invalid UTF-8 sequence");
        s.chars().next()
    }

    // -----------------------------------------------------------------------
    // Range extraction
    // -----------------------------------------------------------------------

    /// Extract the text in the logical byte range `[start, end)` as a `String`.
    ///
    /// # Panics
    ///
    /// Panics if `start > end` or `end > self.len()`.
    pub fn text_range(&self, start: usize, end: usize) -> String {
        assert!(start <= end, "text_range: start ({start}) > end ({end})");
        assert!(
            end <= self.len(),
            "text_range: end ({end}) > len ({})",
            self.len()
        );
        if start == end {
            return String::new();
        }

        let mut out = Vec::with_capacity(end - start);

        // The logical text is split into two physical segments:
        //   segment A: buf[0 .. gap_start]       (logical positions 0 .. gap_start)
        //   segment B: buf[gap_end .. buf.len()]  (logical positions gap_start .. len)
        //
        // We need to copy the intersection of [start, end) with each segment.

        // Intersection with segment A (logical 0..gap_start).
        if start < self.gap_start {
            let seg_end = end.min(self.gap_start);
            out.extend_from_slice(&self.buf[start..seg_end]);
        }

        // Intersection with segment B (logical gap_start..len).
        if end > self.gap_start {
            let seg_start = start.max(self.gap_start);
            let phys_start = seg_start + self.gap_size();
            let phys_end = end + self.gap_size();
            out.extend_from_slice(&self.buf[phys_start..phys_end]);
        }

        String::from_utf8(out).expect("text_range: extracted bytes are not valid UTF-8")
    }

    /// Copy bytes in the logical range `[start, end)` into `out`.
    ///
    /// `out` is cleared first, then the bytes are appended.
    /// This is more efficient than `text_range()` when you don't need
    /// a `String` — it avoids the UTF-8 validation and String allocation.
    ///
    /// # Panics
    /// Panics if `start > end` or `end > self.len()`.
    pub fn copy_bytes_to(&self, start: usize, end: usize, out: &mut Vec<u8>) {
        assert!(start <= end, "copy_bytes_to: start ({start}) > end ({end})");
        assert!(
            end <= self.len(),
            "copy_bytes_to: end ({end}) > len ({})",
            self.len()
        );
        out.clear();
        if start == end {
            return;
        }
        out.reserve(end - start);

        // Intersection with segment A (logical 0..gap_start).
        if start < self.gap_start {
            let seg_end = end.min(self.gap_start);
            out.extend_from_slice(&self.buf[start..seg_end]);
        }

        // Intersection with segment B (logical gap_start..len).
        if end > self.gap_start {
            let seg_start = start.max(self.gap_start);
            let phys_start = seg_start + self.gap_size();
            let phys_end = end + self.gap_size();
            out.extend_from_slice(&self.buf[phys_start..phys_end]);
        }
    }

    /// Return the full buffer contents as a `String`.
    pub fn to_string(&self) -> String {
        self.text_range(0, self.len())
    }

    // -----------------------------------------------------------------------
    // Mutation
    // -----------------------------------------------------------------------

    /// Insert `s` at logical byte position `pos`.
    ///
    /// After the call the gap sits immediately after the newly inserted text,
    /// so consecutive inserts at the same position are fast.
    ///
    /// # Panics
    ///
    /// Panics if `pos > self.len()` or `pos` is not on a UTF-8 boundary.
    pub fn insert_str(&mut self, pos: usize, s: &str) {
        assert!(
            pos <= self.len(),
            "insert_str: position {pos} out of range (len {})",
            self.len()
        );
        if s.is_empty() {
            return;
        }
        // Validate that `pos` falls on a char boundary (unless at the very end).
        debug_assert!(
            pos == self.len() || self.is_char_boundary(pos),
            "insert_str: position {pos} is not on a UTF-8 character boundary"
        );

        let bytes = s.as_bytes();
        self.move_gap_to(pos);
        self.ensure_gap(bytes.len());

        // Copy the new text into the gap.
        self.buf[self.gap_start..self.gap_start + bytes.len()].copy_from_slice(bytes);
        self.gap_start += bytes.len();
    }

    /// Delete the logical byte range `[start, end)`.
    ///
    /// # Panics
    ///
    /// Panics if `start > end`, `end > self.len()`, or either boundary is not
    /// on a UTF-8 character boundary.
    pub fn delete_range(&mut self, start: usize, end: usize) {
        assert!(start <= end, "delete_range: start ({start}) > end ({end})");
        assert!(
            end <= self.len(),
            "delete_range: end ({end}) > len ({})",
            self.len()
        );
        if start == end {
            return;
        }
        debug_assert!(
            self.is_char_boundary(start),
            "delete_range: start ({start}) is not on a UTF-8 character boundary"
        );
        debug_assert!(
            end == self.len() || self.is_char_boundary(end),
            "delete_range: end ({end}) is not on a UTF-8 character boundary"
        );

        // Move the gap so that it starts at `start`, then extend it to swallow
        // the bytes up to `end`.
        self.move_gap_to(start);
        // After move_gap_to(start), gap_start == start and the bytes that were
        // logically at [start, end) now sit at buf[gap_end .. gap_end + (end - start)].
        self.gap_end += end - start;
    }

    /// Overwrite the logical byte range `[start, end)` with `replacement`.
    ///
    /// `replacement` must have the exact same byte length as the replaced
    /// region, and both boundaries must lie on UTF-8 character boundaries.
    pub fn replace_same_len_range(&mut self, start: usize, end: usize, replacement: &str) {
        assert!(
            start <= end,
            "replace_same_len_range: start ({start}) > end ({end})"
        );
        assert!(
            end <= self.len(),
            "replace_same_len_range: end ({end}) > len ({})",
            self.len()
        );
        assert_eq!(
            replacement.len(),
            end - start,
            "replace_same_len_range: replacement byte length ({}) must match replaced length ({})",
            replacement.len(),
            end - start
        );
        if start == end {
            return;
        }
        debug_assert!(
            self.is_char_boundary(start),
            "replace_same_len_range: start ({start}) is not on a UTF-8 character boundary"
        );
        debug_assert!(
            end == self.len() || self.is_char_boundary(end),
            "replace_same_len_range: end ({end}) is not on a UTF-8 character boundary"
        );

        self.move_gap_to(end);
        self.buf[start..end].copy_from_slice(replacement.as_bytes());
    }

    // -----------------------------------------------------------------------
    // Gap management
    // -----------------------------------------------------------------------

    /// Move the gap so that `gap_start == pos`.
    ///
    /// This copies text bytes across the gap to reposition it.
    ///
    /// # Panics
    ///
    /// Panics if `pos > self.len()`.
    pub fn move_gap_to(&mut self, pos: usize) {
        assert!(
            pos <= self.len(),
            "move_gap_to: position {pos} out of range (len {})",
            self.len()
        );

        if pos == self.gap_start {
            return;
        }

        let gap = self.gap_size();

        if pos < self.gap_start {
            // Moving gap left: shift buf[pos..gap_start] to the right by `gap`.
            let count = self.gap_start - pos;
            // Use copy_within which handles overlapping regions.
            self.buf.copy_within(pos..pos + count, pos + gap);
            self.gap_start = pos;
            self.gap_end = pos + gap;
        } else {
            // Moving gap right: shift buf[gap_end..gap_end + (pos - gap_start)]
            // to the left by `gap`.
            let count = pos - self.gap_start;
            let src_start = self.gap_end;
            let dst_start = self.gap_start;
            self.buf
                .copy_within(src_start..src_start + count, dst_start);
            self.gap_start = pos;
            self.gap_end = pos + gap;
        }
    }

    /// Ensure the gap is at least `min_size` bytes. If it is already large
    /// enough this is a no-op; otherwise the backing buffer is reallocated.
    pub fn ensure_gap(&mut self, min_size: usize) {
        if self.gap_size() >= min_size {
            return;
        }
        let grow = (min_size - self.gap_size()).max(MIN_GAP_GROW);
        let old_gap_end = self.gap_end;
        let after_gap_len = self.buf.len() - old_gap_end;

        // Extend the backing buffer.
        self.buf.resize(self.buf.len() + grow, 0);

        // Shift the post-gap segment to the right by `grow` to widen the gap.
        if after_gap_len > 0 {
            self.buf
                .copy_within(old_gap_end..old_gap_end + after_gap_len, old_gap_end + grow);
        }
        self.gap_end += grow;
    }

    // -----------------------------------------------------------------------
    // Position conversion
    // -----------------------------------------------------------------------

    /// Convert a logical byte position to a char (scalar value) position.
    ///
    /// Returns the number of complete characters before `byte_pos`.
    ///
    /// # Panics
    ///
    /// Panics if `byte_pos > self.len()` or is not on a character boundary.
    pub fn byte_to_char(&self, byte_pos: usize) -> usize {
        assert!(
            byte_pos <= self.len(),
            "byte_to_char: byte_pos ({byte_pos}) > len ({})",
            self.len()
        );
        if byte_pos == 0 {
            return 0;
        }

        // Count chars in pre-gap portion that falls within [0, byte_pos).
        let mut chars = 0usize;

        if byte_pos <= self.gap_start {
            // Entirely within segment A.
            let slice = &self.buf[..byte_pos];
            let s = std::str::from_utf8(slice)
                .expect("byte_to_char: not a valid UTF-8 boundary (segment A)");
            chars = s.chars().count();
        } else {
            // Spans both segments.
            let pre = std::str::from_utf8(&self.buf[..self.gap_start])
                .expect("byte_to_char: pre-gap segment is not valid UTF-8");
            chars += pre.chars().count();

            let remaining = byte_pos - self.gap_start;
            let post_slice = &self.buf[self.gap_end..self.gap_end + remaining];
            let post = std::str::from_utf8(post_slice)
                .expect("byte_to_char: not a valid UTF-8 boundary (segment B)");
            chars += post.chars().count();
        }
        chars
    }

    /// Convert a char position to a logical byte position.
    ///
    /// `char_pos` is the number of characters from the start of the buffer.
    ///
    /// # Panics
    ///
    /// Panics if `char_pos > self.char_count()`.
    pub fn char_to_byte(&self, char_pos: usize) -> usize {
        if char_pos == 0 {
            return 0;
        }

        let mut remaining = char_pos;

        // Walk segment A (pre-gap).
        let pre = std::str::from_utf8(&self.buf[..self.gap_start])
            .expect("char_to_byte: pre-gap segment is not valid UTF-8");
        for (byte_offset, _ch) in pre.char_indices() {
            if remaining == 0 {
                return byte_offset;
            }
            remaining -= 1;
        }
        // If we consumed all of segment A and remaining == 0, the byte position
        // is at gap_start.
        if remaining == 0 {
            return self.gap_start;
        }

        // Walk segment B (post-gap).
        let post = std::str::from_utf8(&self.buf[self.gap_end..])
            .expect("char_to_byte: post-gap segment is not valid UTF-8");
        for (byte_offset, _ch) in post.char_indices() {
            if remaining == 0 {
                // byte_offset is relative to segment B; logical = gap_start + byte_offset.
                return self.gap_start + byte_offset;
            }
            remaining -= 1;
        }
        if remaining == 0 {
            return self.len();
        }

        // Clamp to end of buffer instead of panicking — this can happen
        // when window_start / point are stale after buffer modification.
        tracing::debug!(
            "char_to_byte: char_pos ({char_pos}) exceeds char_count ({}), clamping",
            self.char_count()
        );
        return self.len();
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Check whether `pos` falls on a UTF-8 character boundary in the logical
    /// text.
    fn is_char_boundary(&self, pos: usize) -> bool {
        if pos == 0 || pos >= self.len() {
            return true;
        }
        let b = self.byte_at(pos);
        // In UTF-8, a continuation byte has the bit pattern 10xxxxxx.
        // A character boundary is any byte that is NOT a continuation byte.
        is_utf8_start_byte(b)
    }

    // pdump accessors
    /// Extract the logical text content as a byte vector (for pdump).
    pub(crate) fn dump_text(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(self.len());
        out.extend_from_slice(&self.buf[..self.gap_start]);
        out.extend_from_slice(&self.buf[self.gap_end..]);
        out
    }
    /// Reconstruct from text bytes (for pdump load).
    pub(crate) fn from_dump(text: Vec<u8>) -> Self {
        let len = text.len();
        Self {
            buf: text,
            gap_start: len,
            gap_end: len,
        }
    }
}

// ---------------------------------------------------------------------------
// Trait implementations
// ---------------------------------------------------------------------------

impl Default for GapBuffer {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for GapBuffer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_string())
    }
}

impl fmt::Debug for GapBuffer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GapBuffer")
            .field("len", &self.len())
            .field("gap_start", &self.gap_start)
            .field("gap_end", &self.gap_end)
            .field("gap_size", &self.gap_size())
            .field("text", &self.to_string())
            .finish()
    }
}

// ---------------------------------------------------------------------------
// Free helper functions
// ---------------------------------------------------------------------------

/// Return the byte length of a UTF-8 character given its first byte.
///
/// Panics on an invalid leading byte (continuation byte or 0xFF/0xFE).
#[inline]
fn utf8_char_len(first_byte: u8) -> usize {
    match first_byte {
        0x00..=0x7F => 1,
        0xC0..=0xDF => 2,
        0xE0..=0xEF => 3,
        0xF0..=0xF7 => 4,
        _ => panic!("utf8_char_len: invalid UTF-8 leading byte 0x{first_byte:02X}"),
    }
}

/// Returns `true` if `b` is a valid start byte (not a continuation byte).
#[inline]
fn is_utf8_start_byte(b: u8) -> bool {
    // Continuation bytes are 0x80..=0xBF (bit pattern 10xxxxxx).
    (b & 0xC0) != 0x80
}

// ===========================================================================
// Tests
// ===========================================================================

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

    // -----------------------------------------------------------------------
    // Construction & basic queries
    // -----------------------------------------------------------------------

    #[test]
    fn new_buffer_is_empty() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::new();
        assert_eq!(buf.len(), 0);
        assert!(buf.is_empty());
        assert_eq!(buf.char_count(), 0);
        assert_eq!(buf.to_string(), "");
    }

    #[test]
    fn from_str_ascii() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello");
        assert_eq!(buf.len(), 5);
        assert_eq!(buf.char_count(), 5);
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn from_str_empty() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("");
        assert_eq!(buf.len(), 0);
        assert!(buf.is_empty());
        assert_eq!(buf.to_string(), "");
    }

    // -----------------------------------------------------------------------
    // insert_str
    // -----------------------------------------------------------------------

    #[test]
    fn insert_at_beginning() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("world");
        buf.insert_str(0, "hello ");
        assert_eq!(buf.to_string(), "hello world");
    }

    #[test]
    fn insert_at_end() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.insert_str(5, " world");
        assert_eq!(buf.to_string(), "hello world");
    }

    #[test]
    fn insert_in_middle() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("helo");
        buf.insert_str(2, "l");
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn insert_into_empty_buffer() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::new();
        buf.insert_str(0, "abc");
        assert_eq!(buf.to_string(), "abc");
        assert_eq!(buf.len(), 3);
    }

    #[test]
    fn insert_empty_string_is_noop() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.insert_str(3, "");
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn multiple_sequential_inserts() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::new();
        buf.insert_str(0, "a");
        buf.insert_str(1, "b");
        buf.insert_str(2, "c");
        buf.insert_str(3, "d");
        assert_eq!(buf.to_string(), "abcd");
    }

    #[test]
    fn insert_larger_than_gap() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::new();
        let long = "x".repeat(256);
        buf.insert_str(0, &long);
        assert_eq!(buf.to_string(), long);
        assert_eq!(buf.len(), 256);
    }

    // -----------------------------------------------------------------------
    // delete_range
    // -----------------------------------------------------------------------

    #[test]
    fn delete_from_beginning() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello world");
        buf.delete_range(0, 6);
        assert_eq!(buf.to_string(), "world");
    }

    #[test]
    fn delete_from_end() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello world");
        buf.delete_range(5, 11);
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn delete_from_middle() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello world");
        buf.delete_range(5, 6); // delete the space
        assert_eq!(buf.to_string(), "helloworld");
    }

    #[test]
    fn delete_everything() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.delete_range(0, 5);
        assert_eq!(buf.to_string(), "");
        assert!(buf.is_empty());
    }

    #[test]
    fn delete_empty_range_is_noop() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.delete_range(2, 2);
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn delete_then_insert() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello world");
        buf.delete_range(5, 11);
        buf.insert_str(5, " rust");
        assert_eq!(buf.to_string(), "hello rust");
    }

    // -----------------------------------------------------------------------
    // byte_at / char_at
    // -----------------------------------------------------------------------

    #[test]
    fn byte_at_ascii() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("abcde");
        assert_eq!(buf.byte_at(0), b'a');
        assert_eq!(buf.byte_at(4), b'e');
    }

    #[test]
    fn byte_at_after_gap_move() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("abcde");
        buf.move_gap_to(2);
        // Logical content unchanged.
        assert_eq!(buf.byte_at(0), b'a');
        assert_eq!(buf.byte_at(2), b'c');
        assert_eq!(buf.byte_at(4), b'e');
    }

    #[test]
    fn char_at_ascii() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello");
        assert_eq!(buf.char_at(0), Some('h'));
        assert_eq!(buf.char_at(4), Some('o'));
        assert_eq!(buf.char_at(5), None);
    }

    #[test]
    #[should_panic]
    fn byte_at_out_of_range_panics() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hi");
        buf.byte_at(2);
    }

    // -----------------------------------------------------------------------
    // text_range
    // -----------------------------------------------------------------------

    #[test]
    fn text_range_full() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello world");
        assert_eq!(buf.text_range(0, 11), "hello world");
    }

    #[test]
    fn text_range_prefix() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello world");
        assert_eq!(buf.text_range(0, 5), "hello");
    }

    #[test]
    fn text_range_suffix() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello world");
        assert_eq!(buf.text_range(6, 11), "world");
    }

    #[test]
    fn text_range_spanning_gap() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello world");
        buf.move_gap_to(5);
        // Range spans the gap.
        assert_eq!(buf.text_range(3, 8), "lo wo");
    }

    #[test]
    fn text_range_empty() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello");
        assert_eq!(buf.text_range(2, 2), "");
    }

    // -----------------------------------------------------------------------
    // move_gap_to
    // -----------------------------------------------------------------------

    #[test]
    fn move_gap_to_start() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.move_gap_to(0);
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn move_gap_to_end() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.move_gap_to(5);
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn move_gap_around() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("abcdef");
        buf.move_gap_to(3);
        assert_eq!(buf.to_string(), "abcdef");
        buf.move_gap_to(0);
        assert_eq!(buf.to_string(), "abcdef");
        buf.move_gap_to(6);
        assert_eq!(buf.to_string(), "abcdef");
        buf.move_gap_to(2);
        assert_eq!(buf.to_string(), "abcdef");
    }

    // -----------------------------------------------------------------------
    // ensure_gap
    // -----------------------------------------------------------------------

    #[test]
    fn ensure_gap_grows() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        let old_gap = buf.gap_size();
        buf.ensure_gap(old_gap + 100);
        assert!(buf.gap_size() >= old_gap + 100);
        // Content must be preserved.
        assert_eq!(buf.to_string(), "hello");
    }

    #[test]
    fn ensure_gap_noop_when_large_enough() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        let old_gap = buf.gap_size();
        buf.ensure_gap(1);
        assert_eq!(buf.gap_size(), old_gap);
    }

    // -----------------------------------------------------------------------
    // Multibyte / UTF-8 (CJK, emoji)
    // -----------------------------------------------------------------------

    #[test]
    fn multibyte_cjk() {
        crate::test_utils::init_test_tracing();
        // Each CJK character is 3 bytes in UTF-8.
        let text = "\u{4F60}\u{597D}\u{4E16}\u{754C}"; // 你好世界
        let buf = GapBuffer::from_str(text);
        assert_eq!(buf.len(), 12); // 4 chars * 3 bytes
        assert_eq!(buf.char_count(), 4);
        assert_eq!(buf.to_string(), text);

        // char_at at byte boundaries
        assert_eq!(buf.char_at(0), Some('\u{4F60}')); //        assert_eq!(buf.char_at(3), Some('\u{597D}')); //        assert_eq!(buf.char_at(6), Some('\u{4E16}')); //        assert_eq!(buf.char_at(9), Some('\u{754C}')); //    }

    #[test]
    fn multibyte_emoji() {
        crate::test_utils::init_test_tracing();
        // Emoji are 4 bytes in UTF-8.
        let text = "\u{1F600}\u{1F60D}"; // two emoji
        let buf = GapBuffer::from_str(text);
        assert_eq!(buf.len(), 8);
        assert_eq!(buf.char_count(), 2);
        assert_eq!(buf.char_at(0), Some('\u{1F600}'));
        assert_eq!(buf.char_at(4), Some('\u{1F60D}'));
    }

    #[test]
    fn insert_multibyte_in_middle() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("ab");
        buf.insert_str(1, "\u{1F600}"); // insert emoji between a and b
        assert_eq!(buf.to_string(), "a\u{1F600}b");
        assert_eq!(buf.len(), 6); // 1 + 4 + 1
        assert_eq!(buf.char_count(), 3);
    }

    #[test]
    fn delete_multibyte_char() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("a\u{4F60}b"); // a你b
        // Delete the CJK char (bytes 1..4).
        buf.delete_range(1, 4);
        assert_eq!(buf.to_string(), "ab");
    }

    #[test]
    fn text_range_multibyte_spanning_gap() {
        crate::test_utils::init_test_tracing();
        let text = "\u{4F60}\u{597D}\u{4E16}\u{754C}"; // 你好世界
        let mut buf = GapBuffer::from_str(text);
        buf.move_gap_to(6); // gap between 好 and 世
        assert_eq!(buf.text_range(0, 6), "\u{4F60}\u{597D}");
        assert_eq!(buf.text_range(6, 12), "\u{4E16}\u{754C}");
        assert_eq!(buf.text_range(3, 9), "\u{597D}\u{4E16}");
    }

    #[test]
    fn mixed_ascii_and_multibyte() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello\u{4E16}\u{754C}!");
        // "hello世界!" — 5 + 3 + 3 + 1 = 12 bytes, 8 chars
        assert_eq!(buf.len(), 12);
        assert_eq!(buf.char_count(), 8);

        buf.insert_str(5, " ");
        assert_eq!(buf.to_string(), "hello \u{4E16}\u{754C}!");
        assert_eq!(buf.len(), 13);

        buf.delete_range(6, 12); // delete "世界"
        assert_eq!(buf.to_string(), "hello !");
    }

    // -----------------------------------------------------------------------
    // byte_to_char / char_to_byte
    // -----------------------------------------------------------------------

    #[test]
    fn byte_char_roundtrip_ascii() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hello");
        for i in 0..=5 {
            assert_eq!(buf.byte_to_char(i), i);
            assert_eq!(buf.char_to_byte(i), i);
        }
    }

    #[test]
    fn byte_to_char_cjk() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("\u{4F60}\u{597D}\u{4E16}"); // 你好世
        assert_eq!(buf.byte_to_char(0), 0);
        assert_eq!(buf.byte_to_char(3), 1);
        assert_eq!(buf.byte_to_char(6), 2);
        assert_eq!(buf.byte_to_char(9), 3);
    }

    #[test]
    fn char_to_byte_cjk() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("\u{4F60}\u{597D}\u{4E16}"); // 你好世
        assert_eq!(buf.char_to_byte(0), 0);
        assert_eq!(buf.char_to_byte(1), 3);
        assert_eq!(buf.char_to_byte(2), 6);
        assert_eq!(buf.char_to_byte(3), 9);
    }

    #[test]
    fn byte_char_roundtrip_mixed() {
        crate::test_utils::init_test_tracing();
        // "a你b" — byte offsets: a=0, 你=1..4, b=4
        let buf = GapBuffer::from_str("a\u{4F60}b");
        assert_eq!(buf.byte_to_char(0), 0); // before 'a'
        assert_eq!(buf.byte_to_char(1), 1); // before '你'
        assert_eq!(buf.byte_to_char(4), 2); // before 'b'
        assert_eq!(buf.byte_to_char(5), 3); // end

        assert_eq!(buf.char_to_byte(0), 0);
        assert_eq!(buf.char_to_byte(1), 1);
        assert_eq!(buf.char_to_byte(2), 4);
        assert_eq!(buf.char_to_byte(3), 5);
    }

    #[test]
    fn byte_char_conversion_with_gap_in_middle() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("a\u{4F60}b\u{597D}c");
        // Move gap to middle of the text.
        buf.move_gap_to(4); // between 你 and b
        // Conversions should be unaffected by gap position.
        assert_eq!(buf.byte_to_char(0), 0);
        assert_eq!(buf.byte_to_char(1), 1);
        assert_eq!(buf.byte_to_char(4), 2);
        assert_eq!(buf.byte_to_char(5), 3);
        assert_eq!(buf.byte_to_char(8), 4);

        assert_eq!(buf.char_to_byte(0), 0);
        assert_eq!(buf.char_to_byte(1), 1);
        assert_eq!(buf.char_to_byte(2), 4);
        assert_eq!(buf.char_to_byte(3), 5);
        assert_eq!(buf.char_to_byte(4), 8);
    }

    #[test]
    fn byte_char_conversion_empty() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::new();
        assert_eq!(buf.byte_to_char(0), 0);
        assert_eq!(buf.char_to_byte(0), 0);
    }

    #[test]
    fn byte_to_char_emoji() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("x\u{1F600}y"); // x😀y
        // byte offsets: x=0, 😀=1..5, y=5
        assert_eq!(buf.byte_to_char(0), 0);
        assert_eq!(buf.byte_to_char(1), 1);
        assert_eq!(buf.byte_to_char(5), 2);
        assert_eq!(buf.byte_to_char(6), 3);
    }

    // -----------------------------------------------------------------------
    // Edge cases
    // -----------------------------------------------------------------------

    #[test]
    fn repeated_insert_delete_cycle() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::new();
        for i in 0..100 {
            let s = format!("{i}");
            buf.insert_str(buf.len(), &s);
        }
        let full = buf.to_string();
        assert!(!full.is_empty());

        // Delete everything one byte at a time from the front.
        while !buf.is_empty() {
            buf.delete_range(0, 1);
        }
        assert!(buf.is_empty());
        assert_eq!(buf.to_string(), "");
    }

    #[test]
    fn gap_moves_correctly_after_multiple_operations() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("the quick brown fox");
        buf.delete_range(4, 10); // delete "quick "
        assert_eq!(buf.to_string(), "the brown fox");
        buf.insert_str(4, "slow ");
        assert_eq!(buf.to_string(), "the slow brown fox");
        buf.delete_range(9, 15); // delete "brown "
        assert_eq!(buf.to_string(), "the slow fox");
        buf.insert_str(9, "red ");
        assert_eq!(buf.to_string(), "the slow red fox");
    }

    #[test]
    fn insert_at_every_position() {
        crate::test_utils::init_test_tracing();
        for pos in 0..=5 {
            let mut buf = GapBuffer::from_str("hello");
            buf.insert_str(pos, "X");
            assert_eq!(buf.len(), 6);
            assert_eq!(buf.byte_at(pos), b'X');
        }
    }

    #[test]
    fn display_trait() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("display test");
        let s = format!("{buf}");
        assert_eq!(s, "display test");
    }

    #[test]
    fn debug_trait_contains_text() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("dbg");
        let dbg = format!("{buf:?}");
        assert!(dbg.contains("dbg"));
        assert!(dbg.contains("GapBuffer"));
    }

    #[test]
    fn default_is_empty() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::default();
        assert!(buf.is_empty());
    }

    #[test]
    fn clone_is_independent() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("original");
        let clone = buf.clone();
        buf.insert_str(0, "X");
        assert_eq!(buf.to_string(), "Xoriginal");
        assert_eq!(clone.to_string(), "original");
    }

    #[test]
    #[should_panic]
    fn insert_past_end_panics() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hi");
        buf.insert_str(3, "x");
    }

    #[test]
    #[should_panic]
    fn delete_past_end_panics() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hi");
        buf.delete_range(0, 3);
    }

    #[test]
    #[should_panic]
    fn delete_inverted_range_panics() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hello");
        buf.delete_range(3, 1);
    }

    #[test]
    #[should_panic]
    fn text_range_past_end_panics() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hi");
        buf.text_range(0, 3);
    }

    #[test]
    #[should_panic]
    fn move_gap_past_end_panics() {
        crate::test_utils::init_test_tracing();
        let mut buf = GapBuffer::from_str("hi");
        buf.move_gap_to(3);
    }

    #[test]
    #[should_panic]
    fn byte_to_char_past_end_panics() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hi");
        buf.byte_to_char(3);
    }

    #[test]
    fn char_to_byte_past_end_clamps() {
        crate::test_utils::init_test_tracing();
        let buf = GapBuffer::from_str("hi");
        // char_to_byte clamps to buffer end instead of panicking
        // when char_pos exceeds char_count (for stale positions).
        assert_eq!(buf.char_to_byte(3), buf.len());
        assert_eq!(buf.char_to_byte(100), buf.len());
    }

    // -----------------------------------------------------------------------
    // copy_bytes_to
    // -----------------------------------------------------------------------

    #[test]
    fn copy_bytes_to_basic() {
        crate::test_utils::init_test_tracing();
        let gb = GapBuffer::from_str("Hello, world!");
        let mut out = Vec::new();
        gb.copy_bytes_to(0, 5, &mut out);
        assert_eq!(&out, b"Hello");

        gb.copy_bytes_to(7, 13, &mut out);
        assert_eq!(&out, b"world!");
    }

    #[test]
    fn copy_bytes_to_spanning_gap() {
        crate::test_utils::init_test_tracing();
        let mut gb = GapBuffer::from_str("abcdef");
        gb.move_gap_to(3); // gap after "abc"
        let mut out = Vec::new();
        gb.copy_bytes_to(1, 5, &mut out); // "bcde" — spans gap
        assert_eq!(&out, b"bcde");
    }

    #[test]
    fn copy_bytes_to_empty_range() {
        crate::test_utils::init_test_tracing();
        let gb = GapBuffer::from_str("test");
        let mut out = vec![1, 2, 3]; // pre-existing contents
        gb.copy_bytes_to(2, 2, &mut out);
        assert!(out.is_empty());
    }
}