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
use std::iter::once;
use std::num::NonZeroUsize;

use bstr::ByteSlice;
use regex::bytes::Regex;
use simd_csv::ByteRecord;

use crate::config::{Config, Delimiter};
use crate::select::SelectedColumns;
use crate::util;
use crate::CliResult;

static USAGE: &str = r#"
Separate a single column into multiple ones by splitting its cells according
to some splitting algorithm that can be one of:

    * (default): splitting by a single substring
    * -r, --regex: splitting using a regular expression
    * -m, --match: decomposing into regular expression matches
    * -c, --capture-groups: decomposing into regular expression capture groups
    * --fixed-width: cutting every <n> bytes
    * --widths: split by a list of consecutive widths
    * --cuts: cut at predefined byte offsets
    * --offsets: extract byte slices

Created columns can be given a name using the --into flag, else they will be
given generic names based on the original column name. For instance, splitting a
column named "text" will produce columns named "text1", "text2"...

It is also possible to limit the number of splits using the --max flag.

If the number of splits is known beforehand (that is to say when using --into
or --max or --widths or --cuts or --offsets), the command will be able to stream
the data. Else it will have to buffer the whole file into memory to record the
maximum number of splits produced by the selected method.

Finally, note that by default, the separated column will be removed from the output,
unless the -k/--keep flag is used.

Examples:

  Splitting a full name
    $ xan separate fullname ' ' data.csv
    $ xan separate --into first_name,last_name ' ' data.csv

  Splitting a full name using a regular expression
    $ xan separate -r fullname '\s+' data.csv

  Extracting digit sequences from a column named 'birthdate' using a regex:
    $ xan separate -r -m birthdate '\d+' data.csv

  Extracting year, month and day from a column named 'date' using capture groups:
    $ xan separate date '(\d{4})-(\d{2})-(\d{2})' data.csv -r -c --into year,month,day

  Splitting a column named 'code' into sequences of 3 bytes:
    $ xan separate code --fixed-width 3 data.csv

  Splitting a column named 'code' into parts of widths 2, 4 and 3:
    $ xan separate code --widths 2,4,3 data.csv

  Splitting a column named 'code' on bytes 2 and 6:
    $ xan separate code --cuts 2,6 data.csv

  Split column named 'code' into of segments defined by byte offsets [0, 2), [2, 6) and [6, 9):
    $ xan separate code --offsets 0,2,6,9 data.csv

Usage:
    xan separate [options] <column> <separator> [<input>]
    xan separate --help

separate mode options:
    -r, --regex           Split cells using a regular expression instead of using
                          a simple substring.
    -m, --match           When using -r/--regex, extract parts of the cell matching
                          the regex pattern.
    -c, --capture-groups  When using -r/--regex, extract parts of the call matching
                          the regex pattern's capture groups.
    --fixed-width         Split cells every <separator> bytes. Each resulting part
                          will then be trimmed of leading/trailing whitespace.
    --widths              Split cells using the given widths (given as a comma-separated
                          list of integers). Each resulting part will then be trimmed of
                          leading/trailing whitespace.
    --cuts                Split cells on the given bytes (given as a comma-separated
                          list of increasing, non-repeating integers). Each resulting part
                          will then be trimmed of leading/trailing whitespace.
    --offsets             Split cells according to the specified byte offsets (given as a
                          comma-separated list of increasing, non-repeating integers).
                          Each resulting part will then be trimmed of leading/trailing whitespace.

separate options:
    -M, --max <n>          Limit the number of cells splitted to at most <n>.
                           By default, all possible splits are made.
    --into <column-names>  Specify names for the new columns created by the
                           splits. If not provided, new columns will be named
                           before the original column name ('text' column will
                           be separated into 'text1', 'text2', etc.). If used with --max,
                           the number of names provided must be equal or lower
                           than <n>. Cannot be used with --prefix.
    --prefix <prefix>      Specify a prefix for the new columns created by the
                           splits. By default, no prefix is used and new columns
                           are named before the original column name ('text'
                           column will be separated into 'text1', 'text2', etc.).
                           Cannot be used with --into.
    --too-many <option>    Specify how to handle extra cells when the number
                           of splitted cells exceeds --max, or
                           the number of provided names with --into.
                           Must be one of:
                                - 'error': stop as soon as an inconsistent number
                                    of splits is produced.
                                - 'drop': drop splits over expected maximum.
                                - 'merge': append the rest of the cell to the last
                                    produced split.
                           Note that 'merge' cannot be used with -m/--match
                           nor -c/--capture-groups.
                           [default: error]
    -k, --keep             Keep the separated column after splitting, instead of
                           discarding it.

