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
use std::io::Write;

/// Configuration for the paste command.
pub struct PasteConfig {
    /// Delimiter characters, cycled through columns.
    pub delimiters: Vec<u8>,
    /// Serial mode: paste one file at a time.
    pub serial: bool,
    /// Use NUL as line terminator instead of newline.
    pub zero_terminated: bool,
}

impl Default for PasteConfig {
    fn default() -> Self {
        Self {
            delimiters: vec![b'\t'],
            serial: false,
            zero_terminated: false,
        }
    }
}

/// Parse delimiter string with escape sequences.
/// Supports: \n (newline), \t (tab), \\ (backslash), \0 (NUL), empty string (no delimiter).
pub fn parse_delimiters(s: &str) -> Vec<u8> {
    if s.is_empty() {
        return Vec::new();
    }
    let bytes = s.as_bytes();
    let mut result = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\\' && i + 1 < bytes.len() {
            match bytes[i + 1] {
                b'n' => {
                    result.push(b'\n');
                    i += 2;
                }
                b't' => {
                    result.push(b'\t');
                    i += 2;
                }
                b'\\' => {
                    result.push(b'\\');
                    i += 2;
                }
                b'0' => {
                    result.push(0);
                    i += 2;
                }
                _ => {
                    // Unknown escape: treat backslash as literal
                    result.push(b'\\');
                    i += 1;
                }
            }
        } else {
            result.push(bytes[i]);
            i += 1;
        }
    }
    result
}

/// Output buffer size for streaming paste (2 MiB).
const BUF_SIZE: usize = 2 * 1024 * 1024;

/// Raw write to stdout fd 1. Returns any error encountered.
#[cfg(unix)]
pub fn raw_write_all(data: &[u8]) -> std::io::Result<()> {
    let mut written = 0;
    while written < data.len() {
        let ret = unsafe {
            libc::write(
                1,
                data[written..].as_ptr() as *const libc::c_void,
                (data.len() - written) as _,
            )
        };
        if ret > 0 {
            written += ret as usize;
        } else if ret == 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::WriteZero,
                "write returned 0",
            ));
        } else {
            let err = std::io::Error::last_os_error();
            if err.kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            return Err(err);
        }
    }
    Ok(())
}

#[cfg(not(unix))]
pub fn raw_write_all(data: &[u8]) -> std::io::Result<()> {
    let stdout = std::io::stdout();
    let mut lock = stdout.lock();
    lock.write_all(data)?;
    lock.flush()
}

/// Streaming paste for the parallel (normal) mode.
/// Scans each file line-by-line with memchr on-the-fly — no pre-split offset arrays.
/// Uses a single 2MB output buffer with raw fd writes.
pub fn paste_parallel_stream(file_data: &[&[u8]], config: &PasteConfig) -> std::io::Result<()> {
    let terminator = if config.zero_terminated { 0u8 } else { b'\n' };
    let delims = &config.delimiters;
    let has_delims = !delims.is_empty();
    let nfiles = file_data.len();

    if nfiles == 0 || file_data.iter().all(|d| d.is_empty()) {
        return Ok(());
    }

    // Fast path: single file is a passthrough (output == input)
    if nfiles == 1 {
        let data = file_data[0];
        if data.is_empty() {
            return Ok(());
        }
        if *data.last().unwrap() == terminator {
            return raw_write_all(data);
        }
        raw_write_all(data)?;
        return raw_write_all(&[terminator]);
    }

    // Fast path: 2 files with single-byte delimiter (the most common case)
    if nfiles == 2 && delims.len() == 1 {
        return paste_two_files_streaming(file_data[0], file_data[1], delims[0], terminator);
    }

    // General N-file streaming paste
    paste_n_files_streaming(file_data, delims, has_delims, nfiles, terminator)
}

