ocomment-core 0.1.0

Byte-oriented comment scanning and transformation engine
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
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
use crate::{
    ByteSpan, Comment, CommentKind, Edit, ExternalSpanError, Language, Layout, PreparedScanner,
    ScanReport, SourceMap, TransformOptions, TransformPlan, TransformResult,
    scanner::{
        disposition, keep_yaml_structural_trails, lines_a_removal_must_swallow, scan,
        unicode_line_terminator_width,
    },
};
use unicode_width::UnicodeWidthChar;

/// Scan `source` and produce the bytes a removal would write.
///
/// This is [`scan`] followed by the edits its report calls for. Nothing is
/// written anywhere: the caller gets the new bytes, the edits that made them,
/// the report they were decided from, and a
/// [`SourceMap`](crate::SourceMap) between the two.
///
/// A source the scanner reported invalid — an unterminated comment or string —
/// is returned byte for byte with no edits at all, unless
/// [`ScanOptions::force_invalid`](crate::ScanOptions::force_invalid) is set.
///
/// # Examples
///
/// ```
/// use ocomment_core::{Language, TransformOptions, transform};
///
/// let result = transform(
///     b"let x = 1; // note\n",
///     Language::Rust,
///     TransformOptions::default(),
/// );
/// assert_eq!(result.output, b"let x = 1; \n");
/// assert_eq!(result.report.comments.len(), 1);
/// assert_eq!(result.edits.len(), 1);
///
/// // An unterminated comment leaves the file alone.
/// let broken = transform(b"x /* no end", Language::C, TransformOptions::default());
/// assert!(!broken.report.valid);
/// assert_eq!(broken.output, b"x /* no end");
/// ```
pub fn transform(source: &[u8], language: Language, options: TransformOptions) -> TransformResult {
    transform_plan(source, language, options).finish(source)
}

/// Scan `source` and compute its edits without building output bytes or a
/// source map.
///
/// This is the lazy counterpart of [`transform`]. It is useful for checkers
/// and report writers that only need the report or edit list.
pub fn transform_plan(
    source: &[u8],
    language: Language,
    options: TransformOptions,
) -> TransformPlan {
    let force_invalid = options.scan.force_invalid;
    let report = scan(source, language, options.scan);
    plan_report(source, report, options.layout, force_invalid)
}

impl PreparedScanner {
    /// Scan and plan edits with this scanner's already-compiled policy.
    pub fn transform_plan(
        &self,
        source: &[u8],
        language: Language,
        layout: Layout,
    ) -> TransformPlan {
        let report = self.scan(source, language);
        plan_report(source, report, layout, self.options().force_invalid)
    }

    /// Produce a complete transformation with this scanner's compiled policy.
    pub fn transform(&self, source: &[u8], language: Language, layout: Layout) -> TransformResult {
        self.transform_plan(source, language, layout).finish(source)
    }

    /// Validate externally supplied spans and plan their edits with this
    /// scanner's already-compiled policy.
    pub fn transform_spans_plan(
        &self,
        source: &[u8],
        language: Language,
        spans: &[(ByteSpan, CommentKind)],
        layout: Layout,
    ) -> Result<TransformPlan, ExternalSpanError> {
        let report = self.scan_spans(source, language, spans)?;
        Ok(plan_report(
            source,
            report,
            layout,
            self.options().force_invalid,
        ))
    }

    /// Validate and classify externally supplied spans without planning edits.
    pub fn scan_spans(
        &self,
        source: &[u8],
        language: Language,
        spans: &[(ByteSpan, CommentKind)],
    ) -> Result<ScanReport, ExternalSpanError> {
        external_report(source, language, spans, self)
    }
}

