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

use regex::Regex;

/// Exit code: expression is non-null and non-zero.
pub const EXIT_SUCCESS: i32 = 0;
/// Exit code: expression is null or zero.
pub const EXIT_FAILURE: i32 = 1;
/// Exit code: expression is syntactically invalid.
pub const EXIT_EXPR_ERROR: i32 = 2;
/// Exit code: regex error.
pub const EXIT_REGEX_ERROR: i32 = 3;

/// A value produced by evaluating an expr expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExprValue {
    Integer(i64),
    Str(String),
}

impl fmt::Display for ExprValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExprValue::Integer(n) => write!(f, "{}", n),
            ExprValue::Str(s) => write!(f, "{}", s),
        }
    }
}

impl ExprValue {
    /// Returns true if this value is considered "null" (empty string or integer 0).
    pub fn is_null(&self) -> bool {
        match self {
            ExprValue::Integer(n) => *n == 0,
            ExprValue::Str(s) => s.is_empty() || s == "0",
        }
    }

    /// Try to interpret this value as an integer.
    pub fn as_integer(&self) -> Option<i64> {
        match self {
            ExprValue::Integer(n) => Some(*n),
            ExprValue::Str(s) => parse_integer(s),
        }
    }
}

/// Parse an integer from a string, accepting optional leading sign and digits only.
fn parse_integer(s: &str) -> Option<i64> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }
    let (sign, digits) = if let Some(rest) = s.strip_prefix('-') {
        (-1i64, rest)
    } else if let Some(rest) = s.strip_prefix('+') {
        (1i64, rest)
    } else {
        (1i64, s)
    };
    if digits.is_empty() || !digits.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    digits.parse::<i64>().ok().map(|v| sign * v)
}

/// Errors that can occur during expression evaluation.
#[derive(Debug, Clone)]
pub enum ExprError {
    /// Syntax error in the expression.
    Syntax(String),
    /// Division by zero.
    DivisionByZero,
    /// Invalid regex pattern.
    RegexError(String),
    /// Non-integer argument where integer was required.
    NonIntegerArgument,
    /// Missing operand.
    MissingOperand,
}

impl fmt::Display for ExprError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExprError::Syntax(msg) => write!(f, "syntax error: {}", msg),
            ExprError::DivisionByZero => write!(f, "division by zero"),
            ExprError::RegexError(msg) => write!(f, "{}", msg),
            ExprError::NonIntegerArgument => write!(f, "non-integer argument"),
            ExprError::MissingOperand => write!(f, "missing operand"),
        }
    }
}

impl ExprError {
    /// Returns the exit code for this error type.
    pub fn exit_code(&self) -> i32 {
        match self {
            ExprError::RegexError(_) => EXIT_REGEX_ERROR,
            _ => EXIT_EXPR_ERROR,
        }
    }
}

/// Recursive descent parser for expr expressions.
struct ExprParser<'a> {
    args: &'a [String],
    pos: usize,
}

impl<'a> ExprParser<'a> {
    fn new(args: &'a [String]) -> Self {
        ExprParser { args, pos: 0 }
    }

    /// Peek at the current token without consuming it.
    fn peek(&self) -> Option<&str> {
        if self.pos < self.args.len() {
            Some(self.args[self.pos].as_str())
        } else {
            None
        }
    }

    /// Consume the current token and advance.
    fn consume(&mut self) -> Option<&str> {
        if self.pos < self.args.len() {
            let tok = self.args[self.pos].as_str();
            self.pos += 1;
            Some(tok)
        } else {
            None
        }
    }

    /// Expect a specific token, returning an error if not found.
    fn expect(&mut self, expected: &str) -> Result<(), ExprError> {
        match self.consume() {
            Some(tok) if tok == expected => Ok(()),
            Some(tok) => Err(ExprError::Syntax(format!(
                "expected '{}', found '{}'",
                expected, tok
            ))),
            None => Err(ExprError::Syntax(format!("expected '{}'", expected))),
        }
    }

    /// Parse the top-level: OR expression.
    /// OR: AND ( '|' AND )*
    fn parse_or(&mut self) -> Result<ExprValue, ExprError> {
        let mut left = self.parse_and()?;
        while self.peek() == Some("|") {
            self.consume();
            let right = self.parse_and()?;
            left = if !left.is_null() { left } else { right };
        }
        Ok(left)
    }

