fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
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
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
use std::io::Write;

/// Fold (wrap) lines to a given width.
///
/// Modes:
/// - `bytes` mode (-b): count bytes, break at byte boundaries
/// - default mode: count columns (tab = advance to next tab stop, backspace = decrement)
///
/// If `spaces` (-s): break at the last space within the width instead of mid-word.
pub fn fold_bytes(
    data: &[u8],
    width: usize,
    count_bytes: bool,
    break_at_spaces: bool,
    out: &mut impl Write,
) -> std::io::Result<()> {
    if data.is_empty() {
        return Ok(());
    }

    if width == 0 {
        return fold_width_zero(data, out);
    }

    // Fast path: byte mode, use SIMD-accelerated scanning
    if count_bytes {
        if break_at_spaces {
            return fold_byte_fast_spaces(data, width, out);
        } else {
            return fold_byte_fast(data, width, out);
        }
    }

    // Column mode without tabs: byte mode is equivalent (on glibc)
    if memchr::memchr(b'\t', data).is_none() {
        if break_at_spaces {
            return fold_byte_fast_spaces(data, width, out);
        } else {
            return fold_byte_fast(data, width, out);
        }
    }

    fold_column_mode_streaming(data, width, break_at_spaces, out)
}

/// Width 0: GNU fold behavior — each byte becomes a newline.
fn fold_width_zero(data: &[u8], out: &mut impl Write) -> std::io::Result<()> {
    let output = vec![b'\n'; data.len()];
    out.write_all(&output)
}

/// Parallel threshold for fold byte mode.
const FOLD_BYTE_PARALLEL_THRESHOLD: usize = 32 * 1024 * 1024;

/// Fast fold by byte count without -s flag.
/// Uses unsafe pointer copies and a pre-allocated 1MB output buffer.
/// For files >= 32MB, uses rayon parallel processing on line-aligned chunks.
fn fold_byte_fast(data: &[u8], width: usize, out: &mut impl Write) -> std::io::Result<()> {
    if data.len() >= FOLD_BYTE_PARALLEL_THRESHOLD {
        return fold_byte_fast_parallel(data, width, out);
    }

    const BUF_CAP: usize = 1024 * 1024 + 4096;
    let mut buf: Vec<u8> = Vec::with_capacity(BUF_CAP);
    let src = data.as_ptr();
    let mut wp: usize = 0;
    let mut base = buf.as_mut_ptr();
    let mut seg_start = 0usize;

    for nl_pos in memchr::memchr_iter(b'\n', data) {
        let seg_len = nl_pos - seg_start;

        if seg_len <= width {
            let total = seg_len + 1;
            if wp + total > BUF_CAP {
                unsafe { buf.set_len(wp) };
                out.write_all(&buf)?;
                buf.clear();
                wp = 0;
                base = buf.as_mut_ptr();
            }
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(seg_start), base.add(wp), total);
            }
            wp += total;
        } else {
            let mut off = seg_start;
            let end = nl_pos;
            while off + width < end {
                let chunk = width + 1;
                if wp + chunk > BUF_CAP {
                    unsafe { buf.set_len(wp) };
                    out.write_all(&buf)?;
                    buf.clear();
                    wp = 0;
                    base = buf.as_mut_ptr();
                }
                unsafe {
                    std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), width);
                    *base.add(wp + width) = b'\n';
                }
                wp += chunk;
                off += width;
            }
            let rem = end - off + 1;
            if wp + rem > BUF_CAP {
                unsafe { buf.set_len(wp) };
                out.write_all(&buf)?;
                buf.clear();
                wp = 0;
                base = buf.as_mut_ptr();
            }
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), rem);
            }
            wp += rem;
        }
        seg_start = nl_pos + 1;
    }

    if seg_start < data.len() {
        let mut off = seg_start;
        let end = data.len();
        while off + width < end {
            let chunk = width + 1;
            if wp + chunk > BUF_CAP {
                unsafe { buf.set_len(wp) };
                out.write_all(&buf)?;
                buf.clear();
                wp = 0;
                base = buf.as_mut_ptr();
            }
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), width);
                *base.add(wp + width) = b'\n';
            }
            wp += chunk;
            off += width;
        }
        if off < end {
            let rem = end - off;
            if wp + rem > BUF_CAP {
                unsafe { buf.set_len(wp) };
                out.write_all(&buf)?;
                buf.clear();
                wp = 0;
                base = buf.as_mut_ptr();
            }
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), rem);
            }
            wp += rem;
        }
    }

    if wp > 0 {
        unsafe { buf.set_len(wp) };
        out.write_all(&buf)?;
    }
    Ok(())
}

