gnu-sort 1.0.5

High-performance Rust implementation of GNU sort with zero-copy operations, SIMD optimization, and parallel processing
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
//! Configuration management for sort operations

use crate::error::{SortError, SortResult};
use std::str::FromStr;

/// Sort key specification for field-based sorting
#[derive(Debug, Clone)]
pub struct SortKey {
    /// Starting field number (1-based)
    pub start_field: usize,
    /// Starting character position within field (1-based, optional)
    pub start_char: Option<usize>,
    /// Ending field number (1-based, optional)
    pub end_field: Option<usize>,
    /// Ending character position within field (1-based, optional)
    pub end_char: Option<usize>,
    /// Sort options specific to this key
    pub options: SortKeyOptions,
}

/// Options specific to a sort key
#[derive(Debug, Clone, Default)]
pub struct SortKeyOptions {
    pub numeric: bool,
    pub general_numeric: bool,
    pub month: bool,
    pub reverse: bool,
    pub ignore_case: bool,
    pub dictionary_order: bool,
    pub ignore_leading_blanks: bool,
    pub human_numeric: bool,
    pub version: bool,
    pub random: bool,
}

impl SortKey {
    /// Parse a sort key from a string like "2,4" or "1.3,1.5" or "2nr"
    pub fn parse(keydef: &str) -> SortResult<Self> {
        // Split by comma to get start and optional end
        let parts: Vec<&str> = keydef.split(',').collect();
        if parts.is_empty() || parts.len() > 2 {
            return Err(SortError::parse_error(&format!(
                "invalid key specification: {keydef}"
            )));
        }

        // Parse start position and options
        let (start_field, start_char, start_opts) = Self::parse_field_spec(parts[0])?;

        // Parse end position if present
        let (end_field, end_char, end_opts) = if parts.len() == 2 {
            let (field, char_pos, opts) = Self::parse_field_spec(parts[1])?;
            (Some(field), char_pos, opts)
        } else {
            (None, None, SortKeyOptions::default())
        };

        // Merge options (start options take precedence)
        let mut options = start_opts;
        // Apply end options only if they're set and start options aren't
        if !options.numeric {
            options.numeric = end_opts.numeric;
        }
        if !options.general_numeric {
            options.general_numeric = end_opts.general_numeric;
        }
        if !options.month {
            options.month = end_opts.month;
        }
        if !options.reverse {
            options.reverse = end_opts.reverse;
        }
        if !options.ignore_case {
            options.ignore_case = end_opts.ignore_case;
        }
        if !options.dictionary_order {
            options.dictionary_order = end_opts.dictionary_order;
        }
        if !options.ignore_leading_blanks {
            options.ignore_leading_blanks = end_opts.ignore_leading_blanks;
        }
        if !options.human_numeric {
            options.human_numeric = end_opts.human_numeric;
        }
        if !options.version {
            options.version = end_opts.version;
        }
        if !options.random {
            options.random = end_opts.random;
        }

        Ok(Self {
            start_field,
            start_char,
            end_field,
            end_char,
            options,
        })
    }