/// Fast path for 2-file paste: uses memchr_iter iterators advanced in lockstep.
/// Zero allocation for line offsets — each iterator maintains its internal SIMD state.
/// memchr_iter amortizes SIMD setup across the entire file scan, avoiding per-line
/// memchr call overhead.
fn paste_two_files_streaming(
    data_a: &[u8],
    data_b: &[u8],
    delim: u8,
    terminator: u8,
) -> std::io::Result<()> {
    if data_a.is_empty() && data_b.is_empty() {
        return Ok(());
    }

    let ptr_a = data_a.as_ptr();
    let ptr_b = data_b.as_ptr();
    let len_a = data_a.len();
    let len_b = data_b.len();

    let buf_cap = BUF_SIZE;
    let mut buf: Vec<u8> = Vec::with_capacity(buf_cap + 65536);
    let mut pos: usize = 0;

    // Use memchr_iter to scan both files — no per-line memchr calls, no offset arrays.
    let mut iter_a = memchr::memchr_iter(terminator, data_a);
    let mut iter_b = memchr::memchr_iter(terminator, data_b);

    let mut cur_a: usize = 0; // start of current line in A
    let mut cur_b: usize = 0; // start of current line in B
    let mut done_a = len_a == 0;
    let mut done_b = len_b == 0;

    while !done_a || !done_b {
        // Advance file A iterator to get next line
        let (a_start, a_len, a_has_line) = if !done_a {
            match iter_a.next() {
                Some(nl_pos) => {
                    let start = cur_a;
                    let line_len = nl_pos - cur_a;
                    cur_a = nl_pos + 1;
                    (start, line_len, true)
                }
                None => {
                    // No more newlines — check for trailing data without terminator
                    done_a = true;
                    if cur_a < len_a {
                        let start = cur_a;
                        let line_len = len_a - cur_a;
                        cur_a = len_a;
                        (start, line_len, true)
                    } else {
                        (0, 0, false)
                    }
                }
            }
        } else {
            (0, 0, false)
        };

        // Advance file B iterator to get next line
        let (b_start, b_len, b_has_line) = if !done_b {
            match iter_b.next() {
                Some(nl_pos) => {
                    let start = cur_b;
                    let line_len = nl_pos - cur_b;
                    cur_b = nl_pos + 1;
                    (start, line_len, true)
                }
                None => {
                    done_b = true;
                    if cur_b < len_b {
                        let start = cur_b;
                        let line_len = len_b - cur_b;
                        cur_b = len_b;
                        (start, line_len, true)
                    } else {
                        (0, 0, false)
                    }
                }
            }
        } else {
            (0, 0, false)
        };

        // If neither file produced a line this iteration, we're truly done.
        if !a_has_line && !b_has_line {
            break;
        }

        debug_assert!(a_start + a_len <= len_a, "a out of bounds");
        debug_assert!(b_start + b_len <= len_b, "b out of bounds");
        // On 64-bit: overflow requires a 9 EiB file. On 32-bit: a single
        // line cannot exceed 2 GiB (unmappable in 4 GiB address space).
        debug_assert!(a_len < isize::MAX as usize && b_len < isize::MAX as usize);
        debug_assert!(
            a_len
                .checked_add(b_len)
                .and_then(|x| x.checked_add(2))
                .is_some()
        );
        let out_len = a_len + b_len + 2;

        // Flush if needed
        if pos + out_len > buf.capacity() {
            unsafe { buf.set_len(pos) };
            raw_write_all(&buf)?;
            buf.clear();
            pos = 0;
            if out_len > buf.capacity() {
                buf.reserve(out_len);
            }
        }

        // Write: lineA + delim + lineB + terminator
        unsafe {
            let base = buf.as_mut_ptr();
            if a_len > 0 {
                std::ptr::copy_nonoverlapping(ptr_a.add(a_start), base.add(pos), a_len);
                pos += a_len;
            }
            *base.add(pos) = delim;
            pos += 1;
            if b_len > 0 {
                std::ptr::copy_nonoverlapping(ptr_b.add(b_start), base.add(pos), b_len);
                pos += b_len;
            }
            *base.add(pos) = terminator;
            pos += 1;
        }

        // Periodic flush
        if pos >= buf_cap {
            unsafe { buf.set_len(pos) };
            raw_write_all(&buf)?;
            buf.clear();
            pos = 0;
        }
    }

    // Final flush
    if pos > 0 {
        unsafe { buf.set_len(pos) };
        raw_write_all(&buf)?;
    }

    Ok(())
}

