awkrs 0.5.3

Awk implementation in Rust with broad CLI compatibility, parallel records, and experimental Cranelift JIT
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
//! [`AwkStr`] — the byte string an awk value holds.
//!
//! # Why bytes
//!
//! awk strings are byte strings. gawk, mawk and one-true-awk all pass an
//! arbitrary input byte through `$0`, a field, `substr`, `index` and `print`
//! untouched, and all three build one from `printf "%c", 233`. A Rust `String`
//! cannot hold a byte that is not part of valid UTF-8, so awkrs replaced every
//! such byte with `U+FFFD` on the way in — three bytes out where the references
//! emit one, and a value that can never match the byte it came from.
//!
//! # Why this representation
//!
//! - **`Vec<u8>` with a UTF-8 fast path** (this type). `regex::bytes::Regex` is
//!   already a dependency and already drives `RS` matching, so the byte-regex
//!   engine is proven here rather than new. The output half of the pipeline is
//!   already bytes — `print_buf`, `line_buf`, `ofs_bytes`, `ors_bytes` — so this
//!   meets it instead of converting at the boundary. [`AwkStr::as_utf8`] keeps
//!   the common case (valid UTF-8, usually ASCII) a borrow, not a copy.
//! - **`bstr::BString`** would be a new dependency for an API that is written
//!   here in a few hundred lines, and it still derefs to `[u8]` — every `&str`
//!   consumer in the tree breaks exactly the same way. It buys convenience, not
//!   a smaller change.
//! - **An enum (`Utf8(String) | Bytes(Vec<u8>)`)** was rejected on correctness,
//!   not effort: it gives one logical string two representations, so equality,
//!   hashing and array-subscript identity all have to normalise across the
//!   variants, and any site that forgets silently answers wrong — `a["x"]` and
//!   `a[Bytes("x")]` becoming separate entries is the same class of bug the
//!   `CONVFMT` subscript rules already had to be fixed for.

use std::borrow::Cow;
use std::fmt;
use std::ops::Deref;

/// The string payload of `crate::runtime::Value`: an arbitrary byte sequence.
///
/// Ordering and equality are over the bytes, which is what awk's string
/// comparison wants in a single-byte locale and what `strcmp` gives the
/// references. Valid UTF-8 sorts identically either way, so this only differs
/// from the old `String` ordering for input the old type could not hold.
#[derive(Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AwkStr(Vec<u8>);

impl AwkStr {
    /// Empty string.
    #[inline]
    pub fn new() -> Self {
        AwkStr(Vec::new())
    }

    /// Empty string, usable in a `static` / `const`.
    ///
    /// `Vec::new` is const but `String::new().into()` is not, and the VM keeps a
    /// `static EMPTY_STR: Value` to hand out for a missing slot without
    /// allocating.
    #[inline]
    pub const fn new_const() -> Self {
        AwkStr(Vec::new())
    }

    /// Empty string with room for `n` bytes.
    #[inline]
    pub fn with_capacity(n: usize) -> Self {
        AwkStr(Vec::with_capacity(n))
    }

    /// Take ownership of raw bytes — no validation, by design.
    #[inline]
    pub fn from_vec(v: Vec<u8>) -> Self {
        AwkStr(v)
    }

