cdx 0.1.24

Library and application for text file manipulation and command line data mining, a little like the gnu textutils
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
//! stuff

use crate::prelude::*;
use crate::*;
pub use input::Delimiter;
use std::io;
use util::FileLocData;

/// Whether backslash escape handling is enabled while parsing.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub enum BackslashMode {
    /// Backslash has no special meaning.
    #[default]
    Off,
    /// Backslash causes the following byte to be ignored for parser-state changes.
    On,
}

/// Quote handling strategy used while parsing.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum Quotes {
    /// No quote handling.
    #[default]
    None,
    /// A single open/close quote pair.
    Single(u8, u8),
    /// Multiple open/close quote pairs; any open byte can start a quoted region.
    Multi(Vec<(u8, u8)>),
}

/// Behavior when end-of-line is reached while still inside a quote.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub enum UnterminatedQuoteMode {
    /// Continue reading physical lines until quote state closes or EOF is reached.
    #[default]
    ContinueRecord,
    /// End the record at end-of-line even if quote state is still open.
    EndAtEol,
    /// Return `InvalidData` if end-of-line is reached while quote state is still open.
    Error,
}

/// Complete parser configuration used by [`read`].
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Config {
    /// Column delimiter mode. Only use for column parsing, not line parsing.
    pub delimiter: Delimiter,
    /// Quote handling mode.
    pub quotes: Quotes,
    /// Backslash escape handling mode.
    pub backslash: BackslashMode,
    /// Unterminated quote behavior.
    pub unterminated_quote: UnterminatedQuoteMode,
}

impl Config {
    /// Get the byte that represents this delimiter, if applicable.
    #[must_use]
    pub const fn delim(&self) -> u8 {
        self.delimiter.delim()
    }
}

/// The outcome of a call to [`read`].
#[must_use]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub enum ReadResult {
    /// A record ending in `\n` was read.
    #[default]
    Lf,
    /// A record ending in `\r` was read.
    Cr,
    /// A record ending in `\r\n` was read.
    CrLf,
    /// A record was read, but EOF was reached before a terminator.
    NoTerminator,
    /// EOF was reached before any record bytes were read.
    Eof,
}

impl ReadResult {
    /// Returns the EOL as a string slice.
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Lf => "\n",
            Self::Cr => "\r",
            Self::CrLf => "\r\n",
            Self::NoTerminator => "",
            Self::Eof => "",
        }
    }
    /// Returns the EOL as a byte slice.
    #[must_use]
    pub const fn as_bytes(&self) -> &'static [u8] {
        match self {
            Self::Lf => b"\n",
            Self::Cr => b"\r",
            Self::CrLf => b"\r\n",
            Self::NoTerminator => b"",
            Self::Eof => b"",
        }
    }
}

/// Reads a line of input from `reader` into `line`, applying the specified `options`.
///
/// The line is cleared before reading, and the resulting line does not include
/// the record-ending character(s).
/// Returns the record terminator on success, or an error if an I/O error occurs
/// or if the input is invalid according to the options.
/// A line can end with \r, \n, or \r\n, and bytes inside the record are preserved unchanged.
/// `location.bytes` is increased by every byte consumed from `reader`, including
/// stripped record terminators. `location.line` is increased after successful reads
/// except [`ReadResult::Eof`]. Errors include the file name and line number; on
/// error, `location.line` is not increased, but `location.bytes` may already reflect
/// bytes consumed before the error was detected.
///
/// If Eof is returned the line buffer is not modified.
pub fn read(
    line: &mut Vec<u8>,
    reader: &mut dyn BufRead,
    options: &Config,
    location: &mut FileLocData,
) -> anyhow::Result<ReadResult> {
    let available = fill_buf(reader, location)?;
    if available.is_empty() {
        return Ok(ReadResult::Eof);
    }
    line.clear();

    let result = if options.quotes == Quotes::None && options.backslash == BackslashMode::Off {
        read_unquoted(line, reader, location)
    } else {
        read_quoted(line, reader, options, location)
    }?;

    if result != ReadResult::Eof {
        location.line += 1;
    }

    Ok(result)
}