    /// Parse a field specification like "2" or "2.3" or "2nr"
    fn parse_field_spec(spec: &str) -> SortResult<(usize, Option<usize>, SortKeyOptions)> {
        if spec.is_empty() {
            return Err(SortError::parse_error("empty field specification"));
        }

        let mut chars = spec.chars().peekable();
        let mut field_str = String::new();
        let mut char_str = String::new();
        let mut options = SortKeyOptions::default();

        // Parse field number
        while let Some(&ch) = chars.peek() {
            if ch.is_ascii_digit() {
                field_str.push(ch);
                chars.next();
            } else {
                break;
            }
        }

        if field_str.is_empty() {
            return Err(SortError::parse_error(&format!(
                "invalid field specification: {spec}"
            )));
        }

        let field = field_str
            .parse::<usize>()
            .map_err(|_| SortError::parse_error(&format!("invalid field number: {field_str}")))?;

        if field == 0 {
            return Err(SortError::parse_error("field numbers start at 1"));
        }

        // Check for character position (after a dot)
        let char_pos = if chars.peek() == Some(&'.') {
            chars.next(); // consume the dot
            while let Some(&ch) = chars.peek() {
                if ch.is_ascii_digit() {
                    char_str.push(ch);
                    chars.next();
                } else {
                    break;
                }
            }

            if char_str.is_empty() {
                None
            } else {
                let pos = char_str.parse::<usize>().map_err(|_| {
                    SortError::parse_error(&format!("invalid character position: {char_str}"))
                })?;
                if pos == 0 {
                    return Err(SortError::parse_error("character positions start at 1"));
                }
                Some(pos)
            }
        } else {
            None
        };

        // Parse options (single letters after the field spec)
        for ch in chars {
            match ch {
                'n' => options.numeric = true,
                'g' => options.general_numeric = true,
                'M' => options.month = true,
                'r' => options.reverse = true,
                'f' => options.ignore_case = true,
                'd' => options.dictionary_order = true,
                'b' => options.ignore_leading_blanks = true,
                'h' => options.human_numeric = true,
                'V' => options.version = true,
                'R' => options.random = true,
                'i' => {} // ignore non-printing - not fully implemented
                'z' => {} // zero-terminated - handled globally
                _ => {
                    return Err(SortError::parse_error(&format!("invalid key option: {ch}")));
                }
            }
        }

        Ok((field, char_pos, options))
    }
}

/// Main configuration structure for sort operations
#[derive(Debug, Clone)]
pub struct SortConfig {
    /// Primary sort mode
    pub mode: SortMode,
    /// Sort order (normal or reverse)
    pub reverse: bool,
    /// Output only unique lines
    pub unique: bool,
    /// Use stable sort algorithm
    pub stable: bool,
    /// Check if input is already sorted
    pub check: bool,
    /// Merge already sorted files
    pub merge: bool,
    /// Use zero bytes as line terminators instead of newlines
    pub zero_terminated: bool,
    /// Ignore case differences
    pub ignore_case: bool,
    /// Consider only dictionary order (alphanumeric and blanks)
    pub dictionary_order: bool,
    /// Ignore leading blanks
    pub ignore_leading_blanks: bool,
    /// Ignore non-printing characters
    pub ignore_nonprinting: bool,
    /// Field separator character
    pub field_separator: Option<char>,
    /// Sort keys (field specifications)
    pub keys: Vec<SortKey>,
    /// Output file path
    pub output_file: Option<String>,
    /// Buffer size for I/O operations
    pub buffer_size: Option<usize>,
    /// Number of parallel threads to use
    pub parallel_threads: Option<usize>,
    /// Files to read from (if not specified, use stdin)
    pub input_files: Vec<String>,
    /// Debug mode (for troubleshooting)
    pub debug: bool,
    /// Compress temporary files
    pub compress_temp: bool,
    /// Temporary directory for external sorting
    pub temp_dir: Option<String>,
}

/// Sort mode enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortMode {
    /// Standard lexicographic sorting
    Lexicographic,
    /// Numeric sorting (integers)
    Numeric,
    /// General numeric sorting (floating point)
    GeneralNumeric,
    /// Human-readable numeric sorting (with suffixes like K, M, G)
    HumanNumeric,
    /// Month name sorting
    Month,
    /// Version number sorting
    Version,
    /// Random sorting (but group identical keys)
    Random,
}

/// Sort order enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortOrder {
    Ascending,
    Descending,
}

impl Default for SortConfig {
    fn default() -> Self {
        Self {
            mode: SortMode::Lexicographic,
            reverse: false,
            unique: false,
            stable: false,
            check: false,
            merge: false,
            zero_terminated: false,
            ignore_case: false,
            dictionary_order: false,
            ignore_leading_blanks: false,
            ignore_nonprinting: false,
            field_separator: None,
            keys: Vec::new(),
            output_file: None,
            buffer_size: None,
            parallel_threads: None,
            input_files: Vec::new(),
            debug: false,
            compress_temp: false,
            temp_dir: None,
        }
    }
}