    /// The bytes. This is the value; everything else is a view of it.
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// The bytes, mutably — for in-place edits that keep the length rule.
    #[inline]
    pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
        &mut self.0
    }

    /// Give up the buffer.
    #[inline]
    pub fn into_bytes(self) -> Vec<u8> {
        self.0
    }

    /// Borrow as `&str` when the bytes are valid UTF-8 — the common case, and
    /// the one path that must not copy.
    ///
    /// `None` means the value holds a byte sequence no `&str` can name; the
    /// caller either works in bytes or falls back to [`Self::to_str_lossy`].
    #[inline]
    pub fn as_utf8(&self) -> Option<&str> {
        std::str::from_utf8(&self.0).ok()
    }

    /// `&str` view, substituting `U+FFFD` for anything that is not valid UTF-8.
    ///
    /// Borrows when it can. Use this only where the answer is allowed to be
    /// approximate (diagnostics, `Display`); anything the program can observe
    /// should go through [`Self::as_bytes`] or [`Self::as_utf8`].
    #[inline]
    pub fn to_str_lossy(&self) -> Cow<'_, str> {
        String::from_utf8_lossy(&self.0)
    }

    /// Owned lossy `String`. Same caveat as [`Self::to_str_lossy`].
    #[inline]
    pub fn to_lossy_string(&self) -> String {
        String::from_utf8_lossy(&self.0).into_owned()
    }

    /// Length in **bytes**, which is what `length()` reports in a single-byte
    /// locale in all three references.
    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// `len() == 0`.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Truncate to empty, keeping the allocation.
    #[inline]
    pub fn clear(&mut self) {
        self.0.clear();
    }

    /// Append a `&str`.
    #[inline]
    pub fn push_str(&mut self, s: &str) {
        self.0.extend_from_slice(s.as_bytes());
    }

    /// Append raw bytes.
    #[inline]
    pub fn push_bytes(&mut self, b: &[u8]) {
        self.0.extend_from_slice(b);
    }

    /// Append one byte — the operation `String` cannot offer.
    #[inline]
    pub fn push_byte(&mut self, b: u8) {
        self.0.push(b);
    }

    /// Append one `char`, UTF-8 encoded.
    #[inline]
    pub fn push_char(&mut self, c: char) {
        let mut buf = [0u8; 4];
        self.0.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
    }

    /// Append another `AwkStr`.
    #[inline]
    pub fn push_awkstr(&mut self, other: &AwkStr) {
        self.0.extend_from_slice(&other.0);
    }

    /// Byte-offset of the first occurrence of `needle`.
    #[inline]
    pub fn find_bytes(&self, needle: &[u8]) -> Option<usize> {
        if needle.is_empty() {
            return Some(0);
        }
        memchr::memmem::find(&self.0, needle)
    }

    /// Whether `needle` occurs anywhere.
    #[inline]
    pub fn contains_bytes(&self, needle: &[u8]) -> bool {
        self.find_bytes(needle).is_some()
    }

    /// `true` when every byte is ASCII — the case where byte semantics and
    /// character semantics cannot disagree, so callers can skip the locale
    /// question entirely.
    #[inline]
    pub fn is_ascii(&self) -> bool {
        self.0.is_ascii()
    }

    /// Characters, with `U+FFFD` standing in for any byte that is not part of
    /// valid UTF-8 — the character view for a multibyte locale.
    ///
    /// Named for what it does: a caller that wants the bytes must ask for
    /// [`Self::as_bytes`], because in a single-byte locale each byte is its own
    /// character and this iterator would merge some of them.
    #[inline]
    pub fn chars_lossy(&self) -> impl Iterator<Item = char> + '_ {
        self.0.utf8_chunks().flat_map(|c| {
            c.valid()
                .chars()
                .chain(std::iter::repeat_n('\u{fffd}', c.invalid().len()))
        })
    }

    /// Byte offset of the start of character `n` (0-based), or the end of the
    /// string when there are fewer than `n` characters.
    ///
    /// "Character" is a UTF-8 character where the bytes form one and a single
    /// byte otherwise, which is what makes character indexing total over
    /// arbitrary input: an unpaired byte is one character and stays itself.
    #[inline]
    pub fn char_offset(&self, n: usize) -> usize {
        let mut i = 0usize;
        let mut seen = 0usize;
        while seen < n && i < self.0.len() {
            i += crate::runtime::utf8_char_len(&self.0[i..]);
            seen += 1;
        }
        i
    }

    /// `count` characters starting at character `start`, as bytes.
    ///
    /// Cutting on character boundaries and copying the **bytes** between them is
    /// what keeps `substr` byte-faithful: rendering the characters back out
    /// would turn any unpaired byte into `U+FFFD`.
    #[inline]
    pub fn substr_chars(&self, start: usize, count: usize) -> AwkStr {
        let from = self.char_offset(start);
        let mut i = from;
        let mut taken = 0usize;
        while taken < count && i < self.0.len() {
            i += crate::runtime::utf8_char_len(&self.0[i..]);
            taken += 1;
        }
        AwkStr(self.0[from..i].to_vec())
    }

    /// `count` bytes starting at byte `start` — the `-b` counterpart of
    /// [`Self::substr_chars`].
    #[inline]
    pub fn substr_bytes(&self, start: usize, count: usize) -> AwkStr {
        if start >= self.0.len() {
            return AwkStr::new();
        }
        let end = start.saturating_add(count).min(self.0.len());
        AwkStr(self.0[start..end].to_vec())
    }

    /// Byte slice of a range, as a new `AwkStr`.
    #[inline]
    pub fn slice(&self, start: usize, end: usize) -> AwkStr {
        let s = start.min(self.0.len());
        let e = end.clamp(s, self.0.len());
        AwkStr(self.0[s..e].to_vec())
    }
}

