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
use std::cmp::Ordering;
use std::io::{self, Write};

/// How to handle sort-order checking.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderCheck {
    Default,
    Strict,
    None,
}

/// An output field specification from -o format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OutputSpec {
    /// Field 0: the join field
    JoinField,
    /// (file_index 0-based, field_index 0-based)
    FileField(usize, usize),
}

/// Configuration for the join command.
pub struct JoinConfig {
    /// Join field for file 1 (0-indexed)
    pub field1: usize,
    /// Join field for file 2 (0-indexed)
    pub field2: usize,
    /// Also print unpairable lines from file 1 (-a 1)
    pub print_unpaired1: bool,
    /// Also print unpairable lines from file 2 (-a 2)
    pub print_unpaired2: bool,
    /// Print ONLY unpairable lines from file 1 (-v 1)
    pub only_unpaired1: bool,
    /// Print ONLY unpairable lines from file 2 (-v 2)
    pub only_unpaired2: bool,
    /// Replace missing fields with this string (-e)
    pub empty_filler: Option<Vec<u8>>,
    /// Ignore case in key comparison (-i)
    pub case_insensitive: bool,
    /// Output format (-o)
    pub output_format: Option<Vec<OutputSpec>>,
    /// Auto output format (-o auto)
    pub auto_format: bool,
    /// Field separator (-t). None = whitespace mode.
    pub separator: Option<u8>,
    /// Order checking
    pub order_check: OrderCheck,
    /// Treat first line as header (--header)
    pub header: bool,
    /// Use NUL as line delimiter (-z)
    pub zero_terminated: bool,
}

impl Default for JoinConfig {
    fn default() -> Self {
        Self {
            field1: 0,
            field2: 0,
            print_unpaired1: false,
            print_unpaired2: false,
            only_unpaired1: false,
            only_unpaired2: false,
            empty_filler: None,
            case_insensitive: false,
            output_format: None,
            auto_format: false,
            separator: None,
            order_check: OrderCheck::Default,
            header: false,
            zero_terminated: false,
        }
    }
}

/// Split data into lines by delimiter using SIMD scanning.
/// Uses heuristic capacity to avoid double-scan.
fn split_lines<'a>(data: &'a [u8], delim: u8) -> Vec<&'a [u8]> {
    if data.is_empty() {
        return Vec::new();
    }
    // Heuristic: assume average line length of ~40 bytes
    let est_lines = data.len() / 40 + 1;
    let mut lines = Vec::with_capacity(est_lines);
    let mut start = 0;
    for pos in memchr::memchr_iter(delim, data) {
        lines.push(&data[start..pos]);
        start = pos + 1;
    }
    if start < data.len() {
        lines.push(&data[start..]);
    }
    lines
}

/// Split a line into fields by whitespace (runs of space/tab).
fn split_fields_whitespace<'a>(line: &'a [u8]) -> Vec<&'a [u8]> {
    let mut fields = Vec::with_capacity(8);
    let mut i = 0;
    let len = line.len();
    while i < len {
        // Skip whitespace
        while i < len && (line[i] == b' ' || line[i] == b'\t') {
            i += 1;
        }
        if i >= len {
            break;
        }
        let start = i;
        while i < len && line[i] != b' ' && line[i] != b'\t' {
            i += 1;
        }
        fields.push(&line[start..i]);
    }
    fields
}

/// Split a line into fields by exact single character.
/// Single-pass: no pre-counting scan.
fn split_fields_char<'a>(line: &'a [u8], sep: u8) -> Vec<&'a [u8]> {
    let mut fields = Vec::with_capacity(8);
    let mut start = 0;
    for pos in memchr::memchr_iter(sep, line) {
        fields.push(&line[start..pos]);
        start = pos + 1;
    }
    fields.push(&line[start..]);
    fields
}

