awk-rs 0.2.0

A 100% POSIX-compatible AWK implementation in Rust
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
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt;

/// AWK value type with dynamic typing and automatic coercion
///
/// AWK has a unique type system where values can be strings, numbers, or
/// "numeric strings" (strings that look like numbers). This enum captures
/// all three cases.
///
/// # Examples
///
/// ```
/// use awk_rs::Value;
///
/// // Numbers
/// let num = Value::Number(42.0);
/// assert_eq!(num.to_number(), 42.0);
/// assert_eq!(num.to_string_val(), "42");
///
/// // Strings
/// let s = Value::from_string("hello".to_string());
/// assert_eq!(s.to_string_val(), "hello");
/// assert_eq!(s.to_number(), 0.0);  // Non-numeric string coerces to 0
///
/// // Numeric strings
/// let ns = Value::from_string("123".to_string());
/// assert_eq!(ns.to_number(), 123.0);
/// assert_eq!(ns.to_string_val(), "123");
///
/// // Truthiness
/// assert!(Value::Number(1.0).is_truthy());
/// assert!(!Value::Number(0.0).is_truthy());
/// assert!(Value::from_string("hello".to_string()).is_truthy());
/// assert!(!Value::from_string("".to_string()).is_truthy());
/// ```
#[derive(Debug, Clone, Default)]
pub enum Value {
    /// Uninitialized value - coerces to "" or 0 depending on context
    #[default]
    Uninitialized,
    /// Numeric value
    Number(f64),
    /// String value
    String(String),
    /// Numeric string - a string that looks like a number
    /// (used for comparison semantics)
    NumericString(String, f64),
}

impl Value {
    /// Create a new string value, detecting if it's a numeric string
    #[inline]
    pub fn from_string(s: String) -> Self {
        if let Some(num) = parse_numeric_string(&s) {
            Value::NumericString(s, num)
        } else {
            Value::String(s)
        }
    }

    /// Create a numeric value
    #[inline]
    pub fn from_number(n: f64) -> Self {
        Value::Number(n)
    }

    /// Check if this value is "true" in boolean context
    /// - Uninitialized is false
    /// - Number 0 is false
    /// - Empty string is false
    /// - Everything else is true
    #[inline]
    pub fn is_truthy(&self) -> bool {
        match self {
            Value::Uninitialized => false,
            Value::Number(n) => *n != 0.0,
            Value::String(s) => !s.is_empty(),
            Value::NumericString(s, _) => !s.is_empty(),
        }
    }

    /// Coerce to numeric value
    #[inline]
    pub fn to_number(&self) -> f64 {
        match self {
            Value::Uninitialized => 0.0,
            Value::Number(n) => *n,
            Value::String(s) => parse_leading_number(s),
            Value::NumericString(_, n) => *n,
        }
    }

    /// Coerce to string value
    #[inline]
    pub fn to_string_val(&self) -> String {
        self.to_string_with_format("%.6g")
    }

    /// Get string as Cow to avoid allocation when possible
    #[inline]
    pub fn as_str(&self) -> Cow<'_, str> {
        match self {
            Value::Uninitialized => Cow::Borrowed(""),
            Value::Number(n) => Cow::Owned(format_number(*n, "%.6g")),
            Value::String(s) => Cow::Borrowed(s),
            Value::NumericString(s, _) => Cow::Borrowed(s),
        }
    }

    /// Coerce to string with specific format (for OFMT/CONVFMT)
    pub fn to_string_with_format(&self, format: &str) -> String {
        match self {
            Value::Uninitialized => String::new(),
            Value::Number(n) => format_number(*n, format),
            Value::String(s) => s.clone(),
            Value::NumericString(s, _) => s.clone(),
        }
    }

    /// Check if this value is definitely numeric
    #[inline]
    pub fn is_numeric(&self) -> bool {
        matches!(self, Value::Number(_))
    }

    /// Check if this value is a numeric string
    #[inline]
    pub fn is_numeric_string(&self) -> bool {
        matches!(self, Value::NumericString(_, _))
    }

    /// Check if this value should compare as a number
    #[inline]
    pub fn compares_as_number(&self) -> bool {
        matches!(
            self,
            Value::Number(_) | Value::NumericString(_, _) | Value::Uninitialized
        )
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Uninitialized => Ok(()),
            Value::Number(n) => write!(f, "{}", format_number(*n, "%.6g")),
            Value::String(s) => write!(f, "{}", s),
            Value::NumericString(s, _) => write!(f, "{}", s),
        }
    }
}

/// Compare two AWK values according to AWK comparison rules
#[inline]
pub fn compare_values(left: &Value, right: &Value) -> Ordering {
    compare_values_with_format(left, right, "%.6g")
}