/// General N-file streaming paste using memchr_iter iterators in lockstep.
/// Each file has its own memchr_iter, cursor position, and done flag.
fn paste_n_files_streaming(
    file_data: &[&[u8]],
    delims: &[u8],
    has_delims: bool,
    nfiles: usize,
    terminator: u8,
) -> std::io::Result<()> {
    // The saved_pos rewind relies on delimiter-only writes (at most nfiles-1
    // bytes per iteration) never exceeding the 65536-byte capacity headroom.
    // When has_delims is false, no delimiter bytes are written, so the limit
    // does not apply. nfiles files produce nfiles-1 delimiters per row.
    if nfiles > 65536 {
        return Err(std::io::Error::other("too many files"));
    }

    let mut cursors: Vec<usize> = vec![0; nfiles];
    let mut done: Vec<bool> = file_data.iter().map(|d| d.is_empty()).collect();
    let mut files_remaining = done.iter().filter(|&&d| !d).count();

    let buf_cap = BUF_SIZE;
    let mut buf: Vec<u8> = Vec::with_capacity(buf_cap + 65536);
    let mut pos: usize = 0;

    // Create memchr_iter for each file
    let mut iters: Vec<memchr::Memchr<'_>> = file_data
        .iter()
        .map(|d| memchr::memchr_iter(terminator, d))
        .collect();

    while files_remaining > 0 {
        // Save buffer position so we can rewind if no file produces a line.
        // Rewind safety depends on three invariants:
        // (1) Data flushes (line-copy paths) only fire when line_len > 0 or rem > 0,
        //     both of which set any_iter_advanced = true. So if !any_iter_advanced,
        //     no data flush happened and saved_pos was never invalidated.
        // (2) Delimiter flush cannot fire: the nfiles guard + 65536-byte headroom
        //     ensures pos + 1 <= buf.capacity() for delimiter-only writes.
        // (3) After a data flush, pos resets to 0, so the delimiter-flush guard
        //     becomes trivially false again.
        debug_assert!(
            pos < buf_cap,
            "saved_pos invariant: pos must be < buf_cap at iteration start"
        );
        let saved_pos = pos;
        let mut any_iter_advanced = false;

        for file_idx in 0..nfiles {
            // Delimiter before columns 1..N
            if file_idx > 0 && has_delims {
                // SAFETY: has_delims guarantees delims.len() > 0, making modulo index valid
                let d = unsafe { *delims.get_unchecked((file_idx - 1) % delims.len()) };
                // Safety of saved_pos rewind:
                // 1. nfiles <= 65536 (checked above) so delimiter-only writes <= 65535 bytes
                // 2. buf capacity = buf_cap + 65536, so delimiter writes never trigger flush
                // 3. Therefore saved_pos remains valid throughout the iteration
                debug_assert!(
                    pos < buf.capacity(),
                    "delimiter flush should be unreachable under nfiles invariant"
                );
                if pos >= buf.capacity() {
                    unsafe { buf.set_len(pos) };
                    raw_write_all(&buf)?;
                    buf.clear();
                    pos = 0;
                }
                unsafe { *buf.as_mut_ptr().add(pos) = d };
                pos += 1;
            }

            if !done[file_idx] {
                let data = file_data[file_idx];
                let cur = cursors[file_idx];

                match iters[file_idx].next() {
                    Some(nl_pos) => {
                        let line_len = nl_pos - cur;
                        any_iter_advanced = true;
                        if line_len > 0 {
                            if pos + line_len > buf.capacity() {
                                unsafe { buf.set_len(pos) };
                                raw_write_all(&buf)?;
                                buf.clear();
                                pos = 0;
                                if line_len > buf.capacity() {
                                    buf.reserve(line_len + 4096);
                                }
                            }
                            unsafe {
                                std::ptr::copy_nonoverlapping(
                                    data.as_ptr().add(cur),
                                    buf.as_mut_ptr().add(pos),
                                    line_len,
                                );
                            }
                            pos += line_len;
                        }
                        cursors[file_idx] = nl_pos + 1;
                    }
                    None => {
                        // No more newlines — check for trailing data
                        let rem = data.len() - cur;
                        if rem > 0 {
                            any_iter_advanced = true;
                            if pos + rem > buf.capacity() {
                                unsafe { buf.set_len(pos) };
                                raw_write_all(&buf)?;
                                buf.clear();
                                pos = 0;
                                if rem > buf.capacity() {
                                    buf.reserve(rem + 4096);
                                }
                            }
                            unsafe {
                                std::ptr::copy_nonoverlapping(
                                    data.as_ptr().add(cur),
                                    buf.as_mut_ptr().add(pos),
                                    rem,
                                );
                            }
                            pos += rem;
                        }
                        done[file_idx] = true;
                        files_remaining -= 1;
                        cursors[file_idx] = data.len();
                    }
                }
            }
        }

        if !any_iter_advanced {
            // Invariant: every remaining active file just exhausted with rem == 0,
            // so files_remaining == 0 here. No content was produced this iteration;
            // rewind the delimiters and break without writing a terminator.
            // Rewind is safe: the nfiles guard (above) ensures delimiter-only
            // writes cannot trigger a flush, and saved_pos remains valid.
            debug_assert_eq!(files_remaining, 0);
            pos = saved_pos;
            break;
        }

        // Terminator
        if pos >= buf.capacity() {
            unsafe { buf.set_len(pos) };
            raw_write_all(&buf)?;
            buf.clear();
            pos = 0;
        }
        unsafe { *buf.as_mut_ptr().add(pos) = terminator };
        pos += 1;

        // Flush when buffer is full
        if pos >= buf_cap {
            unsafe { buf.set_len(pos) };
            raw_write_all(&buf)?;
            buf.clear();
            pos = 0;
        }
    }

    // Final flush
    if pos > 0 {
        unsafe { buf.set_len(pos) };
        raw_write_all(&buf)?;
    }

    Ok(())
}