/// Transform a scanner's already-classified comment spans using the same
/// policy, layout, edit validation, and source-map engine as built-in scans.
///
/// This is the safe hand-off point for declarative or WASM scanners. Spans
/// must be non-empty, sorted, non-overlapping, and contained in `source`.
///
/// The report that comes back carries no diagnostics and is always valid: the
/// external scanner, not this crate, judged whether the source lexed.
///
/// # Errors
///
/// Returns [`ExternalSpanError`] naming the first span that reaches past the
/// end of `source`, covers no bytes, or starts before its predecessor ends,
/// or reporting a `keep_regex`/`remove_regex` entry that would not compile.
/// Nothing is transformed when validation fails.
///
/// # Examples
///
/// ```
/// use ocomment_core::{
///     ByteSpan, CommentKind, ExternalSpanError, Language, TransformOptions, transform_spans,
/// };
///
/// let source = b"a/* ordinary */b/* directive */";
/// let result = transform_spans(
///     source,
///     Language::Unknown,
///     &[
///         (ByteSpan::new(1, 15), CommentKind::Block),
///         (ByteSpan::new(16, source.len()), CommentKind::Directive),
///     ],
///     TransformOptions::default(),
/// )
/// .unwrap();
/// // The same policy the built-in scanners get: the directive is kept.
/// assert_eq!(result.output, b"a b/* directive */");
///
/// let bad = transform_spans(
///     source,
///     Language::Unknown,
///     &[(ByteSpan::new(2, source.len() + 1), CommentKind::Block)],
///     TransformOptions::default(),
/// );
/// assert!(matches!(bad, Err(ExternalSpanError::OutOfBounds { .. })));
/// ```
pub fn transform_spans(
    source: &[u8],
    language: Language,
    spans: &[(ByteSpan, CommentKind)],
    options: TransformOptions,
) -> Result<TransformResult, ExternalSpanError> {
    let prepared = PreparedScanner::new(options.scan)
        .map_err(|error| ExternalSpanError::InvalidPattern(error.to_string()))?;
    Ok(prepared
        .transform_spans_plan(source, language, spans, options.layout)?
        .finish(source))
}

fn external_report(
    source: &[u8],
    language: Language,
    spans: &[(ByteSpan, CommentKind)],
    prepared: &PreparedScanner,
) -> Result<ScanReport, ExternalSpanError> {
    let mut cursor = 0;
    let mut comments = Vec::with_capacity(spans.len());
    for (index, (span, kind)) in spans.iter().copied().enumerate() {
        if span.start > span.end || span.end > source.len() {
            return Err(ExternalSpanError::OutOfBounds {
                index,
                source_len: source.len(),
            });
        }
        if span.is_empty() {
            return Err(ExternalSpanError::Empty { index });
        }
        if index > 0 && span.start < cursor {
            return Err(ExternalSpanError::OrderOrOverlap { index });
        }
        cursor = span.end;
        comments.push(Comment {
            span,
            kind,
            disposition: disposition(
                kind,
                prepared.options(),
                &source[span.start..span.end],
                &prepared.patterns,
            ),
        });
    }
    /* NOTE: The one verdict a comment's own bytes cannot reach, so it is
     * applied to the hand-off as a built-in scan applies it: a YAML block
     * scalar leaning on the comment that ends it keeps that comment, whoever
     * found it. Without this the report would promise a removal that
     * `lines_a_removal_must_swallow` cannot make safe. */
    keep_yaml_structural_trails(source, language, &mut comments);
    Ok(ScanReport {
        language,
        comments,
        diagnostics: Vec::new(),
        valid: true,
    })
}

pub(crate) fn transform_report(
    source: &[u8],
    report: crate::ScanReport,
    options: TransformOptions,
) -> TransformResult {
    plan_report(source, report, options.layout, options.scan.force_invalid).finish(source)
}

pub(crate) fn plan_report(
    source: &[u8],
    report: crate::ScanReport,
    layout: Layout,
    force_invalid: bool,
) -> TransformPlan {
    let edits = if report.valid || force_invalid {
        /* NOTE: The one hole whose own bytes carry meaning, so every layout has
         * to be told where not to leave one. `compact` takes the line already;
         * what it does not know on its own is how far past the line to go
         * under a `|+` body. */
        let swallow = lines_a_removal_must_swallow(source, report.language, &report.comments);
        match layout {
            Layout::Lines => line_edits(source, &report.comments, &swallow),
            Layout::Columns => column_edits(source, &report.comments, &swallow),
            Layout::Compact => compact_edits(source, &report.comments, &swallow),
        }
    } else {
        Vec::new()
    };
    TransformPlan { edits, report }
}

impl TransformPlan {
    /// Apply this plan and build its source map.
    ///
    /// # Panics
    ///
    /// Panics when `source` is not the source the plan was made for and an
    /// edit consequently lies outside it, just like [`apply_edits`].
    pub fn finish(self, source: &[u8]) -> TransformResult {
        let output = apply_edits(source, &self.edits);
        let source_map = SourceMap::from_edits(source.len(), &self.edits);
        TransformResult {
            output,
            edits: self.edits,
            report: self.report,
            source_map,
        }
    }