/// Compare two AWK values, using `convfmt` (CONVFMT) when a number has to be
/// converted to a string for a string comparison.
#[inline]
pub fn compare_values_with_format(left: &Value, right: &Value, convfmt: &str) -> Ordering {
    // If both are numeric or numeric strings, compare numerically
    if left.compares_as_number() && right.compares_as_number() {
        let l = left.to_number();
        let r = right.to_number();
        l.partial_cmp(&r).unwrap_or(Ordering::Equal)
    } else {
        // Otherwise compare as strings
        match (left, right) {
            // Slow path: at least one Number operand needs CONVFMT formatting.
            (Value::Number(_), _) | (_, Value::Number(_)) => left
                .to_string_with_format(convfmt)
                .cmp(&right.to_string_with_format(convfmt)),
            // Fast path: no Number operand, so CONVFMT is never applied.
            _ => left.as_str().cmp(&right.as_str()),
        }
    }
}

/// Parse the leading numeric portion of a string using optimized byte-based parsing
/// "42abc" -> 42.0
/// "  3.14  " -> 3.14
/// "abc" -> 0.0
#[inline]
pub fn parse_leading_number(s: &str) -> f64 {
    let bytes = s.as_bytes();
    let mut i = 0;

    // Skip leading whitespace
    while i < bytes.len() && bytes[i].is_ascii_whitespace() {
        i += 1;
    }

    if i >= bytes.len() {
        return 0.0;
    }

    let start = i;

    // Optional sign
    if i < bytes.len() && (bytes[i] == b'+' || bytes[i] == b'-') {
        i += 1;
    }

    let mut has_digits = false;

    // Digits before decimal
    while i < bytes.len() && bytes[i].is_ascii_digit() {
        i += 1;
        has_digits = true;
    }

    // Decimal point and digits after
    if i < bytes.len() && bytes[i] == b'.' {
        i += 1;
        while i < bytes.len() && bytes[i].is_ascii_digit() {
            i += 1;
            has_digits = true;
        }
    }

    // Exponent
    if i < bytes.len() && (bytes[i] == b'e' || bytes[i] == b'E') {
        let exp_start = i;
        i += 1;
        if i < bytes.len() && (bytes[i] == b'+' || bytes[i] == b'-') {
            i += 1;
        }
        if i < bytes.len() && bytes[i].is_ascii_digit() {
            while i < bytes.len() && bytes[i].is_ascii_digit() {
                i += 1;
            }
        } else {
            // Invalid exponent, back up
            i = exp_start;
        }
    }

    if !has_digits {
        return 0.0;
    }

    // Fast path for common integer case
    let num_str = &s[start..i];
    let is_integer_like =
        !num_str.contains('.') && !num_str.contains('e') && !num_str.contains('E');
    match num_str.parse::<i64>() {
        Ok(n) if is_integer_like => return n as f64,
        _ => {}
    }

    num_str.parse().unwrap_or(0.0)
}

/// Check if a string is a numeric string (looks entirely like a number)
#[inline]
fn parse_numeric_string(s: &str) -> Option<f64> {
    let trimmed = s.trim();
    if trimmed.is_empty() {
        return None;
    }

    // Fast path: check if it's a simple integer
    if trimmed.bytes().all(|b| b.is_ascii_digit()) {
        return trimmed.parse().ok();
    }

    // Check for leading sign
    let check = if trimmed.starts_with('-') || trimmed.starts_with('+') {
        &trimmed[1..]
    } else {
        trimmed
    };

    // Simple float pattern check
    let mut has_dot = false;
    let mut has_e = false;
    for (i, b) in check.bytes().enumerate() {
        match b {
            b'0'..=b'9' => continue,
            b'.' if !has_dot && !has_e => has_dot = true,
            b'e' | b'E' if !has_e && i > 0 => {
                has_e = true;
                // Check for sign after e
                if i + 1 < check.len() {
                    let next = check.as_bytes()[i + 1];
                    if next == b'+' || next == b'-' {
                        continue;
                    }
                }
            }
            b'+' | b'-' if has_e => continue,
            _ => return None,
        }
    }

    trimmed.parse().ok()
}

/// Format a number according to printf-style format
pub fn format_number(n: f64, format: &str) -> String {
    if n.is_nan() {
        return "nan".to_string();
    }
    if n.is_infinite() {
        return if n > 0.0 { "inf" } else { "-inf" }.to_string();
    }

    // POSIX: values that are exactly integral convert as integers, with no
    // format applied at all.
    if n.fract() == 0.0 {
        if n.abs() < 1e15 {
            return itoa_fast(n as i64);
        }
        return format!("{:.0}", n);
    }

    crate::fmt::sprintf(format, &[Value::Number(n)])
}