impl SortConfig {
    /// Create a new configuration with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the sort mode
    pub fn with_mode(mut self, mode: SortMode) -> Self {
        self.mode = mode;
        self
    }

    /// Enable reverse sorting
    pub fn with_reverse(mut self, reverse: bool) -> Self {
        self.reverse = reverse;
        self
    }

    /// Enable unique output
    pub fn with_unique(mut self, unique: bool) -> Self {
        self.unique = unique;
        self
    }

    /// Enable stable sorting
    pub fn with_stable(mut self, stable: bool) -> Self {
        self.stable = stable;
        self
    }

    /// Enable check mode
    pub fn with_check(mut self, check: bool) -> Self {
        self.check = check;
        self
    }

    /// Enable merge mode
    pub fn with_merge(mut self, merge: bool) -> Self {
        self.merge = merge;
        self
    }

    /// Enable zero-terminated lines
    pub fn with_zero_terminated(mut self, zero_terminated: bool) -> Self {
        self.zero_terminated = zero_terminated;
        self
    }

    /// Set field separator
    pub fn with_field_separator(mut self, separator: Option<char>) -> Self {
        self.field_separator = separator;
        self
    }

    /// Add a sort key
    pub fn add_key(mut self, key: SortKey) -> Self {
        self.keys.push(key);
        self
    }

    /// Set output file
    pub fn with_output_file(mut self, output_file: Option<String>) -> Self {
        self.output_file = output_file;
        self
    }

    /// Set buffer size
    pub fn with_buffer_size(mut self, buffer_size: Option<usize>) -> Self {
        self.buffer_size = buffer_size;
        self
    }

    /// Set parallel threads
    pub fn with_parallel_threads(mut self, threads: Option<usize>) -> Self {
        self.parallel_threads = threads;
        self
    }

    /// Set input files
    pub fn with_input_files(mut self, files: Vec<String>) -> Self {
        self.input_files = files;
        self
    }

    /// Enable debug mode
    pub fn with_debug(mut self, debug: bool) -> Self {
        self.debug = debug;
        self
    }

    /// Parse buffer size from string (simplified)
    pub fn set_buffer_size_from_string(&mut self, size_str: &str) -> SortResult<()> {
        // Simple parsing for now - just parse as number
        let size = size_str
            .parse::<usize>()
            .map_err(|_| SortError::internal("Invalid buffer size"))?;
        self.buffer_size = Some(size);
        Ok(())
    }

    /// Validate configuration for consistency
    pub fn validate(&self) -> SortResult<()> {
        // Check for conflicting modes
        if self.check && self.merge {
            return Err(SortError::conflicting_options(
                "cannot use both --check and --merge",
            ));
        }

        if self.check && self.unique {
            return Err(SortError::conflicting_options(
                "--check is incompatible with --unique",
            ));
        }

        if self.merge && self.unique {
            // This is actually allowed, but warn about performance implications
        }

        // Validate field separator
        if let Some(sep) = self.field_separator {
            if sep == '\0' && !self.zero_terminated {
                return Err(SortError::invalid_field_separator(
                    "null character separator requires -z option",
                ));
            }
        }

        // Check for reasonable buffer size
        if let Some(buffer_size) = self.buffer_size {
            if buffer_size < 1024 {
                return Err(SortError::invalid_buffer_size(
                    "buffer size too small (minimum 1KB)",
                ));
            }
            // Use u64 to avoid overflow on 32-bit systems
            const MAX_BUFFER_SIZE: u64 = 8 * 1024 * 1024 * 1024; // 8GB
            if buffer_size as u64 > MAX_BUFFER_SIZE {
                return Err(SortError::invalid_buffer_size(
                    "buffer size too large (maximum 8GB)",
                ));
            }
        }

        // Validate thread count
        if let Some(threads) = self.parallel_threads {
            if threads == 0 {
                return Err(SortError::thread_pool_error(
                    "thread count must be positive",
                ));
            }
            if threads > 1024 {
                return Err(SortError::thread_pool_error(
                    "too many threads (maximum 1024)",
                ));
            }
        }

        Ok(())
    }