    /// Build only the transformed bytes, leaving the plan available.
    pub fn output(&self, source: &[u8]) -> Vec<u8> {
        apply_edits(source, &self.edits)
    }

    /// Build only the source map, leaving the plan available.
    pub fn source_map(&self, source_len: usize) -> SourceMap {
        SourceMap::from_edits(source_len, &self.edits)
    }
}

/// Apply sorted, non-overlapping half-open edits.
///
/// The bytes outside the edited spans are copied through untouched, which is
/// what makes a transformation byte-preserving: a BOM, CRLF line endings, a
/// missing final newline, and bytes that are not UTF-8 at all all survive.
///
/// # Panics
///
/// Panics if an edit has `start > end`, starts before its predecessor ends, or
/// reaches past the end of `source`. The edits of a
/// [`TransformResult`] always satisfy that contract; edits assembled by hand
/// have to be sorted first.
///
/// # Examples
///
/// ```
/// use ocomment_core::{ByteSpan, Edit, Language, TransformOptions, apply_edits, transform};
///
/// let edits = [Edit {
///     span: ByteSpan::new(3, 8),
///     replacement: b"there".to_vec(),
/// }];
/// assert_eq!(apply_edits(b"hi world", &edits), b"hi there");
///
/// // Re-applying a transformation's own edits reproduces its output.
/// let source = b"let x = 1; // note\n";
/// let result = transform(source, Language::Rust, TransformOptions::default());
/// assert_eq!(apply_edits(source, &result.edits), result.output);
/// ```
pub fn apply_edits(source: &[u8], edits: &[Edit]) -> Vec<u8> {
    let mut cursor = 0;
    let output_len = edits.iter().fold(source.len(), |length, edit| {
        length
            .saturating_sub(edit.span.len())
            .saturating_add(edit.replacement.len())
    });
    let mut output = Vec::with_capacity(output_len);
    for edit in edits {
        assert!(
            edit.span.start <= edit.span.end,
            "edit has an inverted span"
        );
        assert!(edit.span.start >= cursor, "edits overlap or are not sorted");
        assert!(edit.span.end <= source.len(), "edit is outside the source");
        output.extend_from_slice(&source[cursor..edit.span.start]);
        output.extend_from_slice(&edit.replacement);
        cursor = edit.span.end;
    }
    output.extend_from_slice(&source[cursor..]);
    output
}

/// The edits [`Layout::Lines`] makes: one per removed comment, over exactly
/// the bytes that comment covers — save where `swallow` names a whole line,
/// because there the hole itself would say something (see
/// [`lines_a_removal_must_swallow`]). This is the layout that promises line
/// numbers and those lines are the one place it cannot keep that promise.
fn line_edits(source: &[u8], comments: &[Comment], swallow: &[Option<ByteSpan>]) -> Vec<Edit> {
    let mut edits = Vec::new();
    let mut floor = 0usize;
    for (index, comment) in comments.iter().enumerate() {
        if !comment.disposition.is_remove() {
            continue;
        }
        let edit = match swallow.get(index).copied().flatten() {
            Some(line) => Edit {
                span: ByteSpan::new(line.start.max(floor), line.end),
                replacement: Vec::new(),
            },
            None => Edit {
                span: comment.span,
                replacement: if comment.kind == CommentKind::HtmlComment {
                    Vec::new()
                } else {
                    line_replacement(source, comment.span)
                },
            },
        };
        floor = edit.span.end;
        edits.push(edit);
    }
    edits
}

/// What [`Layout::Lines`] leaves in place of a removed comment: the line
/// terminators the comment spanned, so every following line keeps its number,
/// and a single space when the comment was all that kept two tokens apart. A
/// comment that spanned a terminator needs no space of its own, because a
/// newline is a lexical separator already.
fn line_replacement(source: &[u8], span: ByteSpan) -> Vec<u8> {
    let mut output = newline_sequence(&source[span.start..span.end]);
    if output.is_empty() && has_non_whitespace_neighbors(source, span) {
        output.push(b' ');
    }
    output
}