/// Parallel fold by byte count. Splits at newline boundaries, processes in parallel.
fn fold_byte_fast_parallel(data: &[u8], width: usize, out: &mut impl Write) -> std::io::Result<()> {
    use rayon::prelude::*;

    let num_chunks = rayon::current_num_threads().max(2);
    let target_chunk_size = data.len() / num_chunks;
    let mut chunks: Vec<&[u8]> = Vec::with_capacity(num_chunks + 1);
    let mut pos: usize = 0;

    for _ in 0..num_chunks - 1 {
        if pos >= data.len() {
            break;
        }
        let target_end = (pos + target_chunk_size).min(data.len());
        let chunk_end = if target_end >= data.len() {
            data.len()
        } else {
            match memchr::memchr(b'\n', &data[target_end..]) {
                Some(off) => target_end + off + 1,
                None => data.len(),
            }
        };
        chunks.push(&data[pos..chunk_end]);
        pos = chunk_end;
    }
    if pos < data.len() {
        chunks.push(&data[pos..]);
    }

    let results: Vec<Vec<u8>> = chunks
        .par_iter()
        .map(|chunk| {
            let mut buf = Vec::with_capacity(chunk.len() + chunk.len() / width + 256);
            fold_byte_chunk(chunk, width, &mut buf);
            buf
        })
        .collect();

    for result in &results {
        if !result.is_empty() {
            out.write_all(result)?;
        }
    }
    Ok(())
}

/// Process a chunk for fold byte mode into a Vec<u8>.
/// Uses unsafe pointer copies for maximum throughput.
fn fold_byte_chunk(data: &[u8], width: usize, buf: &mut Vec<u8>) {
    if data.is_empty() {
        return;
    }

    let needed = data.len() + data.len() / width + 256;
    buf.reserve(needed);
    let base = buf.as_mut_ptr();
    let src = data.as_ptr();
    let initial_len = buf.len();
    let mut wp: usize = initial_len;
    let mut seg_start = 0usize;

    for nl_pos in memchr::memchr_iter(b'\n', data) {
        let seg_len = nl_pos - seg_start;

        if seg_len <= width {
            let total = seg_len + 1;
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(seg_start), base.add(wp), total);
            }
            wp += total;
        } else {
            let mut off = seg_start;
            let end = nl_pos;
            while off + width < end {
                unsafe {
                    std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), width);
                    *base.add(wp + width) = b'\n';
                }
                wp += width + 1;
                off += width;
            }
            let rem = end - off + 1;
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), rem);
            }
            wp += rem;
        }
        seg_start = nl_pos + 1;
    }

    // Handle final segment without trailing newline
    if seg_start < data.len() {
        let mut off = seg_start;
        let end = data.len();
        while off + width < end {
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), width);
                *base.add(wp + width) = b'\n';
            }
            wp += width + 1;
            off += width;
        }
        if off < end {
            let rem = end - off;
            unsafe {
                std::ptr::copy_nonoverlapping(src.add(off), base.add(wp), rem);
            }
            wp += rem;
        }
    }

    unsafe {
        buf.set_len(wp);
    }
}

/// Fast fold by byte count with -s (break at spaces).
/// For files >= 32MB, uses rayon parallel processing.
fn fold_byte_fast_spaces(data: &[u8], width: usize, out: &mut impl Write) -> std::io::Result<()> {
    if data.len() >= FOLD_BYTE_PARALLEL_THRESHOLD {
        return fold_byte_spaces_parallel(data, width, out);
    }

    let mut outbuf: Vec<u8> = Vec::with_capacity(1024 * 1024 + 4096);
    let mut pos: usize = 0;

    for nl_pos in memchr::memchr_iter(b'\n', data) {
        let segment = &data[pos..nl_pos];
        fold_segment_bytes_spaces_buffered(segment, width, &mut outbuf);
        outbuf.push(b'\n');
        pos = nl_pos + 1;

        if outbuf.len() >= 1024 * 1024 {
            out.write_all(&outbuf)?;
            outbuf.clear();
        }
    }

    if pos < data.len() {
        fold_segment_bytes_spaces_buffered(&data[pos..], width, &mut outbuf);
    }

    if !outbuf.is_empty() {
        out.write_all(&outbuf)?;
    }
    Ok(())
}