fn read_unquoted(
    line: &mut Vec<u8>,
    reader: &mut dyn BufRead,
    location: &mut FileLocData,
) -> anyhow::Result<ReadResult> {
    loop {
        let available = fill_buf(reader, location)?;
        if available.is_empty() {
            return Ok(if line.is_empty() { ReadResult::Eof } else { ReadResult::NoTerminator });
        }

        if let Some(index) = memchr::memchr2(b'\n', b'\r', available) {
            line.extend_from_slice(&available[..index]);
            if available[index] == b'\r' {
                let consume_len =
                    if available.get(index + 1) == Some(&b'\n') { index + 2 } else { index + 1 };
                let cr_ends_buffer = consume_len == available.len() && available[index] == b'\r';
                consume(reader, location, consume_len);
                if cr_ends_buffer && consume_len == index + 1 && consume_split_lf(reader, location)?
                {
                    return Ok(ReadResult::CrLf);
                }
                return Ok(if consume_len == index + 2 {
                    ReadResult::CrLf
                } else {
                    ReadResult::Cr
                });
            }
            consume(reader, location, index + 1);
            return Ok(ReadResult::Lf);
        }

        let len = available.len();
        line.extend_from_slice(available);
        consume(reader, location, len);
    }
}

fn read_quoted(
    line: &mut Vec<u8>,
    reader: &mut dyn BufRead,
    options: &Config,
    location: &mut FileLocData,
) -> anyhow::Result<ReadResult> {
    let mut close_quote = None;
    let mut escaped = false;

    'read: loop {
        let available = fill_buf(reader, location)?;
        if available.is_empty() {
            return Ok(if line.is_empty() { ReadResult::Eof } else { ReadResult::NoTerminator });
        }

        let mut consumed = 0;
        while consumed < available.len() {
            let byte = available[consumed];

            if byte == b'\n' || byte == b'\r' {
                escaped = false;
                line.extend_from_slice(&available[..consumed]);
                let terminator_in_buffer_len =
                    if byte == b'\r' && available.get(consumed + 1) == Some(&b'\n') {
                        2
                    } else {
                        1
                    };

                if close_quote.is_none()
                    || options.unterminated_quote == UnterminatedQuoteMode::EndAtEol
                {
                    let cr_ends_buffer = byte == b'\r' && consumed + 1 == available.len();
                    consume(reader, location, consumed + terminator_in_buffer_len);
                    if cr_ends_buffer && consume_split_lf(reader, location)? {
                        return Ok(ReadResult::CrLf);
                    }
                    return Ok(if terminator_in_buffer_len == 2 {
                        ReadResult::CrLf
                    } else if byte == b'\r' {
                        ReadResult::Cr
                    } else {
                        ReadResult::Lf
                    });
                }

                if options.unterminated_quote == UnterminatedQuoteMode::Error {
                    let cr_ends_buffer = byte == b'\r' && consumed + 1 == available.len();
                    consume(reader, location, consumed + terminator_in_buffer_len);
                    if cr_ends_buffer {
                        let _ = consume_split_lf(reader, location)?;
                    }
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!(
                            "{}: end of line reached inside quoted field",
                            location_display(location)
                        ),
                    )
                    .into());
                }

                line.extend_from_slice(&available[consumed..consumed + terminator_in_buffer_len]);
                let cr_ends_buffer = byte == b'\r' && consumed + 1 == available.len();
                consume(reader, location, consumed + terminator_in_buffer_len);
                let _ = cr_ends_buffer && append_split_lf(line, reader, location)?;
                continue 'read;
            }

            if options.backslash == BackslashMode::On && escaped {
                escaped = false;
                consumed += 1;
                continue;
            }

            if options.backslash == BackslashMode::On && byte == b'\\' {
                escaped = true;
                consumed += 1;
                continue;
            }

            if let Some(close) = close_quote {
                if byte == close {
                    close_quote = None;
                }
            } else if let Some(close) = quote_close(byte, &options.quotes) {
                close_quote = Some(close);
            }

            consumed += 1;
        }

        if consumed == available.len() {
            line.extend_from_slice(available);
            consume(reader, location, consumed);
        }
    }
}

fn quote_close(open: u8, quotes: &Quotes) -> Option<u8> {
    match quotes {
        Quotes::None => None,
        Quotes::Single(start, end) => (*start == open).then_some(*end),
        Quotes::Multi(pairs) => {
            pairs.iter().find_map(|(start, end)| (*start == open).then_some(*end))
        }
    }
}