/// Streaming paste for serial mode.
/// For each file, join all lines with the delimiter list (cycling).
pub fn paste_serial_stream(file_data: &[&[u8]], config: &PasteConfig) -> std::io::Result<()> {
    let terminator = if config.zero_terminated { 0u8 } else { b'\n' };
    let delims = &config.delimiters;
    let has_delims = !delims.is_empty();

    // Fast path: single-delimiter serial mode — bulk copy with optional scatter replace.
    // When delimiter != terminator: copy chunks and replace terminators with the delimiter.
    // When delimiter == terminator (identity): copy chunks as-is, no replacement needed.
    // Processes in BUF_SIZE chunks to avoid full-file allocation.
    if has_delims && delims.len() == 1 {
        let replacement = delims[0];
        let needs_replace = replacement != terminator;
        let mut buf: Vec<u8> = Vec::with_capacity(BUF_SIZE + 4096);

        for data in file_data {
            if data.is_empty() {
                buf.push(terminator);
                if buf.len() >= BUF_SIZE {
                    raw_write_all(&buf)?;
                    buf.clear();
                }
                continue;
            }

            // Strip trailing terminator — we'll add one at the end.
            let effective = if data.last() == Some(&terminator) {
                &data[..data.len() - 1]
            } else {
                data
            };

            // Process in chunks to avoid full-file allocation + page faults.
            let mut cursor = 0usize;
            while cursor < effective.len() {
                let chunk_end = (cursor + BUF_SIZE).min(effective.len());
                let chunk = &effective[cursor..chunk_end];
                let start = buf.len();
                buf.extend_from_slice(chunk);
                // Replace terminators with delimiter only when they differ.
                if needs_replace {
                    for pos in memchr::memchr_iter(terminator, chunk) {
                        buf[start + pos] = replacement;
                    }
                }
                cursor = chunk_end;

                if buf.len() >= BUF_SIZE {
                    raw_write_all(&buf)?;
                    buf.clear();
                }
            }

            buf.push(terminator);
            if buf.len() >= BUF_SIZE {
                raw_write_all(&buf)?;
                buf.clear();
            }
        }

        if !buf.is_empty() {
            raw_write_all(&buf)?;
        }
        return Ok(());
    }

    let mut buf: Vec<u8> = Vec::with_capacity(BUF_SIZE + 4096);

    for data in file_data {
        if data.is_empty() {
            buf.push(terminator);
            if buf.len() >= BUF_SIZE {
                raw_write_all(&buf)?;
                buf.clear();
            }
            continue;
        }

        let mut cursor = 0usize;
        let mut line_idx = 0usize;
        let mut iter = memchr::memchr_iter(terminator, data);

        loop {
            // Delimiter before lines 1..N
            if line_idx > 0 && has_delims {
                buf.push(delims[(line_idx - 1) % delims.len()]);
            }

            match iter.next() {
                Some(nl_pos) => {
                    let line = &data[cursor..nl_pos];
                    if !line.is_empty() {
                        if buf.len() + line.len() > buf.capacity() {
                            raw_write_all(&buf)?;
                            buf.clear();
                            if line.len() > buf.capacity() {
                                buf.reserve(line.len() + 4096);
                            }
                        }
                        buf.extend_from_slice(line);
                    }
                    cursor = nl_pos + 1;
                }
                None => {
                    // No more terminators — check for trailing data
                    if cursor < data.len() {
                        let remaining = &data[cursor..];
                        if buf.len() + remaining.len() > buf.capacity() {
                            raw_write_all(&buf)?;
                            buf.clear();
                            if remaining.len() > buf.capacity() {
                                buf.reserve(remaining.len() + 4096);
                            }
                        }
                        buf.extend_from_slice(remaining);
                    }
                    break;
                }
            }

            line_idx += 1;

            if buf.len() >= BUF_SIZE {
                raw_write_all(&buf)?;
                buf.clear();
            }
        }

        buf.push(terminator);
        if buf.len() >= BUF_SIZE {
            raw_write_all(&buf)?;
            buf.clear();
        }
    }

    // Final flush
    if !buf.is_empty() {
        raw_write_all(&buf)?;
    }

    Ok(())
}