/// Parallel fold by byte count with -s.
fn fold_byte_spaces_parallel(
    data: &[u8],
    width: usize,
    out: &mut impl Write,
) -> std::io::Result<()> {
    use rayon::prelude::*;

    let num_chunks = rayon::current_num_threads().max(2);
    let target_chunk_size = data.len() / num_chunks;
    let mut chunks: Vec<&[u8]> = Vec::with_capacity(num_chunks + 1);
    let mut pos: usize = 0;

    for _ in 0..num_chunks - 1 {
        if pos >= data.len() {
            break;
        }
        let target_end = (pos + target_chunk_size).min(data.len());
        let chunk_end = if target_end >= data.len() {
            data.len()
        } else {
            match memchr::memchr(b'\n', &data[target_end..]) {
                Some(off) => target_end + off + 1,
                None => data.len(),
            }
        };
        chunks.push(&data[pos..chunk_end]);
        pos = chunk_end;
    }
    if pos < data.len() {
        chunks.push(&data[pos..]);
    }

    let results: Vec<Vec<u8>> = chunks
        .par_iter()
        .map(|chunk| {
            let mut buf = Vec::with_capacity(chunk.len() + chunk.len() / width + 256);
            fold_byte_spaces_chunk(chunk, width, &mut buf);
            buf
        })
        .collect();

    for result in &results {
        if !result.is_empty() {
            out.write_all(result)?;
        }
    }
    Ok(())
}

/// Process a chunk for fold byte mode with -s into a Vec<u8>.
fn fold_byte_spaces_chunk(data: &[u8], width: usize, outbuf: &mut Vec<u8>) {
    let mut pos: usize = 0;

    for nl_pos in memchr::memchr_iter(b'\n', data) {
        let segment = &data[pos..nl_pos];
        fold_segment_bytes_spaces_buffered(segment, width, outbuf);
        outbuf.push(b'\n');
        pos = nl_pos + 1;
    }

    if pos < data.len() {
        fold_segment_bytes_spaces_buffered(&data[pos..], width, outbuf);
    }
}

/// Parallel threshold for fold column mode.
const FOLD_PARALLEL_THRESHOLD: usize = 4 * 1024 * 1024;

/// Streaming fold by column count — single-pass stream using memchr2.
/// For large files (>4MB), uses rayon parallel processing on line-aligned chunks.
/// Each chunk is processed independently since column resets at newlines.
fn fold_column_mode_streaming(
    data: &[u8],
    width: usize,
    break_at_spaces: bool,
    out: &mut impl Write,
) -> std::io::Result<()> {
    if break_at_spaces {
        return fold_column_mode_spaces_streaming(data, width, out);
    }

    if data.len() >= FOLD_PARALLEL_THRESHOLD {
        return fold_column_parallel(data, width, out);
    }

    let mut outbuf: Vec<u8> = Vec::with_capacity(data.len() + data.len() / 4);
    fold_column_chunk(data, width, &mut outbuf);
    if !outbuf.is_empty() {
        out.write_all(&outbuf)?;
    }
    Ok(())
}

/// Parallel fold for column mode with tabs. Splits data into line-aligned chunks,
/// processes each in parallel with rayon, writes results in order.
fn fold_column_parallel(data: &[u8], width: usize, out: &mut impl Write) -> std::io::Result<()> {
    use rayon::prelude::*;

    let num_chunks = rayon::current_num_threads().max(2);
    let target_chunk_size = data.len() / num_chunks;
    let mut chunks: Vec<&[u8]> = Vec::with_capacity(num_chunks + 1);
    let mut pos: usize = 0;

    for _ in 0..num_chunks - 1 {
        if pos >= data.len() {
            break;
        }
        let target_end = (pos + target_chunk_size).min(data.len());
        let chunk_end = if target_end >= data.len() {
            data.len()
        } else {
            match memchr::memchr(b'\n', &data[target_end..]) {
                Some(off) => target_end + off + 1,
                None => data.len(),
            }
        };
        chunks.push(&data[pos..chunk_end]);
        pos = chunk_end;
    }
    if pos < data.len() {
        chunks.push(&data[pos..]);
    }

    let results: Vec<Vec<u8>> = chunks
        .par_iter()
        .map(|chunk| {
            let mut buf = Vec::with_capacity(chunk.len() + chunk.len() / 4);
            fold_column_chunk(chunk, width, &mut buf);
            buf
        })
        .collect();

    for result in &results {
        if !result.is_empty() {
            out.write_all(result)?;
        }
    }
    Ok(())
}