/// Split a line into fields based on the separator setting.
#[inline]
fn split_fields<'a>(line: &'a [u8], separator: Option<u8>) -> Vec<&'a [u8]> {
    if let Some(sep) = separator {
        split_fields_char(line, sep)
    } else {
        split_fields_whitespace(line)
    }
}

/// Extract a single field from a line without allocating a Vec.
#[inline]
fn extract_field<'a>(line: &'a [u8], field_index: usize, separator: Option<u8>) -> &'a [u8] {
    if let Some(sep) = separator {
        let mut count = 0;
        let mut start = 0;
        for pos in memchr::memchr_iter(sep, line) {
            if count == field_index {
                return &line[start..pos];
            }
            count += 1;
            start = pos + 1;
        }
        if count == field_index {
            return &line[start..];
        }
        b""
    } else {
        let mut count = 0;
        let mut i = 0;
        let len = line.len();
        while i < len {
            while i < len && (line[i] == b' ' || line[i] == b'\t') {
                i += 1;
            }
            if i >= len {
                break;
            }
            let start = i;
            while i < len && line[i] != b' ' && line[i] != b'\t' {
                i += 1;
            }
            if count == field_index {
                return &line[start..i];
            }
            count += 1;
        }
        b""
    }
}

/// Compare two keys, optionally case-insensitive.
#[inline]
fn compare_keys(a: &[u8], b: &[u8], case_insensitive: bool) -> Ordering {
    if case_insensitive {
        for (&ca, &cb) in a.iter().zip(b.iter()) {
            match ca.to_ascii_lowercase().cmp(&cb.to_ascii_lowercase()) {
                Ordering::Equal => continue,
                other => return other,
            }
        }
        a.len().cmp(&b.len())
    } else {
        a.cmp(b)
    }
}

/// Write a paired output line (default format: join_key + other fields).
/// Zero-copy: writes directly from line slices without allocating field Vecs.
fn write_paired_default_zerocopy(
    line1: &[u8],
    line2: &[u8],
    join_key: &[u8],
    field1: usize,
    field2: usize,
    separator: Option<u8>,
    out_sep: u8,
    delim: u8,
    buf: &mut Vec<u8>,
) {
    buf.extend_from_slice(join_key);
    write_other_fields(line1, field1, separator, out_sep, buf);
    write_other_fields(line2, field2, separator, out_sep, buf);
    buf.push(delim);
}

/// Write all fields from a line except the join field, prefixed by out_sep.
/// Avoids allocating a Vec<&[u8]> for field splitting.
#[inline]
fn write_other_fields(
    line: &[u8],
    skip_field: usize,
    separator: Option<u8>,
    out_sep: u8,
    buf: &mut Vec<u8>,
) {
    if let Some(sep) = separator {
        let mut field_idx = 0;
        let mut start = 0;
        for pos in memchr::memchr_iter(sep, line) {
            if field_idx != skip_field {
                buf.push(out_sep);
                buf.extend_from_slice(&line[start..pos]);
            }
            field_idx += 1;
            start = pos + 1;
        }
        // Last field (no trailing separator)
        if field_idx != skip_field {
            buf.push(out_sep);
            buf.extend_from_slice(&line[start..]);
        }
    } else {
        // Whitespace-delimited
        let mut field_idx = 0;
        let mut i = 0;
        let len = line.len();
        while i < len {
            while i < len && (line[i] == b' ' || line[i] == b'\t') {
                i += 1;
            }
            if i >= len {
                break;
            }
            let start = i;
            while i < len && line[i] != b' ' && line[i] != b'\t' {
                i += 1;
            }
            if field_idx != skip_field {
                buf.push(out_sep);
                buf.extend_from_slice(&line[start..i]);
            }
            field_idx += 1;
        }
    }
}