/// Streaming paste entry point. Writes directly to stdout using raw fd writes.
pub fn paste_stream(file_data: &[&[u8]], config: &PasteConfig) -> std::io::Result<()> {
    if config.serial {
        paste_serial_stream(file_data, config)
    } else {
        paste_parallel_stream(file_data, config)
    }
}

/// Pre-split a file into line offset pairs using a single SIMD memchr_iter pass.
/// Returns a Vec of (start, end) byte offsets — one per line.
#[inline]
fn presplit_lines(data: &[u8], terminator: u8) -> Vec<(u32, u32)> {
    if data.is_empty() {
        return Vec::new();
    }
    assert!(
        data.len() <= u32::MAX as usize,
        "presplit_lines: data exceeds 4 GiB"
    );
    // Heuristic: assume average line length ~40 bytes to avoid a count pre-scan.
    let estimated_lines = data.len() / 40 + 1;
    let mut offsets = Vec::with_capacity(estimated_lines);
    let mut start = 0u32;
    for pos in memchr::memchr_iter(terminator, data) {
        offsets.push((start, pos as u32));
        start = pos as u32 + 1;
    }
    if data.last() != Some(&terminator) && (start as usize) < data.len() {
        offsets.push((start, data.len() as u32));
    }
    offsets
}

/// Paste files in normal (parallel) mode and return the output buffer.
/// Pre-splits files into line offsets (one SIMD pass each), then the main
/// loop uses O(1) array indexing instead of per-line memchr calls.
/// Uses unsafe raw pointer writes to eliminate bounds-check overhead.
pub fn paste_parallel_to_vec(file_data: &[&[u8]], config: &PasteConfig) -> Vec<u8> {
    let terminator = if config.zero_terminated { 0u8 } else { b'\n' };
    let delims = &config.delimiters;

    if file_data.is_empty() || file_data.iter().all(|d| d.is_empty()) {
        return Vec::new();
    }

    // Pre-split each file into line offsets — single SIMD pass per file.
    let file_lines: Vec<Vec<(u32, u32)>> = file_data
        .iter()
        .map(|data| presplit_lines(data, terminator))
        .collect();

    let max_lines = file_lines.iter().map(|l| l.len()).max().unwrap_or(0);
    if max_lines == 0 {
        return Vec::new();
    }

    // Compute exact output size to avoid reallocation.
    let nfiles = file_data.len();
    let has_delims = !delims.is_empty();
    let delims_per_line = if has_delims && nfiles > 1 {
        nfiles - 1
    } else {
        0
    };

    let mut exact_size = max_lines * (delims_per_line + 1); // delimiters + terminators
    for fl in &file_lines {
        for &(s, e) in fl.iter() {
            exact_size += (e - s) as usize;
        }
    }
    // Empty-file lines contribute nothing but delimiter slots are already counted

    let mut output = Vec::with_capacity(exact_size);

    // SAFETY: We computed exact_size above. All writes go through raw pointers
    // with total bytes written == exact_size. We set_len at the end.
    unsafe {
        let base: *mut u8 = output.as_mut_ptr();
        let mut pos = 0usize;

        for line_idx in 0..max_lines {
            for file_idx in 0..nfiles {
                if file_idx > 0 && has_delims {
                    *base.add(pos) = delims[(file_idx - 1) % delims.len()];
                    pos += 1;
                }
                let lines = &file_lines[file_idx];
                if line_idx < lines.len() {
                    let (s, e) = *lines.get_unchecked(line_idx);
                    let len = (e - s) as usize;
                    if len > 0 {
                        std::ptr::copy_nonoverlapping(
                            file_data.get_unchecked(file_idx).as_ptr().add(s as usize),
                            base.add(pos),
                            len,
                        );
                        pos += len;
                    }
                }
            }
            *base.add(pos) = terminator;
            pos += 1;
        }

        assert_eq!(pos, exact_size, "exact_size miscalculated");
        output.set_len(pos);
    }

    output
}