fn fill_buf<'a>(reader: &'a mut dyn BufRead, location: &FileLocData) -> anyhow::Result<&'a [u8]> {
    reader.fill_buf().map_err(|err| {
        io::Error::new(err.kind(), format!("{}: {err}", location_display(location))).into()
    })
}

fn location_display(location: &FileLocData) -> String {
    format!("{}:{}", location.name, location.line + 1)
}

pub(crate) fn consume(reader: &mut dyn BufRead, location: &mut FileLocData, bytes: usize) {
    reader.consume(bytes);
    location.bytes += bytes;
}

pub(crate) fn consume_split_lf(
    reader: &mut dyn BufRead,
    location: &mut FileLocData,
) -> anyhow::Result<bool> {
    if fill_buf(reader, location)?.first() == Some(&b'\n') {
        consume(reader, location, 1);
        return Ok(true);
    }
    Ok(false)
}

fn append_split_lf(
    line: &mut Vec<u8>,
    reader: &mut dyn BufRead,
    location: &mut FileLocData,
) -> anyhow::Result<bool> {
    if fill_buf(reader, location)?.first() == Some(&b'\n') {
        line.push(b'\n');
        consume(reader, location, 1);
        return Ok(true);
    }
    Ok(false)
}

#[cfg(test)]
mod tests {
    use std::io::{BufReader, Cursor};

    use super::*;

    struct ErrorReader;

    impl Read for ErrorReader {
        fn read(&mut self, _buffer: &mut [u8]) -> io::Result<usize> {
            Err(io::Error::other("forced read failure"))
        }
    }

    fn read_bytes(input: &[u8], options: &Config) -> anyhow::Result<(Vec<u8>, ReadResult)> {
        let mut reader = Cursor::new(input);
        let mut line = Vec::new();
        let mut location = FileLocData::new("input.txt");
        let result = read(&mut line, &mut reader, options, &mut location)?;
        Ok((line, result))
    }

    #[test]
    fn unquoted_strips_line_terminators() -> anyhow::Result<()> {
        let options = Config::default();

        assert_eq!(read_bytes(b"abc\nnext", &options)?, (b"abc".to_vec(), ReadResult::Lf));
        assert_eq!(read_bytes(b"abc\rnext", &options)?, (b"abc".to_vec(), ReadResult::Cr));
        assert_eq!(read_bytes(b"abc\r\nnext", &options)?, (b"abc".to_vec(), ReadResult::CrLf));
        assert_eq!(read_bytes(b"abc", &options)?, (b"abc".to_vec(), ReadResult::NoTerminator));
        assert_eq!(read_bytes(b"", &options)?, (Vec::new(), ReadResult::Eof));

        Ok(())
    }

    #[test]
    fn continued_quote_preserves_inner_line_ending() -> anyhow::Result<()> {
        let options = Config { quotes: Quotes::Single(b'"', b'"'), ..Config::default() };

        assert_eq!(
            read_bytes(b"\"a\r\nb\"\r\nnext", &options)?,
            (b"\"a\r\nb\"".to_vec(), ReadResult::CrLf)
        );

        Ok(())
    }

    #[test]
    fn unterminated_quote_can_end_at_eol() -> anyhow::Result<()> {
        let options = Config {
            quotes: Quotes::Single(b'"', b'"'),
            unterminated_quote: UnterminatedQuoteMode::EndAtEol,
            ..Config::default()
        };

        assert_eq!(read_bytes(b"\"abc\nnext", &options)?, (b"\"abc".to_vec(), ReadResult::Lf));

        Ok(())
    }

    #[test]
    fn unterminated_quote_can_error() {
        let options = Config {
            quotes: Quotes::Single(b'"', b'"'),
            unterminated_quote: UnterminatedQuoteMode::Error,
            ..Config::default()
        };

        assert!(read_bytes(b"\"abc\nnext", &options).is_err());
    }