    /// Parse AND expression.
    /// AND: COMPARISON ( '&' COMPARISON )*
    fn parse_and(&mut self) -> Result<ExprValue, ExprError> {
        let mut left = self.parse_comparison()?;
        while self.peek() == Some("&") {
            self.consume();
            let right = self.parse_comparison()?;
            left = if !left.is_null() && !right.is_null() {
                left
            } else {
                ExprValue::Integer(0)
            };
        }
        Ok(left)
    }

    /// Parse comparison expression.
    /// COMPARISON: ADDITION ( ('<'|'<='|'='|'!='|'>='|'>') ADDITION )*
    fn parse_comparison(&mut self) -> Result<ExprValue, ExprError> {
        let mut left = self.parse_addition()?;
        while matches!(
            self.peek(),
            Some("<") | Some("<=") | Some("=") | Some("!=") | Some(">=") | Some(">")
        ) {
            let op = self.consume().unwrap().to_string();
            let right = self.parse_addition()?;
            let result = compare_values(&left, &right, &op);
            left = ExprValue::Integer(if result { 1 } else { 0 });
        }
        Ok(left)
    }

    /// Parse addition/subtraction.
    /// ADDITION: MULTIPLICATION ( ('+'|'-') MULTIPLICATION )*
    fn parse_addition(&mut self) -> Result<ExprValue, ExprError> {
        let mut left = self.parse_multiplication()?;
        while matches!(self.peek(), Some("+") | Some("-")) {
            let op = self.consume().unwrap().to_string();
            let right = self.parse_multiplication()?;
            let lv = left.as_integer().ok_or(ExprError::NonIntegerArgument)?;
            let rv = right.as_integer().ok_or(ExprError::NonIntegerArgument)?;
            left = match op.as_str() {
                "+" => ExprValue::Integer(
                    lv.checked_add(rv)
                        .ok_or_else(|| ExprError::Syntax("integer result too large".into()))?,
                ),
                "-" => ExprValue::Integer(
                    lv.checked_sub(rv)
                        .ok_or_else(|| ExprError::Syntax("integer result too large".into()))?,
                ),
                _ => unreachable!(),
            };
        }
        Ok(left)
    }

    /// Parse multiplication/division/modulo.
    /// MULTIPLICATION: MATCH ( ('*'|'/'|'%') MATCH )*
    fn parse_multiplication(&mut self) -> Result<ExprValue, ExprError> {
        let mut left = self.parse_match()?;
        while matches!(self.peek(), Some("*") | Some("/") | Some("%")) {
            let op = self.consume().unwrap().to_string();
            let right = self.parse_match()?;
            let lv = left.as_integer().ok_or(ExprError::NonIntegerArgument)?;
            let rv = right.as_integer().ok_or(ExprError::NonIntegerArgument)?;
            left = match op.as_str() {
                "*" => ExprValue::Integer(
                    lv.checked_mul(rv)
                        .ok_or_else(|| ExprError::Syntax("integer result too large".into()))?,
                ),
                "/" => {
                    if rv == 0 {
                        return Err(ExprError::DivisionByZero);
                    }
                    ExprValue::Integer(
                        lv.checked_div(rv)
                            .ok_or_else(|| ExprError::Syntax("integer result too large".into()))?,
                    )
                }
                "%" => {
                    if rv == 0 {
                        return Err(ExprError::DivisionByZero);
                    }
                    ExprValue::Integer(
                        lv.checked_rem(rv)
                            .ok_or_else(|| ExprError::Syntax("integer result too large".into()))?,
                    )
                }
                _ => unreachable!(),
            };
        }
        Ok(left)
    }

    /// Parse match/colon expression.
    /// MATCH: PRIMARY ( ':' PRIMARY )?
    fn parse_match(&mut self) -> Result<ExprValue, ExprError> {
        let left = self.parse_primary()?;
        if self.peek() == Some(":") {
            self.consume();
            let right = self.parse_primary()?;
            let pattern_str = match &right {
                ExprValue::Str(s) => s.clone(),
                ExprValue::Integer(n) => n.to_string(),
            };
            let string = match &left {
                ExprValue::Str(s) => s.clone(),
                ExprValue::Integer(n) => n.to_string(),
            };
            return do_match(&string, &pattern_str);
        }
        Ok(left)
    }