/// Paste files in serial mode and return the output buffer.
/// For each file, join all lines with the delimiter list (cycling).
pub fn paste_serial_to_vec(file_data: &[&[u8]], config: &PasteConfig) -> Vec<u8> {
    let terminator = if config.zero_terminated { 0u8 } else { b'\n' };
    let delims = &config.delimiters;
    let has_delims = !delims.is_empty();

    let total_input: usize = file_data.iter().map(|d| d.len()).sum();
    let mut output = Vec::with_capacity(total_input + file_data.len());

    // Fast path: single delimiter — bulk copy with optional scatter replace.
    // When delimiter != terminator: copy the file and replace terminators with the delimiter.
    // When delimiter == terminator (identity): copy the file as-is, no replacement needed.
    // Either way, avoids line-by-line presplit + extend_from_slice (~150K calls for 10MB).
    if has_delims && delims.len() == 1 {
        let delim = delims[0];
        let needs_replace = delim != terminator;
        for data in file_data {
            if data.is_empty() {
                output.push(terminator);
                continue;
            }
            let effective = if data.last() == Some(&terminator) {
                &data[..data.len() - 1]
            } else {
                *data
            };
            if effective.is_empty() {
                output.push(terminator);
                continue;
            }
            let start = output.len();
            output.extend_from_slice(effective);
            if needs_replace {
                for pos in memchr::memchr_iter(terminator, effective) {
                    output[start + pos] = delim;
                }
            }
            output.push(terminator);
        }
        return output;
    }

    for data in file_data {
        if data.is_empty() {
            output.push(terminator);
            continue;
        }
        let lines = presplit_lines(data, terminator);
        if lines.is_empty() {
            output.push(terminator);
            continue;
        }
        let (s, e) = lines[0];
        output.extend_from_slice(&data[s as usize..e as usize]);
        for (i, &(s, e)) in lines[1..].iter().enumerate() {
            if has_delims {
                output.push(delims[i % delims.len()]);
            }
            output.extend_from_slice(&data[s as usize..e as usize]);
        }
        output.push(terminator);
    }

    output
}

/// Main paste entry point. Writes directly to the provided writer.
pub fn paste(
    file_data: &[&[u8]],
    config: &PasteConfig,
    out: &mut impl Write,
) -> std::io::Result<()> {
    let output = if config.serial {
        paste_serial_to_vec(file_data, config)
    } else {
        paste_parallel_to_vec(file_data, config)
    };
    out.write_all(&output)
}

/// Build the paste output as a Vec, then return it for the caller to write.
/// This allows the binary to use raw write() for maximum throughput.
pub fn paste_to_vec(file_data: &[&[u8]], config: &PasteConfig) -> Vec<u8> {
    if config.serial {
        paste_serial_to_vec(file_data, config)
    } else {
        paste_parallel_to_vec(file_data, config)
    }
}