/// The edits [`Layout::Columns`] makes: one per removed comment, of spaces as
/// wide as the comment was — save where `swallow` names a line, because a line
/// of spaces under a YAML block scalar body is indented into it (see
/// [`lines_a_removal_must_swallow`]). This is the layout that promises columns
/// and those lines are the one place it cannot keep that promise.
///
/// The display column is threaded from one edit to the next so every source
/// byte is inspected at most once. It also reflects an explicitly removed HTML
/// comment: because that edit emits no bytes, the newlines it covered do not
/// move the column the edits after it are measured from.
fn column_edits(source: &[u8], comments: &[Comment], swallow: &[Option<ByteSpan>]) -> Vec<Edit> {
    let mut edits = Vec::new();
    let mut cursor = 0usize;
    let mut column = 0usize;
    for (index, comment) in comments.iter().enumerate() {
        if !comment.disposition.is_remove() {
            continue;
        }
        /* NOTE: A swallowed line takes its terminator with it, so what follows
         * starts a line of its own in the output as it did in the source and
         * the column count begins again there. */
        if let Some(line) = swallow.get(index).copied().flatten() {
            let span = ByteSpan::new(line.start.max(cursor), line.end);
            cursor = span.end;
            column = 0;
            edits.push(Edit {
                span,
                replacement: Vec::new(),
            });
            continue;
        }
        column = advance_display_column(source, cursor, comment.span.start, column);
        let (replacement, next) = if comment.kind == CommentKind::HtmlComment {
            (Vec::new(), column)
        } else {
            column_replacement(source, comment.span, column)
        };
        cursor = comment.span.end;
        column = next;
        edits.push(Edit {
            span: comment.span,
            replacement,
        });
    }
    edits
}

/// The edits [`Layout::Compact`] makes: [`Layout::Lines`], plus the promise
/// that a line which held nothing but a removed comment goes away instead of
/// staying behind as a blank one.
///
/// Whether a comment was alone on its line is judged from the bytes of the
/// original source, so a line holding two comments and nothing else keeps its
/// terminator: neither of them was alone on it.
///
/// The start of the current line is tracked forward through the whole source,
/// comment bodies included, so a comment beginning on a line that an earlier
/// comment ended is still measured from that line's real beginning.
///
/// `swallow` names the lines whose hole would carry meaning, and it reaches
/// further than a line: under a `|+` body it takes the empty lines the comment
/// was sheltering too (see [`lines_a_removal_must_swallow`]). Taking the line
/// is what `compact` does anyway, so this only ever widens what it takes, and
/// it is what keeps all three layouts writing the same bytes there.
fn compact_edits(source: &[u8], comments: &[Comment], swallow: &[Option<ByteSpan>]) -> Vec<Edit> {
    let mut edits = Vec::new();
    let mut scan = 0usize;
    let mut line_start = 0usize;
    let mut floor = 0usize;
    for (index, comment) in comments.iter().enumerate() {
        if !comment.disposition.is_remove() {
            continue;
        }
        if let Some(line) = swallow.get(index).copied().flatten() {
            let span = ByteSpan::new(line.start.max(floor), line.end.max(floor));
            floor = span.end;
            scan = span.end;
            line_start = span.end;
            edits.push(Edit {
                span,
                replacement: Vec::new(),
            });
            continue;
        }
        while scan < comment.span.start {
            match unicode_line_terminator_width(source, scan) {
                Some(width) if scan + width <= comment.span.start => {
                    scan += width;
                    line_start = scan;
                }
                _ => scan += 1,
            }
        }
        /* NOTE: The next comment of any disposition, kept ones included: the
         * blanks an edit swallows must never reach into one. */
        let ceiling = comments
            .get(index + 1)
            .map_or(source.len(), |next| next.span.start)
            .max(comment.span.end);
        let edit = compact_edit(source, comment, line_start, floor, ceiling);
        floor = edit.span.end;
        edits.push(edit);
    }
    edits
}

