1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

enum Delimiter; misc changes

This commit is contained in:
TechHara 2022-12-20 20:37:31 -05:00
parent c29839f4f9
commit f2a7175144
2 changed files with 94 additions and 88 deletions

View file

@ -118,11 +118,15 @@ struct Options {
zero_terminated: bool,
}
enum Delimiter {
Whitespace,
String(String), // one char long, String because of UTF8 representation
}
struct FieldOptions {
delimiter: String, // one char long, String because of UTF8 representation
delimiter: Delimiter,
out_delimiter: Option<String>,
only_delimited: bool,
whitespace_delimited: bool,
zero_terminated: bool,
}
@ -352,8 +356,8 @@ fn cut_fields_whitespace<R: Read>(
#[allow(clippy::cognitive_complexity)]
fn cut_fields<R: Read>(reader: R, ranges: &[Range], opts: &FieldOptions) -> UResult<()> {
let newline_char = if opts.zero_terminated { b'\0' } else { b'\n' };
if opts.whitespace_delimited {
match opts.delimiter {
Delimiter::Whitespace => {
return cut_fields_whitespace(
reader,
ranges,
@ -363,13 +367,14 @@ fn cut_fields<R: Read>(reader: R, ranges: &[Range], opts: &FieldOptions) -> URes
Some(ref delim) => delim,
_ => "\t",
},
);
)
}
Delimiter::String(ref delimiter) => {
if let Some(ref o_delim) = opts.out_delimiter {
return cut_fields_delimiter(
reader,
ranges,
&opts.delimiter,
&delimiter,
opts.only_delimited,
newline_char,
o_delim,
@ -378,12 +383,12 @@ fn cut_fields<R: Read>(reader: R, ranges: &[Range], opts: &FieldOptions) -> URes
let mut buf_in = BufReader::new(reader);
let mut out = stdout_writer();
let delim_len = opts.delimiter.len();
let delim_len = delimiter.len();
let result = buf_in.for_byte_record_with_terminator(newline_char, |line| {
let mut fields_pos = 1;
let mut low_idx = 0;
let mut delim_search = Searcher::new(line, opts.delimiter.as_bytes()).peekable();
let mut delim_search = Searcher::new(line, delimiter.as_bytes()).peekable();
let mut print_delim = false;
if delim_search.peek().is_none() {
@ -441,6 +446,8 @@ fn cut_fields<R: Read>(reader: R, ranges: &[Range], opts: &FieldOptions) -> URes
}
Ok(())
}
}
}
fn cut_files(mut filenames: Vec<String>, mode: &Mode) {
@ -585,10 +592,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(Mode::Fields(
ranges,
FieldOptions {
delimiter: delim,
delimiter: Delimiter::String(delim),
out_delimiter: out_delim,
only_delimited,
whitespace_delimited,
zero_terminated,
},
))
@ -597,10 +603,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
None => Ok(Mode::Fields(
ranges,
FieldOptions {
delimiter: "\t".to_owned(),
delimiter: match whitespace_delimited {
true => Delimiter::Whitespace,
false => Delimiter::String("\t".to_owned()),
},
out_delimiter: out_delim,
only_delimited,
whitespace_delimited,
zero_terminated,
},
)),

View file

@ -1,11 +1,9 @@
// This file is part of the uutils coreutils package.
//
// (c) Rolf Morel <rolfmorel@gmail.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// cSpell:ignore multispace
// spell-checker:ignore multispace
use memchr::memchr2;