    /// Parse primary expression: keyword functions, parenthesized expressions, or atoms.
    fn parse_primary(&mut self) -> Result<ExprValue, ExprError> {
        match self.peek() {
            None => Err(ExprError::MissingOperand),
            Some("(") => {
                self.consume();
                let val = self.parse_or()?;
                self.expect(")")?;
                Ok(val)
            }
            Some("match") => {
                self.consume();
                let string_val = self.parse_primary()?;
                let pattern_val = self.parse_primary()?;
                let string = match &string_val {
                    ExprValue::Str(s) => s.clone(),
                    ExprValue::Integer(n) => n.to_string(),
                };
                let pattern = match &pattern_val {
                    ExprValue::Str(s) => s.clone(),
                    ExprValue::Integer(n) => n.to_string(),
                };
                do_match(&string, &pattern)
            }
            Some("substr") => {
                self.consume();
                let string_val = self.parse_primary()?;
                let pos_val = self.parse_primary()?;
                let len_val = self.parse_primary()?;
                let string = match &string_val {
                    ExprValue::Str(s) => s.clone(),
                    ExprValue::Integer(n) => n.to_string(),
                };
                let pos = pos_val.as_integer().ok_or(ExprError::NonIntegerArgument)?;
                let len = len_val.as_integer().ok_or(ExprError::NonIntegerArgument)?;
                Ok(do_substr(&string, pos, len))
            }
            Some("index") => {
                self.consume();
                let string_val = self.parse_primary()?;
                let chars_val = self.parse_primary()?;
                let string = match &string_val {
                    ExprValue::Str(s) => s.clone(),
                    ExprValue::Integer(n) => n.to_string(),
                };
                let chars = match &chars_val {
                    ExprValue::Str(s) => s.clone(),
                    ExprValue::Integer(n) => n.to_string(),
                };
                Ok(do_index(&string, &chars))
            }
            Some("length") => {
                self.consume();
                let val = self.parse_primary()?;
                let s = match &val {
                    ExprValue::Str(s) => s.clone(),
                    ExprValue::Integer(n) => n.to_string(),
                };
                Ok(ExprValue::Integer(s.len() as i64))
            }
            Some("+") => {
                // GNU expr extension: '+' is a quoting prefix that treats the
                // next token as a literal string, even if it would otherwise be
                // interpreted as a keyword (match, length, substr, index).
                self.consume();
                match self.consume() {
                    Some(tok) => {
                        let tok = tok.to_string();
                        if let Some(n) = parse_integer(&tok) {
                            Ok(ExprValue::Integer(n))
                        } else {
                            Ok(ExprValue::Str(tok))
                        }
                    }
                    None => Err(ExprError::Syntax("missing argument after '+'".to_string())),
                }
            }
            _ => {
                // Atom: a literal string or number.
                let tok = self.consume().unwrap().to_string();
                if let Some(n) = parse_integer(&tok) {
                    Ok(ExprValue::Integer(n))
                } else {
                    Ok(ExprValue::Str(tok))
                }
            }
        }
    }
}

/// Compare two ExprValues. If both are integers, compare numerically;
/// otherwise compare as strings lexicographically.
fn compare_values(left: &ExprValue, right: &ExprValue, op: &str) -> bool {
    let left_int = left.as_integer();
    let right_int = right.as_integer();

    if let (Some(lv), Some(rv)) = (left_int, right_int) {
        match op {
            "<" => lv < rv,
            "<=" => lv <= rv,
            "=" => lv == rv,
            "!=" => lv != rv,
            ">=" => lv >= rv,
            ">" => lv > rv,
            _ => false,
        }
    } else {
        let ls = left.to_string();
        let rs = right.to_string();
        match op {
            "<" => ls < rs,
            "<=" => ls <= rs,
            "=" => ls == rs,
            "!=" => ls != rs,
            ">=" => ls >= rs,
            ">" => ls > rs,
            _ => false,
        }
    }
}