/// Write a paired output line with -o format.
fn write_paired_format(
    fields1: &[&[u8]],
    fields2: &[&[u8]],
    join_key: &[u8],
    specs: &[OutputSpec],
    empty: &[u8],
    out_sep: u8,
    delim: u8,
    buf: &mut Vec<u8>,
) {
    for (i, spec) in specs.iter().enumerate() {
        if i > 0 {
            buf.push(out_sep);
        }
        match spec {
            OutputSpec::JoinField => buf.extend_from_slice(join_key),
            OutputSpec::FileField(file_num, field_idx) => {
                let fields = if *file_num == 0 { fields1 } else { fields2 };
                if let Some(f) = fields.get(*field_idx) {
                    buf.extend_from_slice(f);
                } else {
                    buf.extend_from_slice(empty);
                }
            }
        }
    }
    buf.push(delim);
}

/// Write an unpaired output line (default format), zero-copy from line.
fn write_unpaired_default_zerocopy(
    line: &[u8],
    join_field: usize,
    separator: Option<u8>,
    out_sep: u8,
    delim: u8,
    buf: &mut Vec<u8>,
) {
    let key = extract_field(line, join_field, separator);
    buf.extend_from_slice(key);
    write_other_fields(line, join_field, separator, out_sep, buf);
    buf.push(delim);
}

/// Write an unpaired output line with -o format.
fn write_unpaired_format(
    fields: &[&[u8]],
    file_num: usize,
    join_field: usize,
    specs: &[OutputSpec],
    empty: &[u8],
    out_sep: u8,
    delim: u8,
    buf: &mut Vec<u8>,
) {
    let key = fields.get(join_field).copied().unwrap_or(b"");
    for (i, spec) in specs.iter().enumerate() {
        if i > 0 {
            buf.push(out_sep);
        }
        match spec {
            OutputSpec::JoinField => buf.extend_from_slice(key),
            OutputSpec::FileField(fnum, fidx) => {
                if *fnum == file_num {
                    if let Some(f) = fields.get(*fidx) {
                        buf.extend_from_slice(f);
                    } else {
                        buf.extend_from_slice(empty);
                    }
                } else {
                    buf.extend_from_slice(empty);
                }
            }
        }
    }
    buf.push(delim);
}