/// Process a chunk for fold column mode using unsafe pointer writes.
/// Uses memchr2 SIMD scanning for tabs and newlines, with raw pointer output.
fn fold_column_chunk(data: &[u8], width: usize, outbuf: &mut Vec<u8>) {
    if data.is_empty() {
        return;
    }

    // Worst case: every char at width boundary → output ≈ 2x input
    let worst = data.len() * 2 + 4096;
    outbuf.reserve(worst);

    let src = data.as_ptr();
    let out_base = outbuf.as_mut_ptr();
    let initial_len = outbuf.len();
    let mut wp: usize = initial_len;
    let mut col: usize = 0;
    let mut seg_start: usize = 0;
    let mut i: usize = 0;

    while i < data.len() {
        match memchr::memchr2(b'\t', b'\n', &data[i..]) {
            Some(off) => {
                let special_pos = i + off;
                let run_len = special_pos - i;

                if col + run_len > width {
                    loop {
                        let remaining = special_pos - i;
                        let fit = width - col;
                        if fit >= remaining {
                            col += remaining;
                            i = special_pos;
                            break;
                        }
                        let copy_len = i + fit - seg_start;
                        unsafe {
                            std::ptr::copy_nonoverlapping(
                                src.add(seg_start),
                                out_base.add(wp),
                                copy_len,
                            );
                            wp += copy_len;
                            *out_base.add(wp) = b'\n';
                            wp += 1;
                        }
                        i += fit;
                        seg_start = i;
                        col = 0;
                    }
                } else {
                    col += run_len;
                    i = special_pos;
                }

                if data[i] == b'\n' {
                    let copy_len = i + 1 - seg_start;
                    unsafe {
                        std::ptr::copy_nonoverlapping(
                            src.add(seg_start),
                            out_base.add(wp),
                            copy_len,
                        );
                    }
                    wp += copy_len;
                    col = 0;
                    i += 1;
                    seg_start = i;
                } else {
                    let new_col = ((col >> 3) + 1) << 3;
                    if new_col > width && col > 0 {
                        let copy_len = i - seg_start;
                        unsafe {
                            std::ptr::copy_nonoverlapping(
                                src.add(seg_start),
                                out_base.add(wp),
                                copy_len,
                            );
                            wp += copy_len;
                            *out_base.add(wp) = b'\n';
                            wp += 1;
                        }
                        seg_start = i;
                        col = 0;
                        continue;
                    }
                    col = new_col;
                    i += 1;
                }
            }
            None => {
                let remaining = data.len() - i;
                if col + remaining > width {
                    loop {
                        let rem_now = data.len() - i;
                        let fit = width - col;
                        if fit >= rem_now {
                            break;
                        }
                        let copy_len = i + fit - seg_start;
                        unsafe {
                            std::ptr::copy_nonoverlapping(
                                src.add(seg_start),
                                out_base.add(wp),
                                copy_len,
                            );
                            wp += copy_len;
                            *out_base.add(wp) = b'\n';
                            wp += 1;
                        }
                        i += fit;
                        seg_start = i;
                        col = 0;
                    }
                }
                break;
            }
        }
    }

    if seg_start < data.len() {
        let copy_len = data.len() - seg_start;
        unsafe {
            std::ptr::copy_nonoverlapping(src.add(seg_start), out_base.add(wp), copy_len);
        }
        wp += copy_len;
    }

    unsafe {
        outbuf.set_len(wp);
    }
}