/// One [`Layout::Compact`] edit.
///
/// `line_start` is where the line holding `comment` begins, `floor` is the end
/// of the previous edit and `ceiling` the start of the next comment, so the
/// span that comes back is sorted and non-overlapping with its neighbours
/// however a scanner laid the comments out.
fn compact_edit(
    source: &[u8],
    comment: &Comment,
    line_start: usize,
    floor: usize,
    ceiling: usize,
) -> Edit {
    let span = comment.span;
    /* NOTE: An HTML comment closes up completely under every layout, the
     * newlines it spanned included, so it never counts as ending a line by
     * spanning one and never puts a terminator back. */
    let html = comment.kind == CommentKind::HtmlComment;
    let interior = first_line_terminator(source, span);
    let tail = line_tail(source, span.end);
    let head_code = source[line_start..span.start]
        .iter()
        .any(|byte| !byte.is_ascii_whitespace());
    let ends_the_line = tail.is_some() || (interior.is_some() && !html);
    let start = if ends_the_line {
        blank_start(source, span.start, floor.max(line_start))
    } else {
        span.start
    };
    let eats_the_terminator = if html {
        !head_code
    } else {
        interior.is_some() || !head_code
    };
    let end = match tail {
        Some((blanks, terminator)) => blanks + if eats_the_terminator { terminator } else { 0 },
        None => span.end,
    };
    let replacement = if html {
        Vec::new()
    } else if !ends_the_line {
        /* NOTE: An interior comment: the line goes on after it, so keeping the
         * two tokens either side apart is the whole story, exactly as under
         * `lines`. */
        line_replacement(source, span)
    } else if let Some(terminator) = interior.filter(|_| head_code) {
        /* NOTE: The code before the comment keeps its own line, and the
         * terminator that ended that line was inside the comment. */
        terminator.to_vec()
    } else {
        /* NOTE: Nothing that survives on this line follows the comment, so the
         * line terminator - the one kept after it or the one that ended the
         * code line - is separator enough. */
        Vec::new()
    };
    Edit {
        span: ByteSpan::new(start, end.min(ceiling)),
        replacement,
    }
}

/// The first line terminator inside a comment, as the bytes that wrote it, so
/// a CRLF file keeps its CRLF. A terminator that would reach past the end of
/// the comment is not one: the same rule [`newline_sequence`] applies.
fn first_line_terminator(source: &[u8], span: ByteSpan) -> Option<&[u8]> {
    let mut index = span.start;
    while index < span.end {
        match unicode_line_terminator_width(source, index) {
            Some(width) if index + width <= span.end => return Some(&source[index..index + width]),
            _ => index += 1,
        }
    }
    None
}

/// How the line a comment ended on runs out: where the blanks after the
/// comment stop, and how wide the line terminator there is — `0` at the end of
/// the source. `None` when something other than blanks follows on that line,
/// which is what makes the comment an interior one rather than the last thing
/// on its line.
fn line_tail(source: &[u8], from: usize) -> Option<(usize, usize)> {
    let mut index = from;
    loop {
        if let Some(width) = unicode_line_terminator_width(source, index) {
            return Some((index, width));
        }
        match source.get(index) {
            None => return Some((index, 0)),
            Some(byte) if byte.is_ascii_whitespace() => index += 1,
            Some(_) => return None,
        }
    }
}

/// Where the run of blanks that ends at `at` begins. It never reaches before
/// `floor` and never crosses a line terminator, so trimming what a removal
/// left at the end of a line can never touch the line before it.
fn blank_start(source: &[u8], at: usize, floor: usize) -> usize {
    let mut index = at;
    while index > floor
        && source[index - 1].is_ascii_whitespace()
        && unicode_line_terminator_width(source, index - 1).is_none()
    {
        index -= 1;
    }
    index
}

fn newline_sequence(bytes: &[u8]) -> Vec<u8> {
    let mut output = Vec::new();
    let mut index = 0;
    while index < bytes.len() {
        if let Some(width) = unicode_line_terminator_width(bytes, index) {
            output.extend_from_slice(&bytes[index..index + width]);
            index += width;
        } else {
            index += 1;
        }
    }
    output
}

fn has_non_whitespace_neighbors(source: &[u8], span: ByteSpan) -> bool {
    source
        .get(span.start.wrapping_sub(1))
        .is_some_and(|byte| !byte.is_ascii_whitespace())
        && source
            .get(span.end)
            .is_some_and(|byte| !byte.is_ascii_whitespace())
}

fn column_replacement(source: &[u8], span: ByteSpan, mut column: usize) -> (Vec<u8>, usize) {
    let mut output = Vec::with_capacity(span.len());
    let mut index = span.start;
    while index < span.end {
        if let Some(width) = unicode_line_terminator_width(source, index)
            && index + width <= span.end
        {
            output.extend_from_slice(&source[index..index + width]);
            index += width;
            column = 0;
            continue;
        }
        match source[index] {
            b'\t' => {
                let width = 8 - (column % 8);
                output.extend(std::iter::repeat_n(b' ', width));
                column += width;
                index += 1;
            }
            byte if byte.is_ascii() => {
                output.push(b' ');
                column += 1;
                index += 1;
            }
            _ => {
                if let Some((character, length)) = utf8_character(source, index, span.end) {
                    let width = character.width().unwrap_or(0);
                    output.extend(std::iter::repeat_n(b' ', width));
                    column += width;
                    index += length;
                } else {
                    // NOTE: Invalid source bytes each occupy one conservative display column.
                    output.push(b' ');
                    column += 1;
                    index += 1;
                }
            }
        }
    }
    (output, column)
}