/// Fast integer to string conversion
#[inline]
fn itoa_fast(n: i64) -> String {
    if n == 0 {
        return "0".to_string();
    }

    let mut result = String::with_capacity(20);
    let mut num = n;
    let negative = num < 0;
    if negative {
        num = -num;
    }

    while num > 0 {
        result.push((b'0' + (num % 10) as u8) as char);
        num /= 10;
    }

    if negative {
        result.push('-');
    }

    result.chars().rev().collect()
}

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

    #[test]
    fn test_uninitialized() {
        let v = Value::Uninitialized;
        assert_eq!(v.to_number(), 0.0);
        assert_eq!(v.to_string_val(), "");
        assert!(!v.is_truthy());
    }

    #[test]
    fn test_number() {
        let v = Value::Number(42.0);
        assert_eq!(v.to_number(), 42.0);
        assert_eq!(v.to_string_val(), "42");
        assert!(v.is_truthy());

        let zero = Value::Number(0.0);
        assert!(!zero.is_truthy());
    }

    #[test]
    fn test_string() {
        let v = Value::from_string("hello".to_string());
        assert_eq!(v.to_number(), 0.0);
        assert_eq!(v.to_string_val(), "hello");
        assert!(v.is_truthy());

        let empty = Value::from_string("".to_string());
        assert!(!empty.is_truthy());
    }

    #[test]
    fn test_numeric_string() {
        let v = Value::from_string("42".to_string());
        assert!(v.is_numeric_string());
        assert_eq!(v.to_number(), 42.0);
        assert_eq!(v.to_string_val(), "42");
    }

    #[test]
    fn test_leading_number() {
        assert_eq!(parse_leading_number("42abc"), 42.0);
        assert_eq!(parse_leading_number("  2.75  "), 2.75);
        assert_eq!(parse_leading_number("abc"), 0.0);
        assert_eq!(parse_leading_number("-5.5"), -5.5);
        assert_eq!(parse_leading_number("1e10"), 1e10);
    }

    #[test]
    fn test_comparison() {
        let n1 = Value::Number(10.0);
        let n2 = Value::Number(2.0);
        assert_eq!(compare_values(&n1, &n2), Ordering::Greater);

        let s1 = Value::from_string("10".to_string());
        let s2 = Value::from_string("2".to_string());
        // Both numeric strings -> compare numerically
        assert_eq!(compare_values(&s1, &s2), Ordering::Greater);

        let s3 = Value::from_string("abc".to_string());
        let s4 = Value::from_string("def".to_string());
        // Both pure strings -> compare lexically
        assert_eq!(compare_values(&s3, &s4), Ordering::Less);
    }

    #[test]
    fn test_itoa_fast() {
        assert_eq!(itoa_fast(0), "0");
        assert_eq!(itoa_fast(42), "42");
        assert_eq!(itoa_fast(-123), "-123");
        assert_eq!(itoa_fast(1000000), "1000000");
    }

    #[test]
    fn test_format_number_nan() {
        assert_eq!(format_number(f64::NAN, "%.6g"), "nan");
    }

    #[test]
    fn test_format_number_inf() {
        assert_eq!(format_number(f64::INFINITY, "%.6g"), "inf");
        assert_eq!(format_number(f64::NEG_INFINITY, "%.6g"), "-inf");
    }

    #[test]
    fn test_format_number_integer() {
        assert_eq!(format_number(42.0, "%.6g"), "42");
        assert_eq!(format_number(-100.0, "%.6g"), "-100");
    }

    #[test]
    fn test_format_number_float() {
        assert_eq!(format_number(2.75, "%.6g"), "2.75");
    }

    #[test]
    fn test_from_number() {
        let v = Value::from_number(2.75);
        assert_eq!(v.to_number(), 2.75);
    }

    #[test]
    fn test_is_truthy_numeric_string() {
        let v = Value::NumericString("42".to_string(), 42.0);
        assert!(v.is_truthy());

        let empty = Value::NumericString("".to_string(), 0.0);
        assert!(!empty.is_truthy());
    }

    #[test]
    fn test_comparison_number_vs_string() {
        let n = Value::Number(10.0);
        let s = Value::from_string("hello".to_string());
        // Number vs non-numeric string
        assert!(compare_values(&n, &s) != Ordering::Equal);
    }

    #[test]
    fn test_comparison_uninitialized() {
        let u = Value::Uninitialized;
        let n = Value::Number(1.0);
        // Uninitialized (0) vs 1 should be Less
        assert_eq!(compare_values(&u, &n), Ordering::Less);
    }

    #[test]
    fn test_parse_leading_with_sign() {
        assert_eq!(parse_leading_number("+42"), 42.0);
        assert_eq!(parse_leading_number("  +2.75"), 2.75);
    }

    #[test]
    fn test_parse_leading_exponent() {
        assert_eq!(parse_leading_number("1e-5"), 1e-5);
        assert_eq!(parse_leading_number("2E+3"), 2000.0);
    }

    #[test]
    fn test_numeric_string_with_exponent() {
        let v = Value::from_string("1e5".to_string());
        assert!(v.is_numeric_string());
        assert_eq!(v.to_number(), 1e5);
    }

    #[test]
    fn test_numeric_string_with_sign() {
        let v = Value::from_string("-42.5".to_string());
        assert!(v.is_numeric_string());
        assert_eq!(v.to_number(), -42.5);
    }

    #[test]
    fn test_numeric_string_whitespace() {
        let v = Value::from_string("  123  ".to_string());
        assert!(v.is_numeric_string());
        assert_eq!(v.to_number(), 123.0);
    }

    #[test]
    fn test_to_string_val_uninitialized() {
        let v = Value::Uninitialized;
        assert_eq!(v.to_string_val(), "");
    }

    #[test]
    fn test_to_number_uninitialized() {
        let v = Value::Uninitialized;
        assert_eq!(v.to_number(), 0.0);
    }
}