/// Convert a POSIX BRE (Basic Regular Expression) pattern to a Rust regex pattern.
/// BRE differences from ERE:
/// - `\(` and `\)` are group delimiters (not `(` and `)`)
/// - `\{` and `\}` are interval delimiters
/// - `(` and `)` are literal in BRE
/// - `{` and `}` are literal in BRE
/// - `\+`, `\?` are special in BRE (some implementations)
/// - `+`, `?` are literal in BRE
/// - The match is always anchored at the beginning (as if `^` is prepended).
///
/// When inside a `\(` ... `\)` group, `\.` is treated as a literal dot insertion
/// that does not consume input. It is excluded from the regex and instead tracked
/// separately so that the match result can be reconstructed with literal dots.
fn bre_to_rust_regex(pattern: &str) -> String {
    let mut result = String::with_capacity(pattern.len() + 2);
    // BRE patterns in expr are implicitly anchored at the start
    result.push('^');

    let bytes = pattern.as_bytes();
    let mut i = 0;
    let mut group_depth = 0u32;
    while i < bytes.len() {
        if bytes[i] == b'\\' && i + 1 < bytes.len() {
            match bytes[i + 1] {
                b'(' => {
                    group_depth += 1;
                    result.push('(');
                    i += 2;
                }
                b')' => {
                    group_depth = group_depth.saturating_sub(1);
                    result.push(')');
                    i += 2;
                }
                b'{' => {
                    result.push('{');
                    i += 2;
                }
                b'}' => {
                    result.push('}');
                    i += 2;
                }
                b'+' => {
                    result.push('+');
                    i += 2;
                }
                b'?' => {
                    result.push('?');
                    i += 2;
                }
                b'1'..=b'9' => {
                    // Backreference: \1 through \9
                    result.push('\\');
                    result.push(bytes[i + 1] as char);
                    i += 2;
                }
                b'n' => {
                    result.push_str("\\n");
                    i += 2;
                }
                b't' => {
                    result.push_str("\\t");
                    i += 2;
                }
                b'.' => {
                    if group_depth > 0 {
                        // Inside a group, \. is a literal dot insertion that
                        // does not consume input — skip it in the regex.
                        i += 2;
                    } else {
                        result.push('\\');
                        result.push('.');
                        i += 2;
                    }
                }
                b'*' | b'\\' | b'[' | b']' | b'^' | b'$' | b'|' => {
                    result.push('\\');
                    result.push(bytes[i + 1] as char);
                    i += 2;
                }
                _ => {
                    // Unknown escape: pass through literally
                    result.push('\\');
                    result.push(bytes[i + 1] as char);
                    i += 2;
                }
            }
        } else {
            match bytes[i] {
                b'(' => {
                    // Literal in BRE
                    result.push_str("\\(");
                    i += 1;
                }
                b')' => {
                    // Literal in BRE
                    result.push_str("\\)");
                    i += 1;
                }
                b'{' => {
                    // Literal in BRE
                    result.push_str("\\{");
                    i += 1;
                }
                b'}' => {
                    // Literal in BRE
                    result.push_str("\\}");
                    i += 1;
                }
                b'+' => {
                    // Literal in BRE (not a quantifier)
                    result.push_str("\\+");
                    i += 1;
                }
                b'?' => {
                    // Literal in BRE (not a quantifier)
                    result.push_str("\\?");
                    i += 1;
                }
                b'|' => {
                    // Literal in BRE (not alternation)
                    result.push_str("\\|");
                    i += 1;
                }
                _ => {
                    result.push(bytes[i] as char);
                    i += 1;
                }
            }
        }
    }
    result
}