    /// Get the effective sort order
    pub fn sort_order(&self) -> SortOrder {
        if self.reverse {
            SortOrder::Descending
        } else {
            SortOrder::Ascending
        }
    }

    /// Check if random sort is enabled
    pub fn random_sort(&self) -> bool {
        matches!(self.mode, SortMode::Random)
    }

    /// Check if numeric sort mode is enabled
    pub fn numeric_sort(&self) -> bool {
        matches!(
            self.mode,
            SortMode::Numeric | SortMode::GeneralNumeric | SortMode::HumanNumeric
        )
    }

    /// Check if any keys have specific sort types
    pub fn has_typed_keys(&self) -> bool {
        false // Simplified - no complex key checking
    }

    /// Get the number of input files (0 means stdin)
    pub fn input_file_count(&self) -> usize {
        self.input_files.len()
    }

    /// Check if reading from stdin
    pub fn reading_from_stdin(&self) -> bool {
        self.input_files.is_empty() || (self.input_files.len() == 1 && self.input_files[0] == "-")
    }

    /// Check if writing to stdout
    pub fn writing_to_stdout(&self) -> bool {
        self.output_file.is_none()
    }

    /// Get effective buffer size (with default)
    pub fn effective_buffer_size(&self) -> usize {
        self.buffer_size.unwrap_or(1024 * 1024) // 1MB default
    }

    /// Get effective thread count
    pub fn effective_thread_count(&self) -> usize {
        self.parallel_threads.unwrap_or_else(num_cpus::get)
    }

    /// Create a configuration for merge operations
    pub fn for_merge(&self) -> Self {
        let mut config = self.clone();
        config.merge = true;
        config.check = false;
        config
    }

    /// Create a configuration for check operations
    pub fn for_check(&self) -> Self {
        let mut config = self.clone();
        config.check = true;
        config.merge = false;
        config.unique = false; // Not applicable for check
        config
    }
}

impl FromStr for SortMode {
    type Err = SortError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "lexicographic" | "text" | "default" => Ok(SortMode::Lexicographic),
            "numeric" | "n" => Ok(SortMode::Numeric),
            "general-numeric" | "g" => Ok(SortMode::GeneralNumeric),
            "human-numeric" | "h" => Ok(SortMode::HumanNumeric),
            "month" | "m" => Ok(SortMode::Month),
            "version" | "v" => Ok(SortMode::Version),
            "random" | "r" => Ok(SortMode::Random),
            _ => Err(SortError::parse_error(&format!("unknown sort mode: {s}"))),
        }
    }
}

impl std::fmt::Display for SortMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            SortMode::Lexicographic => "lexicographic",
            SortMode::Numeric => "numeric",
            SortMode::GeneralNumeric => "general-numeric",
            SortMode::HumanNumeric => "human-numeric",
            SortMode::Month => "month",
            SortMode::Version => "version",
            SortMode::Random => "random",
        };
        write!(f, "{name}")
    }
}

/// Builder pattern for creating configurations
pub struct SortConfigBuilder {
    config: SortConfig,
}

impl SortConfigBuilder {
    /// Start building a new configuration
    pub fn new() -> Self {
        Self {
            config: SortConfig::default(),
        }
    }

    /// Set sort mode
    pub fn mode(mut self, mode: SortMode) -> Self {
        self.config.mode = mode;
        self
    }

    /// Enable reverse sorting
    pub fn reverse(mut self) -> Self {
        self.config.reverse = true;
        self
    }

    /// Enable unique output
    pub fn unique(mut self) -> Self {
        self.config.unique = true;
        self
    }

    /// Enable stable sorting
    pub fn stable(mut self) -> Self {
        self.config.stable = true;
        self
    }

    /// Enable check mode
    pub fn check(mut self) -> Self {
        self.config.check = true;
        self
    }

    /// Enable merge mode
    pub fn merge(mut self) -> Self {
        self.config.merge = true;
        self
    }

    /// Enable zero-terminated lines
    pub fn zero_terminated(mut self) -> Self {
        self.config.zero_terminated = true;
        self
    }

    /// Set field separator
    pub fn field_separator(mut self, separator: char) -> Self {
        self.config.field_separator = Some(separator);
        self
    }