impl Deref for AwkStr {
    type Target = [u8];
    #[inline]
    fn deref(&self) -> &[u8] {
        &self.0
    }
}

impl From<String> for AwkStr {
    #[inline]
    fn from(s: String) -> Self {
        AwkStr(s.into_bytes())
    }
}

impl From<&String> for AwkStr {
    #[inline]
    fn from(s: &String) -> Self {
        AwkStr(s.as_bytes().to_vec())
    }
}

impl From<&str> for AwkStr {
    #[inline]
    fn from(s: &str) -> Self {
        AwkStr(s.as_bytes().to_vec())
    }
}

impl From<Vec<u8>> for AwkStr {
    #[inline]
    fn from(v: Vec<u8>) -> Self {
        AwkStr(v)
    }
}

impl From<&[u8]> for AwkStr {
    #[inline]
    fn from(b: &[u8]) -> Self {
        AwkStr(b.to_vec())
    }
}

impl From<Cow<'_, str>> for AwkStr {
    #[inline]
    fn from(c: Cow<'_, str>) -> Self {
        AwkStr(c.into_owned().into_bytes())
    }
}

impl From<char> for AwkStr {
    #[inline]
    fn from(c: char) -> Self {
        let mut s = AwkStr::new();
        s.push_char(c);
        s
    }
}

impl PartialEq<str> for AwkStr {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.0 == other.as_bytes()
    }
}

impl PartialEq<&str> for AwkStr {
    #[inline]
    fn eq(&self, other: &&str) -> bool {
        self.0 == other.as_bytes()
    }
}

impl PartialEq<String> for AwkStr {
    #[inline]
    fn eq(&self, other: &String) -> bool {
        self.0 == other.as_bytes()
    }
}

impl PartialEq<AwkStr> for str {
    #[inline]
    fn eq(&self, other: &AwkStr) -> bool {
        self.as_bytes() == other.0
    }
}

impl PartialEq<AwkStr> for &str {
    #[inline]
    fn eq(&self, other: &AwkStr) -> bool {
        self.as_bytes() == other.0
    }
}

impl PartialEq<[u8]> for AwkStr {
    #[inline]
    fn eq(&self, other: &[u8]) -> bool {
        self.0 == other
    }
}

/// Lossy, for user-facing text. The bytes are the value; this is a rendering.
impl fmt::Display for AwkStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.to_str_lossy(), f)
    }
}