Common options:
    -h, --help               Display this message
    -o, --output <file>      Write output to <file> instead of stdout.
    -n, --no-headers         When set, the first row will not be evaled
                             as headers.
    -d, --delimiter <arg>    The field delimiter for reading CSV data.
                             Must be a single character.
"#;

#[derive(Deserialize, Debug)]
struct Args {
    arg_column: String,
    arg_separator: String,
    arg_input: Option<String>,
    flag_regex: bool,
    flag_match: bool,
    flag_capture_groups: bool,
    flag_keep: bool,
    flag_max: Option<usize>,
    flag_into: Option<String>,
    flag_prefix: Option<String>,
    flag_too_many: TooManyMode,
    flag_output: Option<String>,
    flag_no_headers: bool,
    flag_delimiter: Option<Delimiter>,
    flag_fixed_width: bool,
    flag_widths: bool,
    flag_cuts: bool,
    flag_offsets: bool,
}

#[derive(Debug, Clone, Copy, Deserialize)]
enum TooManyMode {
    Error,
    Drop,
    Merge,
}

impl TooManyMode {
    fn requires_splitn(&self) -> bool {
        matches!(self, Self::Merge | Self::Drop)
    }
}

#[derive(Debug)]
enum RegexMode {
    Split,
    Match,
    CaptureGroups,
}

#[derive(Debug)]
enum Splitter {
    Substring(Vec<u8>),
    Regex(Regex, RegexMode),
    FixedWidth(usize),
    Offsets(Vec<usize>, bool),
}

impl Splitter {
    fn static_count_splits(&self) -> Option<usize> {
        match self {
            Self::Offsets(offsets, has_implicit_end) => {
                Some(offsets.len() - (if *has_implicit_end { 0 } else { 1 }))
            }
            _ => None,
        }
    }

    fn as_offsets(&self) -> Option<&[usize]> {
        match self {
            Self::Offsets(offsets, _) => Some(offsets),
            _ => None,
        }
    }

    fn count_splits(&self, cell: &[u8]) -> usize {
        match self {
            Self::Substring(sep) => cell.find_iter(sep).count() + 1,
            Self::Regex(pattern, mode) => match mode {
                RegexMode::Split => pattern.find_iter(cell).count() + 1,
                RegexMode::CaptureGroups => pattern.captures_iter(cell).map(|m| m.len() - 1).sum(),
                RegexMode::Match => pattern.find_iter(cell).count(),
            },
            Self::FixedWidth(width) => cell.len().div_ceil(*width),
            Self::Offsets(_, _) => unreachable!(),
        }
    }

    fn split<'s, 'c>(&'s self, cell: &'c [u8]) -> Box<dyn Iterator<Item = &'c [u8]> + 's>
    where
        'c: 's,
    {
        match self {
            Self::Substring(sep) => Box::new(cell.split_str(sep)),
            Self::Regex(pattern, mode) => match mode {
                RegexMode::Split => Box::new(pattern.split(cell)),
                RegexMode::CaptureGroups => {
                    Box::new(pattern.captures_iter(cell).flat_map(|caps| {
                        caps.iter()
                            .skip(1)
                            .map(|m| m.map(|b| b.as_bytes()).unwrap_or(b""))
                            .collect::<Vec<_>>()
                    }))
                }
                RegexMode::Match => Box::new(pattern.find_iter(cell).map(|m| m.as_bytes())),
            },
            Self::FixedWidth(width) => Box::new(cell.chunks(*width).map(|chunk| chunk.trim())),
            Self::Offsets(offsets, has_implicit_end) => {
                let mut splits = Vec::<&[u8]>::with_capacity(
                    offsets.len() - (if *has_implicit_end { 0 } else { 1 }),
                );

                let mut must_add_implicit_end = *has_implicit_end;

                for window in offsets.windows(2) {
                    let start = window[0];
                    let end = window[1].min(cell.len());

                    if start >= cell.len() {
                        must_add_implicit_end = false;
                        break;
                    }

                    splits.push(cell[start..end].trim());
                }

                if must_add_implicit_end {
                    let start = *offsets.last().unwrap();

                    if start < cell.len() {
                        splits.push(cell[start..].trim())
                    }
                }

                Box::new(splits.into_iter())
            }
        }
    }