/// Fold a byte segment (no newlines) with -s (break at spaces), buffered output.
#[inline]
fn fold_segment_bytes_spaces_buffered(segment: &[u8], width: usize, outbuf: &mut Vec<u8>) {
    let mut start = 0;
    while start + width < segment.len() {
        let chunk = &segment[start..start + width];
        match memchr::memrchr2(b' ', b'\t', chunk) {
            Some(sp_offset) => {
                let break_at = start + sp_offset + 1;
                outbuf.extend_from_slice(&segment[start..break_at]);
                outbuf.push(b'\n');
                start = break_at;
            }
            None => {
                outbuf.extend_from_slice(&segment[start..start + width]);
                outbuf.push(b'\n');
                start += width;
            }
        }
    }
    if start < segment.len() {
        outbuf.extend_from_slice(&segment[start..]);
    }
}

/// Streaming fold column mode with -s (break at spaces).
/// Uses buffered output to minimize write syscalls.
/// Fast path: if no tabs in data, column width == byte width, so we can
/// use the simpler byte-mode space-breaking algorithm.
fn fold_column_mode_spaces_streaming(
    data: &[u8],
    width: usize,
    out: &mut impl Write,
) -> std::io::Result<()> {
    // If no tabs, column mode == byte mode (every byte has width 1)
    // BS/CR/control chars could theoretically differ but are vanishingly rare
    // in practice and the difference is negligible.
    if memchr::memchr(b'\t', data).is_none() {
        return fold_byte_fast_spaces(data, width, out);
    }

    let mut pos = 0;
    let mut outbuf: Vec<u8> = Vec::with_capacity(1024 * 1024 + 4096);

    for nl_pos in memchr::memchr_iter(b'\n', data) {
        let line = &data[pos..nl_pos];
        // Short-circuit: line fits in width AND has no tabs → no folding needed
        if line.len() <= width && memchr::memchr(b'\t', line).is_none() {
            outbuf.extend_from_slice(line);
        } else {
            fold_column_spaces_fast(line, width, &mut outbuf);
        }
        outbuf.push(b'\n');

        if outbuf.len() >= 1024 * 1024 {
            out.write_all(&outbuf)?;
            outbuf.clear();
        }

        pos = nl_pos + 1;
    }

    // Handle final line without trailing newline
    if pos < data.len() {
        let line = &data[pos..];
        if line.len() <= width && memchr::memchr(b'\t', line).is_none() {
            outbuf.extend_from_slice(line);
        } else {
            fold_column_spaces_fast(line, width, &mut outbuf);
        }
    }

    if !outbuf.is_empty() {
        out.write_all(&outbuf)?;
    }

    Ok(())
}

/// Fast column-mode fold for a single line with -s (break at spaces).
/// Uses memchr2 to find tabs and spaces in bulk, processing runs of regular
/// bytes without per-byte branching. Matches GNU fold's exact algorithm:
/// - `column > width` triggers break (strictly greater)
/// - Break at last blank: output INCLUDING the blank, remainder starts after it
/// - After break: recalculate column from remaining data, re-process current char
/// - All bytes width 1 except tab (next tab stop), BS (col-1), CR (col=0)
#[inline]
fn fold_column_spaces_fast(line: &[u8], width: usize, outbuf: &mut Vec<u8>) {
    let mut col: usize = 0;
    let mut seg_start: usize = 0;
    let mut last_space_after: usize = 0;
    let mut has_space = false;
    let mut i: usize = 0;

    while i < line.len() {
        let b = line[i];
        if b == b'\t' {
            let new_col = ((col >> 3) + 1) << 3;
            if new_col > width && col > 0 {
                // Tab exceeds width — break
                if has_space {
                    outbuf.extend_from_slice(&line[seg_start..last_space_after]);
                    outbuf.push(b'\n');
                    seg_start = last_space_after;
                    col = recalc_column(&line[seg_start..i]);
                    has_space = false;
                    continue; // re-evaluate tab
                }
                outbuf.extend_from_slice(&line[seg_start..i]);
                outbuf.push(b'\n');
                seg_start = i;
                col = 0;
                continue; // re-evaluate tab with col=0
            }
            // Tab also counts as a breakable whitespace for -s (GNU compat)
            has_space = true;
            last_space_after = i + 1;
            col = new_col;
            i += 1;
        } else if b == b' ' {
            col += 1;
            if col > width {
                if has_space {
                    outbuf.extend_from_slice(&line[seg_start..last_space_after]);
                    outbuf.push(b'\n');
                    seg_start = last_space_after;
                    col = recalc_column(&line[seg_start..i]);
                    has_space = false;
                    continue; // re-evaluate this space
                }
                // No prior blank — break before this space (GNU: output buffer, rescan)
                outbuf.extend_from_slice(&line[seg_start..i]);
                outbuf.push(b'\n');
                seg_start = i;
                col = 1; // space starts the new line with width 1
                has_space = true;
                last_space_after = i + 1;
                i += 1;
                continue;
            }
            has_space = true;
            last_space_after = i + 1;
            i += 1;
        } else {
            // Find next tab or space using SIMD memchr2
            let run_end = match memchr::memchr2(b'\t', b' ', &line[i + 1..]) {
                Some(off) => i + 1 + off,
                None => line.len(),
            };

            // Process run of regular bytes: each has column width 1
            let run_remaining = run_end - i;
            if col + run_remaining <= width {
                // Entire run fits
                col += run_remaining;
                i = run_end;
            } else {
                // Run exceeds width — need to break
                let mut j = i;
                loop {
                    let rem = run_end - j;
                    if col + rem <= width {
                        col += rem;
                        i = run_end;
                        break;
                    }
                    if has_space {
                        // Break at last blank (includes the blank)
                        outbuf.extend_from_slice(&line[seg_start..last_space_after]);
                        outbuf.push(b'\n');
                        seg_start = last_space_after;
                        col = j - seg_start; // regular bytes only, each width 1
                        has_space = false;
                        continue; // re-check with new col
                    }
                    // No blank — hard break at width boundary
                    let fit = width - col;
                    outbuf.extend_from_slice(&line[seg_start..j + fit]);
                    outbuf.push(b'\n');
                    j += fit;
                    seg_start = j;
                    col = 0;
                }
            }
        }
    }

    if seg_start < line.len() {
        outbuf.extend_from_slice(&line[seg_start..]);
    }
}