/// Quoted like a string literal, with `\xNN` for bytes no `str` can hold, so a
/// `{:?}` of a `Value` still reads as text rather than a list of integers.
impl fmt::Debug for AwkStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.as_utf8() {
            Some(s) => fmt::Debug::fmt(s, f),
            None => {
                f.write_str("\"")?;
                for chunk in self.0.utf8_chunks() {
                    for c in chunk.valid().chars() {
                        write!(f, "{}", c.escape_debug())?;
                    }
                    for b in chunk.invalid() {
                        write!(f, "\\x{b:02x}")?;
                    }
                }
                f.write_str("\"")
            }
        }
    }
}

/// `write!(dst, ...)` appends UTF-8, which is always a valid byte sequence.
impl fmt::Write for AwkStr {
    #[inline]
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.push_str(s);
        Ok(())
    }
}

impl std::borrow::Borrow<[u8]> for AwkStr {
    #[inline]
    fn borrow(&self) -> &[u8] {
        &self.0
    }
}

impl FromIterator<u8> for AwkStr {
    fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self {
        AwkStr(iter.into_iter().collect())
    }
}

impl FromIterator<char> for AwkStr {
    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
        let mut s = AwkStr::new();
        for c in iter {
            s.push_char(c);
        }
        s
    }
}

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

    #[test]
    fn a_high_byte_survives_the_round_trip() {
        // The whole point: `String` cannot hold this, so every assertion here
        // is one the previous representation could not make.
        let s = AwkStr::from_vec(vec![b'a', 0xe9, b'b']);
        assert_eq!(s.as_bytes(), &[b'a', 0xe9, b'b']);
        assert_eq!(s.len(), 3, "length is bytes, not characters");
        assert!(s.as_utf8().is_none(), "not valid UTF-8, and not pretended");
        assert_eq!(s.to_str_lossy(), "a\u{fffd}b");
        assert_eq!(s.clone().into_bytes(), vec![b'a', 0xe9, b'b']);
    }

    #[test]
    fn valid_utf8_borrows_rather_than_copies() {
        let s = AwkStr::from("café");
        let borrowed = s.as_utf8().expect("valid UTF-8");
        assert_eq!(borrowed, "café");
        assert_eq!(s.len(), 5, "5 bytes, 4 characters");
        assert!(matches!(s.to_str_lossy(), Cow::Borrowed(_)));
    }

    #[test]
    fn equality_and_ordering_are_over_bytes() {
        assert_eq!(AwkStr::from("abc"), "abc");
        let (abc, abd) = (AwkStr::from("abc"), AwkStr::from("abd"));
        assert!(abc < abd);
        // 0xFF sorts above every ASCII byte, as `strcmp` has it.
        let (z, high) = (AwkStr::from("z"), AwkStr::from_vec(vec![0xff]));
        assert!(z < high);
    }

    #[test]
    fn debug_shows_text_with_escapes_for_unnameable_bytes() {
        assert_eq!(format!("{:?}", AwkStr::from("ab")), "\"ab\"");
        assert_eq!(
            format!("{:?}", AwkStr::from_vec(vec![b'a', 0xe9, b'b'])),
            "\"a\\xe9b\""
        );
    }

    #[test]
    fn find_bytes_locates_a_byte_no_str_can_name() {
        let s = AwkStr::from_vec(vec![b'a', 0xe9, b'b']);
        assert_eq!(s.find_bytes(&[0xe9]), Some(1));
        assert_eq!(s.find_bytes(b"b"), Some(2));
        assert_eq!(s.find_bytes(b"zz"), None);
        assert_eq!(s.find_bytes(b""), Some(0), "empty needle matches at 0");
    }

    #[test]
    fn slice_is_by_byte_offset_and_clamps() {
        let s = AwkStr::from_vec(vec![b'a', 0xe9, b'b']);
        assert_eq!(s.slice(1, 2).as_bytes(), &[0xe9]);
        assert_eq!(s.slice(0, 99).as_bytes(), &[b'a', 0xe9, b'b']);
        assert_eq!(s.slice(5, 9).as_bytes(), b"");
    }
}