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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
// fshuf -- generate random permutations
//
// Usage: shuf [OPTION]... [FILE]
//        shuf -e [OPTION]... [ARG]...
//        shuf -i LO-HI [OPTION]...

use std::fs;
use std::io::{self, Read, Write};
use std::process;

/// Write entire buffer to a raw fd, retrying on short writes.
/// Returns false on BrokenPipe (caller should stop output), propagates other errors.
#[cfg(unix)]
fn raw_write_all(fd: i32, mut data: &[u8]) -> io::Result<bool> {
    while !data.is_empty() {
        let n = unsafe { libc::write(fd, data.as_ptr() as *const _, data.len()) };
        if n < 0 {
            let err = io::Error::last_os_error();
            if err.kind() == io::ErrorKind::Interrupted {
                continue;
            }
            if err.kind() == io::ErrorKind::BrokenPipe {
                return Ok(false);
            }
            return Err(err);
        }
        data = &data[n as usize..];
    }
    Ok(true)
}

/// Write to a `dyn Write` with BrokenPipe handling.
/// Returns false on BrokenPipe (caller should stop output), propagates other errors.
fn checked_write_all(out: &mut dyn Write, data: &[u8]) -> io::Result<bool> {
    match out.write_all(data) {
        Ok(()) => Ok(true),
        Err(e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(false),
        Err(e) => Err(e),
    }
}

const TOOL_NAME: &str = "shuf";
const VERSION: &str = env!("CARGO_PKG_VERSION");

fn print_help() {
    println!("Usage: {} [OPTION]... [FILE]", TOOL_NAME);
    println!("  or:  {} -e [OPTION]... [ARG]...", TOOL_NAME);
    println!("  or:  {} -i LO-HI [OPTION]...", TOOL_NAME);
    println!("Write a random permutation of the input lines to standard output.");
    println!();
    println!("With no FILE, or when FILE is -, read standard input.");
    println!();
    println!("Mandatory arguments to long options are mandatory for short options too.");
    println!("  -e, --echo                treat each ARG as an input line");
    println!("  -i, --input-range=LO-HI   treat each number LO through HI as an input line");
    println!("  -n, --head-count=COUNT     output at most COUNT lines");
    println!("  -o, --output=FILE          write result to FILE instead of standard output");
    println!("  -r, --repeat              output lines can be repeated");
    println!("  -z, --zero-terminated      line delimiter is NUL, not newline");
    println!("      --random-source=FILE   get random bytes from FILE");
    println!("      --help     display this help and exit");
    println!("      --version  output version information and exit");
}

fn print_version() {
    println!("{} (fcoreutils) {}", TOOL_NAME, VERSION);
}

/// Random number generator matching GNU's randint algorithm.
/// When using --random-source, reads bytes from the file on each generation.
/// When no source, uses xorshift64 PRNG seeded from /dev/urandom.
enum RandGen {
    /// GNU-compatible: read bytes from file, maintain running state
    FileSource {
        reader: io::BufReader<fs::File>,
        source_path: String,
        randnum: u64,
        randmax: u64,
    },
    /// Fast PRNG for when no --random-source given
    Xorshift { state: u64 },
}

impl RandGen {
    fn from_file(path: &str) -> Self {
        let f = match fs::File::open(path) {
            Ok(f) => f,
            Err(e) => {
                eprintln!(
                    "{}: {}: {}",
                    TOOL_NAME,
                    path,
                    coreutils_rs::common::io_error_msg(&e)
                );
                process::exit(1);
            }
        };
        RandGen::FileSource {
            reader: io::BufReader::new(f),
            source_path: path.to_string(),
            randnum: 0,
            randmax: 0,
        }
    }

    fn from_urandom() -> Self {
        let mut f = match fs::File::open("/dev/urandom") {
            Ok(f) => f,
            Err(_) => {
                let t = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_nanos();
                let mut state = t as u64;
                if state == 0 {
                    state = 0x12345678_9abcdef0;
                }
                return RandGen::Xorshift { state };
            }
        };
        let mut buf = [0u8; 8];
        let _ = f.read_exact(&mut buf);
        let mut state = u64::from_le_bytes(buf);
        if state == 0 {
            state = 0x12345678_9abcdef0;
        }
        RandGen::Xorshift { state }
    }

    /// Generate a random number in [0, genmax] matching GNU's randint_genmax.
    fn gen_max(&mut self, genmax: u64) -> u64 {
        if genmax == 0 {
            return 0;
        }
        match self {
            RandGen::FileSource {
                reader,
                source_path,
                randnum,
                randmax,
            } => loop {
                if *randmax < genmax {
                    let mut buf = [0u8; 1];
                    if reader.read_exact(&mut buf).is_err() {
                        eprintln!("{}: {}: end of file", TOOL_NAME, source_path);
                        process::exit(1);
                    }
                    *randnum = randnum.wrapping_mul(256).wrapping_add(buf[0] as u64);
                    *randmax = randmax.wrapping_mul(256).wrapping_add(255);
                } else {
                    let excess_max = *randmax - genmax;
                    let excess = excess_max % (genmax + 1);
                    if excess <= *randmax - *randnum {
                        let result = *randnum % (genmax + 1);
                        *randnum /= genmax + 1;
                        *randmax = excess_max / (genmax + 1);
                        return result;
                    }
                    // Rejection: need more bytes
                    let mut buf = [0u8; 1];
                    if reader.read_exact(&mut buf).is_err() {
                        eprintln!("{}: {}: end of file", TOOL_NAME, source_path);
                        process::exit(1);
                    }
                    *randnum = randnum.wrapping_mul(256).wrapping_add(buf[0] as u64);
                    *randmax = randmax.wrapping_mul(256).wrapping_add(255);
                }
            },
            RandGen::Xorshift { state } => {
                // Lemire's nearly-divisionless bounded random:
                // Uses widening multiply (u64 * u64 → u128) to map random value
                // to [0, n) range. Avoids expensive u64 division in ~all cases.
                let s = genmax + 1;
                loop {
                    *state ^= *state << 13;
                    *state ^= *state >> 7;
                    *state ^= *state << 17;
                    let x = *state;
                    let m = (x as u128) * (s as u128);
                    let l = m as u64; // low 64 bits
                    if l < s {
                        // Rare rejection path (< 1/2^32 probability for typical s)
                        let t = s.wrapping_neg() % s;
                        if l < t {
                            continue;
                        }
                    }
                    return (m >> 64) as u64;
                }
            }
        }
    }

    /// Return a random index in [0, n)
    fn gen_range(&mut self, n: usize) -> usize {
        if n <= 1 {
            return 0;
        }
        self.gen_max((n - 1) as u64) as usize
    }
}

/// Top-down Fisher-Yates shuffle (matches GNU's algorithm)
fn shuffle<T>(items: &mut [T], rng: &mut RandGen) {
    let n = items.len();
    if n <= 1 {
        return;
    }
    for i in 0..n {
        let j = i + rng.gen_range(n - i);
        items.swap(i, j);
    }
}

fn main() {
    coreutils_rs::common::reset_sigpipe();

    let args: Vec<String> = std::env::args().skip(1).collect();

    let mut echo_mode = false;
    let mut input_range: Option<(u64, u64)> = None;
    let mut input_range_count = 0u32;
    let mut head_counts: Vec<usize> = Vec::new();
    let mut output_file: Option<String> = None;
    let mut output_file_count = 0u32;
    let mut repeat = false;
    let mut zero_terminated = false;
    let mut random_source: Option<String> = None;
    let mut positional: Vec<String> = Vec::new();
    let mut echo_args: Vec<String> = Vec::new();

    let mut i = 0;
    // First pass: check for -e/--echo to determine positional arg handling
    let has_echo = args.iter().any(|a| {
        a == "-e"
            || a == "--echo"
            || (a.starts_with('-') && !a.starts_with("--") && a.len() > 1 && a[1..].contains('e'))
    });

    // Helper: check if a long option matches (supports abbreviation, min 3 chars)
    fn match_long(arg: &str, full: &str) -> bool {
        arg == full || (arg.len() >= 3 && full.starts_with(arg))
    }

    while i < args.len() {
        let arg = &args[i];

        // Handle long options
        if arg.starts_with("--") {
            if arg == "--help" {
                print_help();
                return;
            } else if arg == "--version" {
                print_version();
                return;
            } else if match_long(arg, "--echo") {
                echo_mode = true;
            } else if match_long(arg, "--repeat") {
                repeat = true;
            } else if match_long(arg, "--zero-terminated") {
                zero_terminated = true;
            } else if arg == "--input-range" || match_long(arg, "--input-range") {
                i += 1;
                if i >= args.len() {
                    eprintln!("{}: option '--input-range' requires an argument", TOOL_NAME);
                    process::exit(1);
                }
                input_range = Some(parse_range(&args[i]));
                input_range_count += 1;
            } else if let Some(rest) = arg.strip_prefix("--input-range=") {
                input_range = Some(parse_range(rest));
                input_range_count += 1;
            } else if arg == "--head-count" || match_long(arg, "--head-count") {
                i += 1;
                if i >= args.len() {
                    eprintln!("{}: option '--head-count' requires an argument", TOOL_NAME);
                    process::exit(1);
                }
                let val = parse_count(&args[i]);
                head_counts.push(val);
            } else if let Some(rest) = arg.strip_prefix("--head-count=") {
                let val = parse_count(rest);
                head_counts.push(val);
            } else if arg == "--output" || match_long(arg, "--output") {
                i += 1;
                if i >= args.len() {
                    eprintln!("{}: option '--output' requires an argument", TOOL_NAME);
                    process::exit(1);
                }
                output_file = Some(args[i].clone());
                output_file_count += 1;
            } else if let Some(rest) = arg.strip_prefix("--output=") {
                output_file = Some(rest.to_string());
                output_file_count += 1;
            } else if arg == "--random-source" || match_long(arg, "--random-source") {
                i += 1;
                if i >= args.len() {
                    eprintln!(
                        "{}: option '--random-source' requires an argument",
                        TOOL_NAME
                    );
                    process::exit(1);
                }
                random_source = Some(args[i].clone());
            } else if let Some(rest) = arg.strip_prefix("--random-source=") {
                random_source = Some(rest.to_string());
            } else {
                eprintln!("{}: unrecognized option '{}'", TOOL_NAME, arg);
                eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                process::exit(1);
            }
        } else if arg.starts_with('-') && arg.len() > 1 {
            // Handle short options (possibly combined like -er, -rn3, etc.)
            let bytes = arg.as_bytes();
            let mut j = 1;
            while j < bytes.len() {
                match bytes[j] {
                    b'e' => echo_mode = true,
                    b'r' => repeat = true,
                    b'z' => zero_terminated = true,
                    b'i' => {
                        // Rest of this arg or next arg is the range value
                        if j + 1 < bytes.len() {
                            let rest = &arg[j + 1..];
                            input_range = Some(parse_range(rest));
                            input_range_count += 1;
                        } else {
                            i += 1;
                            if i >= args.len() {
                                eprintln!("{}: option requires an argument -- 'i'", TOOL_NAME);
                                process::exit(1);
                            }
                            input_range = Some(parse_range(&args[i]));
                            input_range_count += 1;
                        }
                        j = bytes.len(); // consumed rest
                        continue;
                    }
                    b'n' => {
                        if j + 1 < bytes.len() {
                            let rest = &arg[j + 1..];
                            let val = parse_count(rest);
                            head_counts.push(val);
                        } else {
                            i += 1;
                            if i >= args.len() {
                                eprintln!("{}: option requires an argument -- 'n'", TOOL_NAME);
                                process::exit(1);
                            }
                            let val = parse_count(&args[i]);
                            head_counts.push(val);
                        }
                        j = bytes.len();
                        continue;
                    }
                    b'o' => {
                        if j + 1 < bytes.len() {
                            let rest = &arg[j + 1..];
                            output_file = Some(rest.to_string());
                        } else {
                            i += 1;
                            if i >= args.len() {
                                eprintln!("{}: option requires an argument -- 'o'", TOOL_NAME);
                                process::exit(1);
                            }
                            output_file = Some(args[i].clone());
                        }
                        output_file_count += 1;
                        j = bytes.len();
                        continue;
                    }
                    _ => {
                        eprintln!("{}: invalid option -- '{}'", TOOL_NAME, bytes[j] as char);
                        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
                        process::exit(1);
                    }
                }
                j += 1;
            }
        } else if has_echo {
            echo_args.push(arg.clone());
        } else {
            positional.push(arg.clone());
        }
        i += 1;
    }

    // Validate option conflicts (GNU compat)
    if input_range_count > 1 {
        eprintln!("{}: multiple -i options specified", TOOL_NAME);
        process::exit(1);
    }
    if output_file_count > 1 {
        eprintln!("{}: multiple output files specified", TOOL_NAME);
        process::exit(1);
    }
    // GNU shuf: multiple -n uses the smallest value
    let head_count: Option<usize> = if head_counts.is_empty() {
        None
    } else {
        Some(*head_counts.iter().min().unwrap())
    };
    if echo_mode && input_range.is_some() {
        eprintln!("{}: cannot combine -e and -i options", TOOL_NAME);
        process::exit(1);
    }
    if input_range.is_some() && !positional.is_empty() {
        eprintln!(
            "{}: extra operand \u{2018}{}\u{2019}",
            TOOL_NAME, positional[0]
        );
        eprintln!("Try '{} --help' for more information.", TOOL_NAME);
        process::exit(1);
    }

    let mut rng = if let Some(ref source) = random_source {
        RandGen::from_file(source)
    } else {
        RandGen::from_urandom()
    };

    let delimiter = if zero_terminated { b'\0' } else { b'\n' };

    // Dispatch to concrete output type to avoid Box<dyn Write> vtable overhead.
    // This gives ~10-20% speedup on tight RNG+output loops.
    if let Some(ref outfile) = output_file {
        let f = match fs::File::create(outfile) {
            Ok(f) => f,
            Err(e) => {
                eprintln!(
                    "{}: {}: {}",
                    TOOL_NAME,
                    outfile,
                    coreutils_rs::common::io_error_msg(&e)
                );
                process::exit(1);
            }
        };
        let mut out = io::BufWriter::with_capacity(1024 * 1024, f);
        dispatch_mode(
            echo_mode,
            input_range,
            &mut echo_args,
            &positional,
            zero_terminated,
            &mut rng,
            &mut out,
            delimiter,
            head_count,
            repeat,
            output_file.is_none(),
        );
        if let Err(e) = out.flush()
            && e.kind() != std::io::ErrorKind::BrokenPipe
        {
            eprintln!("shuf: write error: {e}");
            std::process::exit(1);
        }
    } else {
        let stdout = io::stdout();
        let mut out = io::BufWriter::with_capacity(1024 * 1024, stdout.lock());
        dispatch_mode(
            echo_mode,
            input_range,
            &mut echo_args,
            &positional,
            zero_terminated,
            &mut rng,
            &mut out,
            delimiter,
            head_count,
            repeat,
            true,
        );
        if let Err(e) = out.flush()
            && e.kind() != std::io::ErrorKind::BrokenPipe
        {
            eprintln!("shuf: write error: {e}");
            std::process::exit(1);
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn dispatch_mode(
    echo_mode: bool,
    input_range: Option<(u64, u64)>,
    echo_args: &mut [String],
    positional: &[String],
    zero_terminated: bool,
    rng: &mut RandGen,
    out: &mut impl Write,
    delimiter: u8,
    head_count: Option<usize>,
    repeat: bool,
    is_stdout: bool,
) {
    if echo_mode {
        if echo_args.is_empty() && !repeat {
            return;
        }
        run_string_shuffle(echo_args, rng, out, delimiter, head_count, repeat);
    } else if let Some((lo, hi)) = input_range {
        run_range_shuffle(lo, hi, rng, out, delimiter, head_count, repeat);
    } else {
        let filename = positional.first().map(|s| s.as_str());
        run_file_shuffle(
            filename,
            zero_terminated,
            rng,
            out,
            delimiter,
            head_count,
            repeat,
            is_stdout,
        );
    }
}

fn run_string_shuffle(
    lines: &mut [String],
    rng: &mut RandGen,
    out: &mut impl Write,
    delimiter: u8,
    head_count: Option<usize>,
    repeat: bool,
) {
    if repeat {
        let count = head_count.unwrap_or(usize::MAX);
        if count == 0 {
            return;
        }
        if lines.is_empty() {
            eprintln!("{}: no lines to repeat", TOOL_NAME);
            process::exit(1);
        }
        for _ in 0..count {
            let idx = rng.gen_range(lines.len());
            match checked_write_all(out, lines[idx].as_bytes()) {
                Ok(false) => return,
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
            match checked_write_all(out, &[delimiter]) {
                Ok(false) => return,
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
        }
    } else {
        shuffle(lines, rng);
        let count = head_count.unwrap_or(lines.len()).min(lines.len());
        for line in lines.iter().take(count) {
            match checked_write_all(out, line.as_bytes()) {
                Ok(false) => return,
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
            match checked_write_all(out, &[delimiter]) {
                Ok(false) => return,
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
        }
    }
}

fn run_range_shuffle(
    lo: u64,
    hi: u64,
    rng: &mut RandGen,
    out: &mut impl Write,
    delimiter: u8,
    head_count: Option<usize>,
    repeat: bool,
) {
    let range_size = hi - lo + 1;
    let mut ibuf = itoa::Buffer::new();

    if repeat {
        let count = head_count.unwrap_or(usize::MAX);
        if count == 0 || range_size == 0 {
            return;
        }
        let batch_size = count.min(8192);
        let mut buf = Vec::with_capacity(batch_size * 12);
        for i in 0..count {
            let val = lo + rng.gen_max(range_size - 1);
            buf.extend_from_slice(ibuf.format(val).as_bytes());
            buf.push(delimiter);
            if (i + 1) % batch_size == 0 {
                match checked_write_all(out, &buf) {
                    Ok(false) => return,
                    Err(err) => {
                        eprintln!("{}: write error: {}", TOOL_NAME, err);
                        process::exit(1);
                    }
                    _ => {}
                }
                buf.clear();
            }
        }
        if !buf.is_empty() {
            match checked_write_all(out, &buf) {
                Ok(false) => process::exit(0),
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
        }
        return;
    }

    if range_size == 0 {
        return;
    }

    let count = head_count
        .unwrap_or(range_size as usize)
        .min(range_size as usize);
    if count == 0 {
        return;
    }

    // Sparse path: when count is small relative to range, pick random values directly
    // using a HashSet for deduplication. O(count) expected time and space.
    if (count as u64) < range_size / 10 || (count < 10000 && range_size > 1_000_000) {
        let mut picked = std::collections::HashSet::with_capacity(count);
        let mut result = Vec::with_capacity(count);
        while picked.len() < count {
            let val = lo + rng.gen_max(range_size - 1);
            if picked.insert(val) {
                result.push(val);
            }
        }
        let mut buf = Vec::with_capacity(count * 8);
        for val in result {
            buf.extend_from_slice(ibuf.format(val).as_bytes());
            buf.push(delimiter);
        }
        match checked_write_all(out, &buf) {
            Ok(false) => process::exit(0),
            Err(err) => {
                eprintln!("{}: write error: {}", TOOL_NAME, err);
                process::exit(1);
            }
            _ => {}
        }
    } else if hi <= u32::MAX as u64 {
        // Use u32 array when range fits — halves cache footprint for Fisher-Yates
        let lo32 = lo as u32;
        let mut values: Vec<u32> = (lo32..=(hi as u32)).collect();
        let n = values.len();
        for i in 0..count {
            let j = i + rng.gen_range(n - i);
            values.swap(i, j);
        }
        const OUT_CHUNK: usize = 2 * 1024 * 1024;
        // SAFETY: capacity has 64 bytes of headroom past OUT_CHUNK.
        // Each iteration writes at most 11 bytes (10-digit u32 + delimiter).
        // pos can enter an iteration at OUT_CHUNK - 1 (just below the flush
        // threshold), so the maximum pos after the write is OUT_CHUNK + 10,
        // an overshoot of 10 bytes — well within the 64-byte headroom.
        let mut buf: Vec<u8> = Vec::with_capacity(OUT_CHUNK + 64);
        let mut pos = 0usize;
        let mut base = buf.as_mut_ptr();
        for &val in values.iter().take(count) {
            let s = ibuf.format(val).as_bytes();
            let slen = s.len();
            unsafe {
                std::ptr::copy_nonoverlapping(s.as_ptr(), base.add(pos), slen);
                pos += slen;
                *base.add(pos) = delimiter;
                pos += 1;
            }
            if pos >= OUT_CHUNK {
                unsafe { buf.set_len(pos) };
                match checked_write_all(out, &buf) {
                    Ok(false) => return,
                    Err(err) => {
                        eprintln!("{}: write error: {}", TOOL_NAME, err);
                        process::exit(1);
                    }
                    _ => {}
                }
                buf.clear();
                pos = 0;
                // Re-derive base: the preceding buf.set_len(pos) and buf.clear()
                // take &mut self, and checked_write_all takes a shared &buf borrow.
                // All three invalidate the raw pointer under Stacked Borrows.
                // Vec::clear() does not reallocate, so the pointer value is identical,
                // but re-deriving resets the aliasing model.
                base = buf.as_mut_ptr();
            }
        }
        if pos > 0 {
            unsafe { buf.set_len(pos) };
            match checked_write_all(out, &buf) {
                Ok(false) => process::exit(0),
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
        }
    } else {
        // Dense path: generate all values and partial Fisher-Yates shuffle.
        let mut values: Vec<u64> = (lo..=hi).collect();
        let n = values.len();
        for i in 0..count {
            let j = i + rng.gen_range(n - i);
            values.swap(i, j);
        }
        // Pre-allocate output buffer sized to hold all formatted output.
        // Average number width ~7 chars for 1M range, so estimate generously.
        let est_size = count * 9;
        let mut buf = Vec::with_capacity(est_size.min(4 * 1024 * 1024));
        const OUT_CHUNK: usize = 512 * 1024;
        for &val in values.iter().take(count) {
            buf.extend_from_slice(ibuf.format(val).as_bytes());
            buf.push(delimiter);
            if buf.len() >= OUT_CHUNK {
                match checked_write_all(out, &buf) {
                    Ok(false) => return,
                    Err(err) => {
                        eprintln!("{}: write error: {}", TOOL_NAME, err);
                        process::exit(1);
                    }
                    _ => {}
                }
                buf.clear();
            }
        }
        if !buf.is_empty() {
            match checked_write_all(out, &buf) {
                Ok(false) => process::exit(0),
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
        }
    }
}

/// Sparse file shuffle: select `count` random lines from `total_lines` without
/// building the full offset array. Uses partial Fisher-Yates on line indices,
/// sorts them, then extracts lines in a single forward pass.
/// O(count) memory instead of O(total_lines).
#[allow(clippy::too_many_arguments)]
fn run_file_shuffle_sparse(
    data: &[u8],
    sep: u8,
    delimiter: u8,
    total_lines: usize,
    count: usize,
    rng: &mut RandGen,
    out: &mut impl Write,
    is_stdout: bool,
) {
    // Pick `count` unique random line indices using partial Fisher-Yates.
    // We maintain a sparse map of swapped positions to avoid O(N) array.
    let mut selected: Vec<u32> = Vec::with_capacity(count);
    let mut swaps: std::collections::HashMap<u32, u32> =
        std::collections::HashMap::with_capacity(count * 2);

    for i in 0..count {
        let j = i + rng.gen_range(total_lines - i);
        let i32 = i as u32;
        let j32 = j as u32;
        let vi = swaps.get(&i32).copied().unwrap_or(i32);
        let vj = swaps.get(&j32).copied().unwrap_or(j32);
        swaps.insert(i32, vj);
        swaps.insert(j32, vi);
        selected.push(vj);
    }
    drop(swaps);

    // Sort indices to extract lines in forward order (cache-friendly)
    // We need to remember the output order, so create (sorted_idx, output_pos) pairs.
    let mut indexed: Vec<(u32, u32)> = selected
        .iter()
        .enumerate()
        .map(|(out_pos, &line_idx)| (line_idx, out_pos as u32))
        .collect();
    indexed.sort_unstable_by_key(|&(line_idx, _)| line_idx);

    // Single forward pass: walk data with memchr, extract selected lines
    let mut results: Vec<(u32, u32, u32)> = Vec::with_capacity(count); // (out_pos, start, end)
    let mut line_num: u32 = 0;
    let mut start: u32 = 0;
    let mut sel_idx = 0;

    for pos in memchr::memchr_iter(sep, data) {
        while sel_idx < indexed.len() && indexed[sel_idx].0 == line_num {
            results.push((indexed[sel_idx].1, start, (pos + 1) as u32));
            sel_idx += 1;
        }
        if sel_idx >= indexed.len() {
            break;
        }
        start = (pos + 1) as u32;
        line_num += 1;
    }
    // Handle last line without trailing delimiter
    if sel_idx < indexed.len() {
        let end = data.len() as u32;
        while sel_idx < indexed.len() && indexed[sel_idx].0 == line_num {
            results.push((indexed[sel_idx].1, start, end));
            sel_idx += 1;
        }
    }

    // Sort by output position to restore shuffle order
    results.sort_unstable_by_key(|&(out_pos, _, _)| out_pos);

    // Write output: sep == delimiter (both \n or both \0).
    // Lines ending with sep already have the delimiter; last unterminated line needs one.
    let _ = is_stdout;
    let mut buf: Vec<u8> = Vec::with_capacity(count.min(8192) * 40);
    for &(_out_pos, s, e) in &results {
        let line = &data[s as usize..e as usize];
        buf.extend_from_slice(line);
        if line.last().copied() != Some(delimiter) {
            buf.push(delimiter);
        }
        if buf.len() >= 2 * 1024 * 1024 {
            match checked_write_all(out, &buf) {
                Ok(false) => return,
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
            buf.clear();
        }
    }
    if !buf.is_empty() {
        match checked_write_all(out, &buf) {
            Ok(false) => process::exit(0),
            Err(err) => {
                eprintln!("{}: write error: {}", TOOL_NAME, err);
                process::exit(1);
            }
            _ => {}
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn run_file_shuffle(
    filename: Option<&str>,
    zero_terminated: bool,
    rng: &mut RandGen,
    out: &mut impl Write,
    delimiter: u8,
    head_count: Option<usize>,
    repeat: bool,
    is_stdout: bool,
) {
    let data = read_file_data(filename);
    let sep = if zero_terminated { 0u8 } else { b'\n' };

    // Fast path: when -n count is small, avoid building the full offset array.
    // Instead: count lines, pick random indices, sort them, and extract in one pass.
    // This turns O(N) allocation + O(N) memchr+push into O(N) memchr count + O(count) work.
    // For -n 100 from 600K lines: avoids 4.8MB offset array allocation + page faults.
    if !repeat && let Some(count) = head_count {
        let total_lines = memchr::memchr_iter(sep, &data).count()
            + if data.last().is_some_and(|&b| b != sep) {
                1
            } else {
                0
            };
        if total_lines == 0 {
            return;
        }
        let count = count.min(total_lines);
        if count == 0 {
            return;
        }
        // Use sparse path when selecting <25% of lines (avoids full offset array)
        if (count as u64) < (total_lines as u64) / 4 {
            run_file_shuffle_sparse(
                &data,
                sep,
                delimiter,
                total_lines,
                count,
                rng,
                out,
                is_stdout,
            );
            return;
        }
    }

    // Build line index as (start, end) pairs using u32 for compactness.
    // At 8 bytes per entry (vs 16 for (usize, usize)), this halves cache footprint
    // for the Fisher-Yates shuffle where random access is the bottleneck.
    // end is delimiter-inclusive: data[start..end] includes the trailing delimiter,
    // enabling single-iovec output per line.
    let estimated_lines = data.len() / 40 + 64;
    let mut offsets: Vec<[u32; 2]> = Vec::with_capacity(estimated_lines);
    let mut start = 0usize;
    for pos in memchr::memchr_iter(sep, &data) {
        offsets.push([start as u32, (pos + 1) as u32]);
        start = pos + 1;
    }
    // Last line without trailing delimiter
    let last_needs_delim = start < data.len();
    if last_needs_delim {
        offsets.push([start as u32, data.len() as u32]);
    }

    if offsets.is_empty() && !repeat {
        return;
    }

    if repeat {
        let count = head_count.unwrap_or(usize::MAX);
        if count == 0 {
            return;
        }
        if offsets.is_empty() {
            eprintln!("{}: no lines to repeat", TOOL_NAME);
            process::exit(1);
        }
        let last_idx = offsets.len() - 1;
        const CHUNK: usize = 2 * 1024 * 1024;
        let mut buf: Vec<u8> = Vec::with_capacity(CHUNK + 256);
        let src = data.as_ptr();
        for _ in 0..count {
            let idx = rng.gen_range(offsets.len());
            let [s, e] = offsets[idx];
            let span = (e - s) as usize;
            // For delimiter-inclusive entries, span includes the delimiter.
            // For last line without delimiter, append it manually.
            // In the repeat path, offsets are never shuffled, so offsets[last_idx]
            // is always the unterminated last line entry. Equivalent to
            // `e as usize == data.len()` (see invariant in the non-repeat path),
            // but cheaper to check.
            let needs_extra = last_needs_delim && idx == last_idx;
            let needed = buf.len() + span + needs_extra as usize;
            if needed > CHUNK && !buf.is_empty() {
                match checked_write_all(out, &buf) {
                    Ok(false) => return,
                    Err(err) => {
                        eprintln!("{}: write error: {}", TOOL_NAME, err);
                        process::exit(1);
                    }
                    _ => {}
                }
                buf.clear();
            }
            let pos = buf.len();
            buf.reserve(span + needs_extra as usize);
            unsafe {
                let dst = buf.as_mut_ptr().add(pos);
                std::ptr::copy_nonoverlapping(src.add(s as usize), dst, span);
                if needs_extra {
                    *dst.add(span) = delimiter;
                    buf.set_len(pos + span + 1);
                } else {
                    buf.set_len(pos + span);
                }
            }
        }
        if !buf.is_empty() {
            match checked_write_all(out, &buf) {
                Ok(false) => process::exit(0),
                Err(err) => {
                    eprintln!("{}: write error: {}", TOOL_NAME, err);
                    process::exit(1);
                }
                _ => {}
            }
        }
    } else {
        let n = offsets.len();
        let count = head_count.unwrap_or(n).min(n);

        // Partial Fisher-Yates: only do `count` rounds instead of shuffling all N.
        // O(count) random swaps instead of O(N). Huge win for -n 100 from 200K lines.
        for i in 0..count {
            let j = i + rng.gen_range(n - i);
            offsets.swap(i, j);
        }

        #[cfg(unix)]
        {
            // Contiguous output buffer with raw write_all_fd.
            // Reduces syscalls from ~3900 writev to ~50 write for 100MB output.
            let out_fd: i32 = if is_stdout {
                let _ = out.flush();
                1
            } else {
                -1
            };

            if out_fd >= 0 {
                const BUF_SIZE: usize = 2 * 1024 * 1024;
                let mut buf: Vec<u8> = Vec::with_capacity(BUF_SIZE + 256);
                let src = data.as_ptr();
                let offsets_slice = &offsets[..count];
                const PREFETCH_DIST: usize = 16;

                for (idx, &[s, e]) in offsets_slice.iter().enumerate() {
                    if idx + PREFETCH_DIST < count {
                        let future_s = offsets_slice[idx + PREFETCH_DIST][0] as usize;
                        #[cfg(target_arch = "x86_64")]
                        unsafe {
                            std::arch::x86_64::_mm_prefetch(
                                src.add(future_s) as *const i8,
                                std::arch::x86_64::_MM_HINT_T1,
                            );
                        }
                    }

                    let span = (e - s) as usize;
                    // e == data.len() uniquely identifies the original unterminated last line.
                    // No delimiter-inclusive entry can satisfy this: they have e = start + len + 1
                    // where the +1 accounts for the delimiter byte, so e <= data.len() only if the
                    // last byte IS the delimiter — but then last_needs_delim is false, short-circuiting.
                    let needs_extra = last_needs_delim && e as usize == data.len();
                    let total = span + needs_extra as usize;

                    if buf.len() + total > BUF_SIZE && !buf.is_empty() {
                        match raw_write_all(out_fd, &buf) {
                            Ok(false) => return,
                            Err(err) => {
                                eprintln!("{}: write error: {}", TOOL_NAME, err);
                                process::exit(1);
                            }
                            _ => {}
                        }
                        buf.clear();
                    }
                    buf.reserve(total);
                    let pos = buf.len();
                    unsafe {
                        let dst = buf.as_mut_ptr().add(pos);
                        std::ptr::copy_nonoverlapping(src.add(s as usize), dst, span);
                        if needs_extra {
                            *dst.add(span) = delimiter;
                            buf.set_len(pos + span + 1);
                        } else {
                            buf.set_len(pos + span);
                        }
                    }
                }
                if !buf.is_empty() {
                    match raw_write_all(out_fd, &buf) {
                        Ok(false) => process::exit(0),
                        Err(err) => {
                            eprintln!("{}: write error: {}", TOOL_NAME, err);
                            process::exit(1);
                        }
                        _ => {}
                    }
                }
            } else {
                // Fallback: copy-based path for non-stdout output
                const CHUNK: usize = 2 * 1024 * 1024;
                let mut buf: Vec<u8> = Vec::with_capacity(CHUNK + 256);
                let src = data.as_ptr();
                let offsets_slice = &offsets[..count];
                const PREFETCH_DIST: usize = 16;

                for (idx, &[s, e]) in offsets_slice.iter().enumerate() {
                    if idx + PREFETCH_DIST < count {
                        let future_s = offsets_slice[idx + PREFETCH_DIST][0] as usize;
                        #[cfg(target_arch = "x86_64")]
                        unsafe {
                            std::arch::x86_64::_mm_prefetch(
                                src.add(future_s) as *const i8,
                                std::arch::x86_64::_MM_HINT_T1,
                            );
                        }
                    }
                    let span = (e - s) as usize;
                    // See invariant comment in unix-stdout path above: e == data.len()
                    // uniquely identifies the unterminated last line.
                    let needs_extra = last_needs_delim && e as usize == data.len();
                    let total = span + needs_extra as usize;
                    if buf.len() + total > CHUNK && !buf.is_empty() {
                        match checked_write_all(out, &buf) {
                            Ok(false) => return,
                            Err(err) => {
                                eprintln!("{}: write error: {}", TOOL_NAME, err);
                                process::exit(1);
                            }
                            _ => {}
                        }
                        buf.clear();
                    }
                    buf.reserve(total);
                    let pos = buf.len();
                    unsafe {
                        let dst = buf.as_mut_ptr().add(pos);
                        std::ptr::copy_nonoverlapping(src.add(s as usize), dst, span);
                        if needs_extra {
                            *dst.add(span) = delimiter;
                            buf.set_len(pos + span + 1);
                        } else {
                            buf.set_len(pos + span);
                        }
                    }
                }
                if !buf.is_empty() {
                    match checked_write_all(out, &buf) {
                        Ok(false) => process::exit(0),
                        Err(err) => {
                            eprintln!("{}: write error: {}", TOOL_NAME, err);
                            process::exit(1);
                        }
                        _ => {}
                    }
                }
            }
        }

        #[cfg(not(unix))]
        {
            const OUT_CHUNK: usize = 2 * 1024 * 1024;
            let mut buf = Vec::with_capacity(OUT_CHUNK + 256);
            for &[s, e] in offsets[..count].iter() {
                let span = (e - s) as usize;
                // See invariant comment in unix-stdout path above: e == data.len()
                // uniquely identifies the unterminated last line.
                let needs_extra = last_needs_delim && e as usize == data.len();
                let total = span + needs_extra as usize;
                if buf.len() + total > OUT_CHUNK && !buf.is_empty() {
                    match checked_write_all(out, &buf) {
                        Ok(false) => return,
                        Err(err) => {
                            eprintln!("{}: write error: {}", TOOL_NAME, err);
                            process::exit(1);
                        }
                        _ => {}
                    }
                    buf.clear();
                }
                buf.reserve(total);
                buf.extend_from_slice(&data[s as usize..e as usize]);
                if needs_extra {
                    buf.push(delimiter);
                }
            }
            if !buf.is_empty() {
                match checked_write_all(out, &buf) {
                    Ok(false) => process::exit(0),
                    Err(err) => {
                        eprintln!("{}: write error: {}", TOOL_NAME, err);
                        process::exit(1);
                    }
                    _ => {}
                }
            }
        }
    }
}

fn read_file_data(filename: Option<&str>) -> coreutils_rs::common::io::FileData {
    match filename {
        Some("-") | None => {
            let mut buf = Vec::new();
            io::stdin().lock().read_to_end(&mut buf).unwrap_or(0);
            coreutils_rs::common::io::FileData::Owned(buf)
        }
        Some(file) => match coreutils_rs::common::io::read_file(std::path::Path::new(file)) {
            Ok(data) => data,
            Err(e) => {
                eprintln!(
                    "{}: {}: {}",
                    TOOL_NAME,
                    file,
                    coreutils_rs::common::io_error_msg(&e)
                );
                process::exit(1);
            }
        },
    }
}

fn parse_range(s: &str) -> (u64, u64) {
    // Find the separator '-' that's not part of a negative number sign
    // Format: LO-HI where LO and HI are non-negative integers
    let sep_pos = if let Some(rest) = s.strip_prefix('-') {
        // First char is '-' (could be negative, which is invalid for u64 range)
        // Look for next '-' as separator
        match rest.find('-') {
            Some(p) => p + 1,
            None => {
                eprintln!("{}: invalid input range: \u{2018}{}\u{2019}", TOOL_NAME, s);
                process::exit(1);
            }
        }
    } else {
        match s.find('-') {
            Some(p) => p,
            None => {
                eprintln!("{}: invalid input range: \u{2018}{}\u{2019}", TOOL_NAME, s);
                process::exit(1);
            }
        }
    };

    let lo_str = &s[..sep_pos];
    let hi_str = &s[sep_pos + 1..];

    let lo: u64 = match lo_str.parse() {
        Ok(v) => v,
        Err(_) => {
            eprintln!("{}: invalid input range: \u{2018}{}\u{2019}", TOOL_NAME, s);
            process::exit(1);
        }
    };
    let hi: u64 = match hi_str.parse() {
        Ok(v) => v,
        Err(_) => {
            eprintln!("{}: invalid input range: \u{2018}{}\u{2019}", TOOL_NAME, s);
            process::exit(1);
        }
    };
    if lo > hi {
        eprintln!("{}: invalid input range: \u{2018}{}\u{2019}", TOOL_NAME, s);
        process::exit(1);
    }
    (lo, hi)
}

fn parse_count(s: &str) -> usize {
    match s.parse() {
        Ok(v) => v,
        Err(_) => {
            eprintln!("{}: invalid line count: '{}'", TOOL_NAME, s);
            process::exit(1);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;
    use std::io::Write;
    use std::process::Command;

    fn cmd() -> Command {
        let mut path = std::env::current_exe().unwrap();
        path.pop();
        path.pop();
        path.push("fshuf");
        Command::new(path)
    }

    #[test]
    fn test_basic_shuffle() {
        let mut child = cmd()
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        {
            let stdin = child.stdin.as_mut().unwrap();
            stdin.write_all(b"a\nb\nc\nd\ne\n").unwrap();
        }
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: HashSet<&str> = stdout.trim().lines().collect();
        let expected: HashSet<&str> = ["a", "b", "c", "d", "e"].iter().copied().collect();
        assert_eq!(
            lines, expected,
            "All elements should be present after shuffle"
        );
    }

    #[test]
    fn test_echo_mode() {
        let output = cmd().args(["-e", "x", "y", "z"]).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: HashSet<&str> = stdout.trim().lines().collect();
        let expected: HashSet<&str> = ["x", "y", "z"].iter().copied().collect();
        assert_eq!(lines, expected);
    }

    #[test]
    fn test_input_range() {
        let output = cmd().args(["-i", "1-5"]).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut lines: Vec<i32> = stdout.trim().lines().map(|l| l.parse().unwrap()).collect();
        lines.sort();
        assert_eq!(lines, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_head_count() {
        let output = cmd().args(["-i", "1-100", "-n", "5"]).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 5);
    }

    #[test]
    fn test_repeat() {
        let output = cmd()
            .args(["-e", "-r", "-n", "10", "a", "b"])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 10);
        for line in &lines {
            assert!(
                *line == "a" || *line == "b",
                "Expected 'a' or 'b', got '{}'",
                line
            );
        }
    }

    #[test]
    fn test_zero_terminated() {
        let mut child = cmd()
            .args(["-z"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        {
            let stdin = child.stdin.as_mut().unwrap();
            stdin.write_all(b"a\0b\0c\0").unwrap();
        }
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = &output.stdout;
        // Output should be NUL-terminated
        let items: HashSet<&[u8]> = stdout
            .split(|&b| b == 0)
            .filter(|s| !s.is_empty())
            .collect();
        assert_eq!(items.len(), 3);
        assert!(items.contains(&b"a"[..]));
        assert!(items.contains(&b"b"[..]));
        assert!(items.contains(&b"c"[..]));
    }

    #[test]
    fn test_output_file() {
        let dir = tempfile::tempdir().unwrap();
        let outpath = dir.path().join("output.txt");
        let output = cmd()
            .args(["-e", "hello", "world", "-o", outpath.to_str().unwrap()])
            .output()
            .unwrap();
        assert!(output.status.success());
        let contents = std::fs::read_to_string(&outpath).unwrap();
        let lines: HashSet<&str> = contents.trim().lines().collect();
        assert!(lines.contains("hello"));
        assert!(lines.contains("world"));
    }
    #[test]
    fn test_match_gnu_format() {
        // GNU shuf -i 1-5 output should have exactly 5 lines, each a number 1-5
        let gnu = Command::new("shuf").args(["-i", "1-5"]).output();
        if let Ok(gnu) = gnu {
            // Both should produce 5 lines with numbers 1-5
            let gnu_lines: Vec<i32> = String::from_utf8_lossy(&gnu.stdout)
                .trim()
                .lines()
                .map(|l| l.parse().unwrap())
                .collect();
            assert_eq!(gnu_lines.len(), 5);

            let ours = cmd().args(["-i", "1-5"]).output().unwrap();
            let our_lines: Vec<i32> = String::from_utf8_lossy(&ours.stdout)
                .trim()
                .lines()
                .map(|l| l.parse().unwrap())
                .collect();
            assert_eq!(our_lines.len(), 5);

            let mut gnu_sorted = gnu_lines;
            gnu_sorted.sort();
            let mut our_sorted = our_lines;
            our_sorted.sort();
            assert_eq!(gnu_sorted, our_sorted, "Same set of numbers");
        }
    }

    // --- GNU compatibility: option conflict validation ---

    #[test]
    fn test_i_and_e_conflict() {
        let output = cmd().args(["-e", "-i", "0-9", "a"]).output().unwrap();
        assert!(!output.status.success(), "-i and -e together should error");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("cannot combine -e and -i"),
            "stderr: {}",
            stderr
        );
    }

    #[test]
    fn test_multiple_i_is_error() {
        let output = cmd().args(["-i", "0-1", "-i", "0-2"]).output().unwrap();
        assert!(!output.status.success(), "multiple -i should error");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("multiple -i"), "stderr: {}", stderr);
    }

    #[test]
    fn test_i_with_extra_operand() {
        let output = cmd().args(["-i", "0-0", "foo"]).output().unwrap();
        assert!(
            !output.status.success(),
            "-i with extra operand should error"
        );
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("extra operand"), "stderr: {}", stderr);
    }

    #[test]
    fn test_multiple_o_is_error() {
        // GNU shuf rejects multiple -o (exit 1)
        let dir = tempfile::tempdir().unwrap();
        let p1 = dir.path().join("fshuf_multi_o_1.txt");
        let p2 = dir.path().join("fshuf_multi_o_2.txt");
        let output = cmd()
            .args([
                "-i",
                "0-0",
                "-o",
                p1.to_str().unwrap(),
                "-o",
                p2.to_str().unwrap(),
            ])
            .output()
            .unwrap();
        assert!(!output.status.success(), "multiple -o should error");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("multiple output files"),
            "stderr: {}",
            stderr
        );
    }

    #[test]
    fn test_multiple_n_uses_smallest() {
        // GNU shuf uses the smallest -n value when multiple are specified
        let output = cmd()
            .args(["-n", "10", "-i", "0-9", "-n", "3", "-n", "20"])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 3, "multiple -n should use smallest value");
    }

    // --- GNU compatibility: --repeat feature ---

    #[test]
    fn test_repeat_input_range_count() {
        let output = cmd()
            .args(["--repeat", "-i", "0-9", "-n", "1000"])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 1000, "should output exactly 1000 lines");
        for line in &lines {
            let num: u64 = line.parse().expect("each line should be a number");
            assert!(num <= 9, "number {} out of range 0-9", num);
        }
    }

    #[test]
    fn test_repeat_stdin_n0_empty() {
        let mut child = cmd()
            .args(["--repeat", "-n", "0"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        {
            let stdin = child.stdin.as_mut().unwrap();
            stdin.write_all(b"a\nb\n").unwrap();
        }
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        assert!(
            output.stdout.is_empty(),
            "--repeat -n0 should produce no output"
        );
    }

    #[test]
    fn test_repeat_input_range_222_233() {
        let output = cmd()
            .args(["--repeat", "-i", "222-233", "-n", "2000"])
            .output()
            .unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 2000);
        for line in &lines {
            let num: u64 = line.parse().expect("each line should be a number");
            assert!(
                (222..=233).contains(&num),
                "number {} out of range 222-233",
                num
            );
        }
    }

    #[test]
    fn test_repeat_stdin_count() {
        let mut child = cmd()
            .args(["--repeat", "-n", "2000"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        {
            let stdin = child.stdin.as_mut().unwrap();
            stdin.write_all(b"a\nb\nc\n").unwrap();
        }
        let output = child.wait_with_output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        let lines: Vec<&str> = stdout.trim().lines().collect();
        assert_eq!(lines.len(), 2000);
        for line in &lines {
            assert!(
                *line == "a" || *line == "b" || *line == "c",
                "unexpected line: {}",
                line
            );
        }
    }

    #[test]
    fn test_input_range_single() {
        let output = cmd().args(["-i", "5-5"]).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert_eq!(stdout.trim(), "5");
    }

    #[test]
    fn test_head_count_zero() {
        let output = cmd().args(["-i", "1-10", "-n", "0"]).output().unwrap();
        assert!(output.status.success());
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert_eq!(stdout, "");
    }
}