    fn splitn<'s, 'c>(
        &'s self,
        limit: usize,
        cell: &'c [u8],
    ) -> Box<dyn Iterator<Item = &'c [u8]> + 's>
    where
        'c: 's,
    {
        match self {
            Self::Substring(sep) => Box::new(cell.splitn_str(limit, sep)),
            Self::Regex(pattern, mode) => match mode {
                RegexMode::Split => Box::new(pattern.splitn(cell, limit)),
                RegexMode::CaptureGroups => {
                    unimplemented!()
                }
                RegexMode::Match => unimplemented!(),
            },
            Self::FixedWidth(width) => Box::new(cell.chunks(*width).take(limit)),
            Self::Offsets(_, _) => unimplemented!(),
        }
    }

    fn split_cell(
        &self,
        cell: &[u8],
        max: usize,
        too_many_mode: TooManyMode,
    ) -> CliResult<ByteRecord> {
        let mut output_record = ByteRecord::new();

        match too_many_mode {
            TooManyMode::Error => {
                for sub_cell in self.split(cell) {
                    output_record.push_field(sub_cell);
                }

                if output_record.len() > max {
                    Err(format!("Number of splits exceeded expected maximum {} but got {}. Consider using the --too-many flag to handle extra splitted cells.", max, output_record.len()))?;
                }
            }
            TooManyMode::Drop => {
                for sub_cell in self.split(cell).take(max) {
                    output_record.push_field(sub_cell);
                }
            }
            TooManyMode::Merge => {
                for sub_cell in self.splitn(max, cell) {
                    output_record.push_field(sub_cell);
                }
            }
        };

        // Padding
        while output_record.len() < max {
            output_record.push_field(b"");
        }

        Ok(output_record)
    }
}