fn advance_display_column(source: &[u8], mut index: usize, end: usize, mut column: usize) -> usize {
    while index < end {
        if let Some(width) = unicode_line_terminator_width(source, index)
            && index + width <= end
        {
            index += width;
            column = 0;
            continue;
        }
        if source[index] == b'\t' {
            column += 8 - (column % 8);
            index += 1;
        } else if source[index].is_ascii() {
            column += 1;
            index += 1;
        } else if let Some((character, length)) = utf8_character(source, index, end) {
            column += character.width().unwrap_or(0);
            index += length;
        } else {
            column += 1;
            index += 1;
        }
    }
    column
}

fn utf8_character(source: &[u8], index: usize, end: usize) -> Option<(char, usize)> {
    let length = match *source.get(index)? {
        0xc2..=0xdf => 2,
        0xe0..=0xef => 3,
        0xf0..=0xf4 => 4,
        _ => return None,
    };
    let bytes = source.get(index..index.checked_add(length)?)?;
    if index + length > end {
        return None;
    }
    let text = std::str::from_utf8(bytes).ok()?;
    Some((text.chars().next()?, length))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Policy, ScanOptions};
    use proptest::prelude::*;

    #[test]
    fn preserves_crlf_and_separates_tokens() {
        let result = transform(b"a/* x\r\ny */b", Language::C, TransformOptions::default());
        assert_eq!(result.output, b"a\r\nb");
        let joined = transform(b"a/*x*/b", Language::C, TransformOptions::default());
        assert_eq!(joined.output, b"a b");
    }

    #[test]
    fn invalid_input_is_not_edited_without_force() {
        let result = transform(b"x /* no end", Language::C, TransformOptions::default());
        assert!(!result.report.valid);
        assert!(result.edits.is_empty());
        assert_eq!(result.output, b"x /* no end");
    }

    #[test]
    fn external_spans_use_the_normal_policy_and_validate_boundaries() {
        let source = b"a/* ordinary */b/* directive */";
        let result = transform_spans(
            source,
            Language::Unknown,
            &[
                (ByteSpan::new(1, 15), CommentKind::Block),
                (ByteSpan::new(16, source.len()), CommentKind::Directive),
            ],
            TransformOptions::default(),
        )
        .unwrap();
        assert_eq!(result.output, b"a b/* directive */");
        assert!(matches!(
            transform_spans(
                source,
                Language::Unknown,
                &[(ByteSpan::new(2, source.len() + 1), CommentKind::Block)],
                TransformOptions::default(),
            ),
            Err(ExternalSpanError::OutOfBounds { .. })
        ));
    }

    #[test]
    fn source_map_prefers_the_following_segment_at_edit_boundaries() {
        let source = b"ab/* remove */cd";
        let result = transform(source, Language::C, TransformOptions::default());
        let edit = &result.edits[0];
        assert_eq!(result.source_map.original_to_output(0), Some(0));
        assert_eq!(
            result.source_map.original_to_output(edit.span.start),
            Some(edit.span.start)
        );
        assert_eq!(
            result.source_map.original_to_output(edit.span.end),
            Some(edit.span.start + edit.replacement.len())
        );
        assert_eq!(
            result.source_map.original_to_output(source.len()),
            Some(result.output.len())
        );
        assert_eq!(
            result.source_map.output_to_original(result.output.len()),
            Some(source.len())
        );
    }

    #[test]
    fn html_is_byte_identical_in_safe_mode() {
        let input = b"a<!-- visible\ncomment -->b";
        assert_eq!(
            transform(input, Language::Html, TransformOptions::default()).output,
            input
        );
        let options = TransformOptions {
            scan: ScanOptions {
                policy: Policy::All,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_eq!(transform(input, Language::Html, options).output, b"ab");
    }

    proptest! {
        #[test]
        fn transform_is_idempotent(left in "[a-z ]{0,30}", body in "[a-z ]{0,30}", right in "[a-z ]{0,30}") {
            let input = format!("{left}/*{body}*/{right}").into_bytes();
            let first = transform(&input, Language::C, TransformOptions::default()).output;
            let second = transform(&first, Language::C, TransformOptions::default()).output;
            prop_assert_eq!(first, second);
        }
    }
}