/// Get the column width and byte length of a byte at `data[pos]`.
/// Returns (column_width, byte_length) — always (1, 1) for non-special bytes.
///
/// GNU fold's multibyte path is guarded by:
///   `#if HAVE_MBRTOC32 && (! defined __GLIBC__ || defined __UCLIBC__)`
/// On glibc (every mainstream Linux distro), that condition is false, so
/// fold counts bytes — one column per byte, same as -b mode.
/// Tab, backspace, and CR are handled by the caller.
#[inline]
fn char_info(data: &[u8], pos: usize) -> (usize, usize) {
    let b = data[pos];
    if b < 0x80 {
        // ASCII: tab/backspace handled by caller; control chars have 0 width
        if b < 0x20 || b == 0x7f {
            (0, 1)
        } else {
            (1, 1)
        }
    } else {
        // High byte: count as 1 column, 1 byte (GNU glibc compat)
        (1, 1)
    }
}

/// Check if folding would produce identical output (all lines fit within width).
/// Used by the binary for direct write-through optimization.
pub fn fold_is_passthrough(data: &[u8], width: usize, count_bytes: bool) -> bool {
    if width == 0 || data.is_empty() {
        return data.is_empty();
    }
    // Column mode with tabs: can't easily determine passthrough
    if !count_bytes && memchr::memchr(b'\t', data).is_some() {
        return false;
    }
    let mut prev = 0;
    for nl_pos in memchr::memchr_iter(b'\n', data) {
        if nl_pos - prev > width {
            return false;
        }
        prev = nl_pos + 1;
    }
    data.len() - prev <= width
}

/// Recalculate column position by replaying a segment (handles tabs, CR, backspace).
/// Used when non-linear column operations (CR, backspace) invalidate the fast
/// `col - col_at_space` delta formula.
fn recalc_column(data: &[u8]) -> usize {
    let mut col = 0;
    let mut i = 0;
    while i < data.len() {
        let b = data[i];
        if b == b'\r' {
            col = 0;
            i += 1;
        } else if b == b'\t' {
            col = ((col / 8) + 1) * 8;
            i += 1;
        } else if b == b'\x08' {
            if col > 0 {
                col -= 1;
            }
            i += 1;
        } else if b < 0x80 {
            if b >= 0x20 && b != 0x7f {
                col += 1;
            }
            i += 1;
        } else {
            let (cw, byte_len) = char_info(data, i);
            col += cw;
            i += byte_len;
        }
    }
    col
}