pub fn run(argv: &[&str]) -> CliResult<()> {
    let args: Args = util::get_args(USAGE, argv)?;

    let segmenters_count = args.flag_fixed_width as u8
        + args.flag_widths as u8
        + args.flag_cuts as u8
        + args.flag_offsets as u8;

    if segmenters_count > 1 {
        Err("Only one of --fixed-width, --widths, --cuts or --offsets argument can be used!")?;
    }

    if args.flag_fixed_width && (args.flag_regex || args.flag_capture_groups || args.flag_match) {
        Err("--fixed-width cannot be used with -r/--regex, -c/--capture-groups nor -m/--match!")?;
    }
    if args.flag_widths || args.flag_cuts || args.flag_offsets {
        if args.flag_regex || args.flag_capture_groups || args.flag_match {
            Err("--width, --cuts, --offsets cannot be used with -r/--regex, -c/--capture-groups nor -m/--match!")?;
        } else if args.flag_max.is_some() {
            Err("--widths, --cuts, --offsets cannot be used with --max!")?;
        }
    }

    if args.flag_capture_groups || args.flag_match {
        if !args.flag_regex {
            Err("-c/--capture-groups and -m/--match can only be used with --regex!")?;
        }
        if args.flag_capture_groups && args.flag_match {
            Err("-c/--capture-groups and -m/--match cannot be used together!")?;
        }
    }

    let too_many_mode = args.flag_too_many;

    if too_many_mode.requires_splitn() {
        if args.flag_max.is_none() && args.flag_into.is_none() {
            Err("--too-many can only be used with --max or --into!")?;
        }

        if (args.flag_capture_groups || args.flag_match)
            && matches!(too_many_mode, TooManyMode::Merge)
        {
            Err("--too-many merge doesn't work with -c/--capture-groups nor -m/--match!")?;
        }
    }

    let new_column_names = args
        .flag_into
        .as_ref()
        .map(|names| util::str_to_csv_byte_record(names));

    match (args.flag_max, &new_column_names, &args.flag_prefix) {
        (_, Some(_), Some(_)) => {
            Err("--into and --prefix cannot be used together!")?;
        }
        (Some(max), Some(names), _) if names.len() > max => {
            Err(format!("--into cannot specify more column names than --max : got {} for --into and {} for --max", names.len(), max))?;
        }
        _ => (),
    }

    let column_to_separate_name = args.arg_column;
    let column_to_separate_sel = SelectedColumns::try_from(column_to_separate_name.clone())?;

    let rconf = Config::new(&args.arg_input)
        .no_headers(args.flag_no_headers)
        .select(column_to_separate_sel)
        .delimiter(args.flag_delimiter);

    let mut wtr = Config::new(&args.flag_output).simd_writer()?;

    let mut rdr = rconf.simd_reader()?;
    let headers = rdr.byte_headers()?.clone();

    let separated_column_index = rconf.single_selection(&headers)?;

    let splitter = if args.flag_regex {
        let regex_mode = if args.flag_match {
            RegexMode::Match
        } else if args.flag_capture_groups {
            RegexMode::CaptureGroups
        } else {
            RegexMode::Split
        };

        Splitter::Regex(Regex::new(&args.arg_separator)?, regex_mode)
    } else if args.flag_fixed_width {
        Splitter::FixedWidth(
            args.arg_separator
                .parse::<NonZeroUsize>()
                .map(NonZeroUsize::get)
                .map_err(|_| "Invalid value for --fixed-width. It must be a positive integer!")?,
        )
    } else if args.flag_widths || args.flag_cuts || args.flag_offsets {
        let widths_or_offsets: Vec<usize> = args
            .arg_separator
            .split(',')
            .map(|s| s.trim().parse::<usize>())
            .collect::<Result<Vec<_>, _>>()
            .map_err(|_| "Invalid values given to --widths, --offsets or --cuts!")?;

        let splitter = if args.flag_widths {
            let mut offsets: Vec<usize> = vec![0];
            let mut cumulative = 0;
            for width in widths_or_offsets.iter() {
                cumulative += *width;
                offsets.push(cumulative);
            }

            Splitter::Offsets(offsets, false)
        } else if args.flag_cuts {
            Splitter::Offsets(once(0).chain(widths_or_offsets).collect(), true)
        } else if widths_or_offsets.len() < 2 {
            Err("--offsets requires at least two byte offsets!")?
        } else {
            Splitter::Offsets(widths_or_offsets, false)
        };

        if let Some(names) = &new_column_names {
            if names.len() > splitter.static_count_splits().unwrap() {
                Err("--into cannot specify more column names than implied by --widths, --cuts or --offsets!")?;
            }
        }

        let mut offsets = splitter.as_offsets().unwrap().to_vec();

        if !offsets.is_sorted() {
            Err(format!("values given to --cuts or --offsets should be monotonically increasing but got {}!", &args.arg_separator))?;
        }

        let len_before = offsets.len();
        offsets.dedup();

        if len_before != offsets.len() {
            Err(format!(
                "values given to --cuts or --offsets should not be repeated but got {}!",
                &args.arg_separator
            ))?;
        }

        splitter
    } else {
        Splitter::Substring(args.arg_separator.as_bytes().to_vec())
    };

    let mut buffered_records: Option<Vec<ByteRecord>> = None;

    let max_splits = if let Some(n) = args.flag_max {
        n
    } else if let Some(names) = &new_column_names {
        names.len()
    } else if let Some(n) = splitter.static_count_splits() {
        n
    } else {
        // We need to buffer the records to memory to know what the max number
        // of splits is.
        let mut max_seen = 0;
        let mut records = Vec::new();

        for result in rdr.byte_records() {
            let record = result?;

            let numsplits = splitter.count_splits(&record[separated_column_index]);
            max_seen = max_seen.max(numsplits);

            records.push(record);
        }

        buffered_records = Some(records);

        max_seen
    };

    // Writing headers
    if !rconf.no_headers {
        let mut new_headers = ByteRecord::new();
        new_headers.extend(
            headers
                .iter()
                .take(separated_column_index + args.flag_keep as usize),
        );

        let mut number_of_new_columns = max_splits;
        let prefix = args.flag_prefix.unwrap_or(column_to_separate_name);

        if let Some(names) = &new_column_names {
            new_headers.extend(names);
            number_of_new_columns -= names.len();
        }

        for i in 1..=number_of_new_columns {
            let header_name = format!("{}{}", prefix, i);
            new_headers.push_field(header_name.as_bytes());
        }

        new_headers.extend(headers.iter().skip(separated_column_index + 1));

        wtr.write_byte_record(&new_headers)?;
    }

    // Flushing
    let mut process_record = |record: &ByteRecord| -> CliResult<()> {
        let splitted =
            splitter.split_cell(&record[separated_column_index], max_splits, too_many_mode)?;

        wtr.write_record(
            record
                .iter()
                .take(separated_column_index + args.flag_keep as usize)
                .chain(splitted.iter())
                .chain(record.iter().skip(separated_column_index + 1)),
        )?;

        Ok(())
    };

    if let Some(records) = buffered_records {
        for record in records {
            process_record(&record)?;
        }
    } else {
        let mut record = ByteRecord::new();

        while rdr.read_byte_record(&mut record)? {
            process_record(&record)?;
        }
    }

    Ok(wtr.flush()?)
}