/// Extract a template for the first `\(` ... `\)` group in a BRE pattern.
/// The template is a list of entries: `true` means a literal dot insertion (from `\.`),
/// `false` means a character matched from the input.
/// Returns None if there is no group.
fn bre_group_template(pattern: &str) -> Option<Vec<bool>> {
    let bytes = pattern.as_bytes();
    let mut i = 0;
    let mut in_group = false;
    let mut template = Vec::new();

    while i < bytes.len() {
        if bytes[i] == b'\\' && i + 1 < bytes.len() {
            match bytes[i + 1] {
                b'(' if !in_group => {
                    in_group = true;
                    i += 2;
                }
                b')' if in_group => {
                    return Some(template);
                }
                b'.' if in_group => {
                    // \. inside group = literal dot insertion (not consuming input)
                    template.push(true);
                    i += 2;
                }
                _ if in_group => {
                    // Any other escape inside the group consumes a character from input
                    template.push(false);
                    i += 2;
                }
                _ => {
                    i += 2;
                }
            }
        } else if in_group {
            // Regular character inside group consumes input
            template.push(false);
            i += 1;
        } else {
            i += 1;
        }
    }
    if in_group { Some(template) } else { None }
}

/// Check whether a BRE pattern contains `\(` ... `\)` groups.
fn bre_has_groups(pattern: &str) -> bool {
    let bytes = pattern.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\\' && i + 1 < bytes.len() && bytes[i + 1] == b'(' {
            return true;
        }
        i += 1;
    }
    false
}

/// Perform regex match operation.
/// If the pattern has `\(` ... `\)` groups, returns the first captured group (or empty string).
/// When the group contains `\.`, literal dots are inserted into the result at those positions
/// without consuming characters from the input.
/// Otherwise returns the number of matched characters (or 0).
fn do_match(string: &str, pattern: &str) -> Result<ExprValue, ExprError> {
    let has_groups = bre_has_groups(pattern);
    let rust_pattern = bre_to_rust_regex(pattern);

    let re = Regex::new(&rust_pattern)
        .map_err(|e| ExprError::RegexError(format!("Invalid regular expression: {}", e)))?;

    match re.captures(string) {
        Some(caps) => {
            if has_groups {
                // Return the first captured group, expanded with literal dot insertions
                match caps.get(1) {
                    Some(m) => {
                        let captured = m.as_str();
                        if let Some(template) = bre_group_template(pattern) {
                            let mut result = String::new();
                            let mut char_iter = captured.chars();
                            for is_literal_dot in &template {
                                if *is_literal_dot {
                                    result.push('.');
                                } else if let Some(ch) = char_iter.next() {
                                    result.push(ch);
                                }
                            }
                            Ok(ExprValue::Str(result))
                        } else {
                            Ok(ExprValue::Str(captured.to_string()))
                        }
                    }
                    None => Ok(ExprValue::Str(String::new())),
                }
            } else {
                // Return the number of matched characters
                let m = caps.get(0).unwrap();
                Ok(ExprValue::Integer(m.as_str().len() as i64))
            }
        }
        None => {
            if has_groups {
                Ok(ExprValue::Str(String::new()))
            } else {
                Ok(ExprValue::Integer(0))
            }
        }
    }
}

/// Perform the substr operation: extract a substring.
/// Position is 1-based. If pos or len <= 0 or pos > length, returns empty string.
fn do_substr(string: &str, pos: i64, len: i64) -> ExprValue {
    if pos <= 0 || len <= 0 {
        return ExprValue::Str(String::new());
    }
    let start = (pos - 1) as usize;
    let slen = string.len();
    if start >= slen {
        return ExprValue::Str(String::new());
    }
    let end = (start + len as usize).min(slen);
    ExprValue::Str(string[start..end].to_string())
}

/// Perform the index operation: find the position of the first character in CHARS
/// that appears in STRING. Returns 0 if not found. Position is 1-based.
fn do_index(string: &str, chars: &str) -> ExprValue {
    for (i, c) in string.chars().enumerate() {
        if chars.contains(c) {
            return ExprValue::Integer((i + 1) as i64);
        }
    }
    ExprValue::Integer(0)
}

/// Evaluate an expr expression from command-line arguments.
pub fn evaluate_expr(args: &[String]) -> Result<ExprValue, ExprError> {
    if args.is_empty() {
        return Err(ExprError::MissingOperand);
    }
    let mut parser = ExprParser::new(args);
    let result = parser.parse_or()?;
    if parser.pos < parser.args.len() {
        return Err(ExprError::Syntax(format!(
            "unexpected argument '{}'",
            parser.args[parser.pos]
        )));
    }
    Ok(result)
}