    #[test]
    fn backslash_preserves_bytes_and_escapes_quote_state() -> anyhow::Result<()> {
        let options = Config {
            quotes: Quotes::Single(b'"', b'"'),
            backslash: BackslashMode::On,
            ..Config::default()
        };

        assert_eq!(
            read_bytes(br#""a\"b":tail"#, &options)?,
            (br#""a\"b":tail"#.to_vec(), ReadResult::NoTerminator)
        );

        Ok(())
    }

    #[test]
    fn location_counts_consumed_bytes_and_successful_records() -> anyhow::Result<()> {
        let options = Config::default();
        let mut reader = Cursor::new(b"abc\r\nnext".as_slice());
        let mut line = Vec::new();
        let mut location = FileLocData::new("input.txt");

        assert_eq!(read(&mut line, &mut reader, &options, &mut location)?, ReadResult::CrLf);
        assert_eq!(line, b"abc");
        assert_eq!(location.line, 1);
        assert_eq!(location.bytes, 5);

        assert_eq!(
            read(&mut line, &mut reader, &options, &mut location)?,
            ReadResult::NoTerminator
        );
        assert_eq!(line, b"next");
        assert_eq!(location.line, 2);
        assert_eq!(location.bytes, 9);

        assert_eq!(read(&mut line, &mut reader, &options, &mut location)?, ReadResult::Eof);
        assert_eq!(line, b"next");
        assert_eq!(location.line, 2);
        assert_eq!(location.bytes, 9);

        Ok(())
    }

    #[test]
    fn location_counts_consumed_bytes_for_continued_quotes() -> anyhow::Result<()> {
        let options = Config { quotes: Quotes::Single(b'"', b'"'), ..Config::default() };
        let mut reader = Cursor::new(b"\"a\r\nb\"\r\nnext".as_slice());
        let mut line = Vec::new();
        let mut location = FileLocData::new("input.txt");

        assert_eq!(read(&mut line, &mut reader, &options, &mut location)?, ReadResult::CrLf);
        assert_eq!(line, b"\"a\r\nb\"");
        assert_eq!(location.line, 1);
        assert_eq!(location.bytes, 8);

        Ok(())
    }

    #[test]
    fn location_counts_split_crlf_for_unquoted_records() -> anyhow::Result<()> {
        let options = Config::default();
        let cursor = Cursor::new(b"abc\r\nnext".as_slice());
        let mut reader = BufReader::with_capacity(4, cursor);
        let mut line = Vec::new();
        let mut location = FileLocData::new("input.txt");

        assert_eq!(read(&mut line, &mut reader, &options, &mut location)?, ReadResult::CrLf);
        assert_eq!(line, b"abc");
        assert_eq!(location.line, 1);
        assert_eq!(location.bytes, 5);

        Ok(())
    }

    #[test]
    fn location_counts_split_crlf_inside_continued_quotes() -> anyhow::Result<()> {
        let options = Config { quotes: Quotes::Single(b'"', b'"'), ..Config::default() };
        let cursor = Cursor::new(b"\"a\r\nb\"\nnext".as_slice());
        let mut reader = BufReader::with_capacity(3, cursor);
        let mut line = Vec::new();
        let mut location = FileLocData::new("input.txt");

        assert_eq!(read(&mut line, &mut reader, &options, &mut location)?, ReadResult::Lf);
        assert_eq!(line, b"\"a\r\nb\"");
        assert_eq!(location.line, 1);
        assert_eq!(location.bytes, 7);

        Ok(())
    }

    #[test]
    fn error_messages_include_file_name_and_line_number() -> anyhow::Result<()> {
        let options = Config {
            quotes: Quotes::Single(b'"', b'"'),
            unterminated_quote: UnterminatedQuoteMode::Error,
            ..Config::default()
        };
        let mut reader = Cursor::new(b"ok\n\"abc\nnext".as_slice());
        let mut line = Vec::new();
        let mut location = FileLocData::new("input.txt");

        assert_eq!(read(&mut line, &mut reader, &options, &mut location)?, ReadResult::Lf);
        let error = read(&mut line, &mut reader, &options, &mut location)
            .expect_err("unterminated quote should error");

        assert!(error.to_string().contains("input.txt:2:"), "{error:#}");
        assert_eq!(location.line, 1);
        assert_eq!(location.bytes, 8);

        Ok(())
    }

    #[test]
    fn io_error_messages_include_file_name_and_line_number() {
        let options = Config::default();
        let mut reader = BufReader::new(ErrorReader);
        let mut line = Vec::new();
        let mut location = FileLocData::new("broken.txt");

        let error =
            read(&mut line, &mut reader, &options, &mut location).expect_err("reader should fail");

        assert!(error.to_string().contains("broken.txt:1:"), "{error:#}");
        assert!(error.to_string().contains("forced read failure"), "{error:#}");
        assert_eq!(location.line, 0);
        assert_eq!(location.bytes, 0);
    }
}