    /// Add a sort key
    pub fn key(mut self, key: SortKey) -> Self {
        self.config.keys.push(key);
        self
    }

    /// Set output file
    pub fn output_file(mut self, file: String) -> Self {
        self.config.output_file = Some(file);
        self
    }

    /// Set buffer size
    pub fn buffer_size(mut self, size: usize) -> Self {
        self.config.buffer_size = Some(size);
        self
    }

    /// Build the final configuration
    pub fn build(self) -> SortResult<SortConfig> {
        self.config.validate()?;
        Ok(self.config)
    }
}

impl Default for SortConfigBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Preset configurations for common use cases
pub mod presets {
    use super::*;

    /// Configuration for numeric sorting
    pub fn numeric() -> SortConfig {
        SortConfig::new().with_mode(SortMode::Numeric)
    }

    /// Configuration for version sorting
    pub fn version() -> SortConfig {
        SortConfig::new().with_mode(SortMode::Version)
    }

    /// Configuration for human-readable sizes
    pub fn human_numeric() -> SortConfig {
        SortConfig::new().with_mode(SortMode::HumanNumeric)
    }

    /// Configuration for case-insensitive sorting
    pub fn case_insensitive() -> SortConfig {
        let mut config = SortConfig::new();
        config.ignore_case = true;
        config
    }

    /// Configuration for sorting with unique output
    pub fn unique() -> SortConfig {
        SortConfig::new().with_unique(true)
    }

    /// Configuration for reverse sorting
    pub fn reverse() -> SortConfig {
        SortConfig::new().with_reverse(true)
    }

    /// Configuration for stable sorting
    pub fn stable() -> SortConfig {
        SortConfig::new().with_stable(true)
    }

    /// Configuration for merge mode
    pub fn merge() -> SortConfig {
        SortConfig::new().with_merge(true)
    }

    /// Configuration for check mode
    pub fn check() -> SortConfig {
        SortConfig::new().with_check(true)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = SortConfig::default();
        assert_eq!(config.mode, SortMode::Lexicographic);
        assert!(!config.reverse);
        assert!(!config.unique);
        assert!(!config.stable);
    }

    #[test]
    fn test_config_builder() {
        let config = SortConfigBuilder::new()
            .mode(SortMode::Numeric)
            .reverse()
            .unique()
            .build()
            .expect("Failed to build test config");

        assert_eq!(config.mode, SortMode::Numeric);
        assert!(config.reverse);
        assert!(config.unique);
    }

    #[test]
    fn test_sort_mode_from_str() {
        assert_eq!(
            "numeric"
                .parse::<SortMode>()
                .expect("Failed to parse numeric mode"),
            SortMode::Numeric
        );
        assert_eq!(
            "version"
                .parse::<SortMode>()
                .expect("Failed to parse version mode"),
            SortMode::Version
        );
        assert!("invalid".parse::<SortMode>().is_err());
    }

    #[test]
    fn test_validate_conflicting_options() {
        let config = SortConfig {
            check: true,
            merge: true,
            ..Default::default()
        };

        assert!(config.validate().is_err());
    }

    #[test]
    fn test_effective_buffer_size() {
        let config = SortConfig::default();
        assert_eq!(config.effective_buffer_size(), 1024 * 1024);

        let config = SortConfig::default().with_buffer_size(Some(2048));
        assert_eq!(config.effective_buffer_size(), 2048);
    }

    #[test]
    fn test_presets() {
        let config = presets::numeric();
        assert_eq!(config.mode, SortMode::Numeric);

        let config = presets::reverse();
        assert!(config.reverse);

        let config = presets::unique();
        assert!(config.unique);
    }

    #[test]
    fn test_reading_from_stdin() {
        let config = SortConfig::default();
        assert!(config.reading_from_stdin());

        let config = SortConfig::default().with_input_files(vec!["-".to_string()]);
        assert!(config.reading_from_stdin());

        let config = SortConfig::default().with_input_files(vec!["file.txt".to_string()]);
        assert!(!config.reading_from_stdin());
    }
}