/// Run the join merge algorithm on two sorted inputs.
pub fn join(
    data1: &[u8],
    data2: &[u8],
    config: &JoinConfig,
    tool_name: &str,
    file1_name: &str,
    file2_name: &str,
    out: &mut impl Write,
) -> io::Result<bool> {
    let delim = if config.zero_terminated { b'\0' } else { b'\n' };
    let out_sep = config.separator.unwrap_or(b' ');
    let empty = config.empty_filler.as_deref().unwrap_or(b"");
    let ci = config.case_insensitive;

    let print_paired = !config.only_unpaired1 && !config.only_unpaired2;
    let show_unpaired1 = config.print_unpaired1 || config.only_unpaired1;
    let show_unpaired2 = config.print_unpaired2 || config.only_unpaired2;

    let lines1 = split_lines(data1, delim);
    let lines2 = split_lines(data2, delim);

    // Pre-compute all join keys — turns O(field_position) per comparison into O(1).
    // Memory: 16 bytes per fat pointer × (lines1 + lines2). At 1M+1M lines ≈ 32 MB,
    // acceptable for the >2x speedup over repeated extract_field scanning.
    let keys1: Vec<&[u8]> = lines1
        .iter()
        .map(|l| extract_field(l, config.field1, config.separator))
        .collect();
    let keys2: Vec<&[u8]> = lines2
        .iter()
        .map(|l| extract_field(l, config.field2, config.separator))
        .collect();

    let mut i1 = 0usize;
    let mut i2 = 0usize;
    let mut had_order_error = false;
    let mut warned1 = false;
    let mut warned2 = false;

    const FLUSH_THRESHOLD: usize = 4 * 1024 * 1024;
    let mut buf = Vec::with_capacity((data1.len() + data2.len()).min(FLUSH_THRESHOLD * 2));

    // Handle -o auto: build format from first lines
    let auto_specs: Option<Vec<OutputSpec>> = if config.auto_format {
        let fc1 = if !lines1.is_empty() {
            split_fields(lines1[0], config.separator).len()
        } else {
            1
        };
        let fc2 = if !lines2.is_empty() {
            split_fields(lines2[0], config.separator).len()
        } else {
            1
        };
        let mut specs = Vec::new();
        specs.push(OutputSpec::JoinField);
        for i in 0..fc1 {
            if i != config.field1 {
                specs.push(OutputSpec::FileField(0, i));
            }
        }
        for i in 0..fc2 {
            if i != config.field2 {
                specs.push(OutputSpec::FileField(1, i));
            }
        }
        Some(specs)
    } else {
        None
    };

    let format = config.output_format.as_deref().or(auto_specs.as_deref());

    // Handle --header: join first lines without sort check
    if config.header && !lines1.is_empty() && !lines2.is_empty() {
        let key = extract_field(lines1[0], config.field1, config.separator);

        if let Some(specs) = format {
            let fields1 = split_fields(lines1[0], config.separator);
            let fields2 = split_fields(lines2[0], config.separator);
            write_paired_format(
                &fields1, &fields2, key, specs, empty, out_sep, delim, &mut buf,
            );
        } else {
            write_paired_default_zerocopy(
                lines1[0],
                lines2[0],
                key,
                config.field1,
                config.field2,
                config.separator,
                out_sep,
                delim,
                &mut buf,
            );
        }
        i1 = 1;
        i2 = 1;
    } else if config.header {
        // One or both files empty — skip header
        if !lines1.is_empty() {
            i1 = 1;
        }
        if !lines2.is_empty() {
            i2 = 1;
        }
    }

    while i1 < lines1.len() && i2 < lines2.len() {
        debug_assert!(i1 < keys1.len() && i2 < keys2.len());
        // SAFETY: keys1.len() == lines1.len() and keys2.len() == lines2.len(),
        // guaranteed by the collect() above; loop condition ensures in-bounds.
        let key1 = unsafe { *keys1.get_unchecked(i1) };
        let key2 = unsafe { *keys2.get_unchecked(i2) };

        // Order checks
        if config.order_check != OrderCheck::None {
            if !warned1 && i1 > (if config.header { 1 } else { 0 }) {
                let prev_key = keys1[i1 - 1];
                if compare_keys(key1, prev_key, ci) == Ordering::Less {
                    had_order_error = true;
                    warned1 = true;
                    eprintln!(
                        "{}: {}:{}: is not sorted: {}",
                        tool_name,
                        file1_name,
                        i1 + 1,
                        String::from_utf8_lossy(lines1[i1])
                    );
                    if config.order_check == OrderCheck::Strict {
                        out.write_all(&buf)?;
                        return Ok(true);
                    }
                }
            }
            if !warned2 && i2 > (if config.header { 1 } else { 0 }) {
                let prev_key = keys2[i2 - 1];
                if compare_keys(key2, prev_key, ci) == Ordering::Less {
                    had_order_error = true;
                    warned2 = true;
                    eprintln!(
                        "{}: {}:{}: is not sorted: {}",
                        tool_name,
                        file2_name,
                        i2 + 1,
                        String::from_utf8_lossy(lines2[i2])
                    );
                    if config.order_check == OrderCheck::Strict {
                        out.write_all(&buf)?;
                        return Ok(true);
                    }
                }
            }
        }

        match compare_keys(key1, key2, ci) {
            Ordering::Less => {
                if show_unpaired1 {
                    if let Some(specs) = format {
                        let fields1 = split_fields(lines1[i1], config.separator);
                        write_unpaired_format(
                            &fields1,
                            0,
                            config.field1,
                            specs,
                            empty,
                            out_sep,
                            delim,
                            &mut buf,
                        );
                    } else {
                        write_unpaired_default_zerocopy(
                            lines1[i1],
                            config.field1,
                            config.separator,
                            out_sep,
                            delim,
                            &mut buf,
                        );
                    }
                }
                i1 += 1;
                if show_unpaired1 && buf.len() >= FLUSH_THRESHOLD {
                    out.write_all(&buf)?;
                    buf.clear();
                }
            }
            Ordering::Greater => {
                if show_unpaired2 {
                    if let Some(specs) = format {
                        let fields2 = split_fields(lines2[i2], config.separator);
                        write_unpaired_format(
                            &fields2,
                            1,
                            config.field2,
                            specs,
                            empty,
                            out_sep,
                            delim,
                            &mut buf,
                        );
                    } else {
                        write_unpaired_default_zerocopy(
                            lines2[i2],
                            config.field2,
                            config.separator,
                            out_sep,
                            delim,
                            &mut buf,
                        );
                    }
                }
                i2 += 1;

                // Periodic flush to limit memory usage for large inputs
                if buf.len() >= FLUSH_THRESHOLD {
                    out.write_all(&buf)?;
                    buf.clear();
                }
            }
            Ordering::Equal => {
                // Find all consecutive file2 lines with the same key
                let group_start = i2;
                let current_key = key2;
                i2 += 1;
                while i2 < lines2.len() {
                    debug_assert!(i2 < keys2.len());
                    // SAFETY: i2 < lines2.len() == keys2.len()
                    let next_key = unsafe { *keys2.get_unchecked(i2) };
                    if compare_keys(next_key, current_key, ci) != Ordering::Equal {
                        break;
                    }
                    i2 += 1;
                }

                // Pre-cache file2 group data for cross-product
                let group_size = i2 - group_start;
                let group2_fields: Vec<Vec<&[u8]>> = if print_paired && format.is_some() {
                    (group_start..i2)
                        .map(|j| split_fields(lines2[j], config.separator))
                        .collect()
                } else {
                    Vec::new()
                };
                // Pre-compute file2 group suffixes for default format path.
                // Avoids rescanning each line2 for field boundaries in the O(n×m) inner loop.
                let group2_suffixes: Vec<Vec<u8>> =
                    if print_paired && format.is_none() && group_size > 1 {
                        (group_start..i2)
                            .map(|j| {
                                let mut s = Vec::with_capacity(lines2[j].len());
                                write_other_fields(
                                    lines2[j],
                                    config.field2,
                                    config.separator,
                                    out_sep,
                                    &mut s,
                                );
                                s
                            })
                            .collect()
                    } else {
                        Vec::new()
                    };

                // For each file1 line with the same key, cross-product with file2 group
                loop {
                    if print_paired {
                        let key = extract_field(lines1[i1], config.field1, config.separator);
                        if let Some(specs) = format {
                            let fields1 = split_fields(lines1[i1], config.separator);
                            for fields2 in &group2_fields {
                                write_paired_format(
                                    &fields1, fields2, key, specs, empty, out_sep, delim, &mut buf,
                                );
                            }
                        } else if !group2_suffixes.is_empty() {
                            // Fast path: pre-computed suffixes avoid per-line field scanning
                            let mut prefix = Vec::with_capacity(lines1[i1].len() + 2);
                            prefix.extend_from_slice(key);
                            write_other_fields(
                                lines1[i1],
                                config.field1,
                                config.separator,
                                out_sep,
                                &mut prefix,
                            );
                            for suffix in &group2_suffixes {
                                buf.extend_from_slice(&prefix);
                                buf.extend_from_slice(suffix);
                                buf.push(delim);
                            }
                        } else {
                            // Single-element group or no group: direct zero-copy
                            for j in group_start..i2 {
                                write_paired_default_zerocopy(
                                    lines1[i1],
                                    lines2[j],
                                    key,
                                    config.field1,
                                    config.field2,
                                    config.separator,
                                    out_sep,
                                    delim,
                                    &mut buf,
                                );
                            }
                        }
                    }
                    // Flush inside cross-product loop to bound buffer for N×M groups
                    if buf.len() >= FLUSH_THRESHOLD {
                        out.write_all(&buf)?;
                        buf.clear();
                    }
                    i1 += 1;
                    if i1 >= lines1.len() {
                        break;
                    }
                    debug_assert!(i1 < keys1.len());
                    // SAFETY: i1 < lines1.len() == keys1.len() (checked above)
                    let next_key = unsafe { *keys1.get_unchecked(i1) };
                    let cmp = compare_keys(next_key, current_key, ci);
                    if cmp != Ordering::Equal {
                        // Check order: next_key should be > current_key
                        if config.order_check != OrderCheck::None
                            && !warned1
                            && cmp == Ordering::Less
                        {
                            had_order_error = true;
                            warned1 = true;
                            eprintln!(
                                "{}: {}:{}: is not sorted: {}",
                                tool_name,
                                file1_name,
                                i1 + 1,
                                String::from_utf8_lossy(lines1[i1])
                            );
                            if config.order_check == OrderCheck::Strict {
                                out.write_all(&buf)?;
                                return Ok(true);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    // Drain remaining from file 1
    while i1 < lines1.len() {
        // Check sort order even when draining (GNU join does this)
        if config.order_check != OrderCheck::None
            && !warned1
            && i1 > (if config.header { 1 } else { 0 })
        {
            let key1 = keys1[i1];
            let prev_key = keys1[i1 - 1];
            if compare_keys(key1, prev_key, ci) == Ordering::Less {
                had_order_error = true;
                warned1 = true;
                eprintln!(
                    "{}: {}:{}: is not sorted: {}",
                    tool_name,
                    file1_name,
                    i1 + 1,
                    String::from_utf8_lossy(lines1[i1])
                );
                if config.order_check == OrderCheck::Strict {
                    out.write_all(&buf)?;
                    return Ok(true);
                }
            }
        }
        if show_unpaired1 {
            if let Some(specs) = format {
                let fields1 = split_fields(lines1[i1], config.separator);
                write_unpaired_format(
                    &fields1,
                    0,
                    config.field1,
                    specs,
                    empty,
                    out_sep,
                    delim,
                    &mut buf,
                );
            } else {
                write_unpaired_default_zerocopy(
                    lines1[i1],
                    config.field1,
                    config.separator,
                    out_sep,
                    delim,
                    &mut buf,
                );
            }
        }
        i1 += 1;
    }

    // Drain remaining from file 2
    while i2 < lines2.len() {
        // Check sort order even when draining (GNU join does this)
        if config.order_check != OrderCheck::None
            && !warned2
            && i2 > (if config.header { 1 } else { 0 })
        {
            let key2 = keys2[i2];
            let prev_key = keys2[i2 - 1];
            if compare_keys(key2, prev_key, ci) == Ordering::Less {
                had_order_error = true;
                warned2 = true;
                eprintln!(
                    "{}: {}:{}: is not sorted: {}",
                    tool_name,
                    file2_name,
                    i2 + 1,
                    String::from_utf8_lossy(lines2[i2])
                );
                if config.order_check == OrderCheck::Strict {
                    out.write_all(&buf)?;
                    return Ok(true);
                }
            }
        }
        if show_unpaired2 {
            if let Some(specs) = format {
                let fields2 = split_fields(lines2[i2], config.separator);
                write_unpaired_format(
                    &fields2,
                    1,
                    config.field2,
                    specs,
                    empty,
                    out_sep,
                    delim,
                    &mut buf,
                );
            } else {
                write_unpaired_default_zerocopy(
                    lines2[i2],
                    config.field2,
                    config.separator,
                    out_sep,
                    delim,
                    &mut buf,
                );
            }
        }
        i2 += 1;
    }

    out.write_all(&buf)?;
    Ok(had_order_error)
}