From a79a5f5c30af4172b68d90a8b6bc2c6998bc040c Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Tue, 27 Feb 2024 21:25:59 -0800 Subject: [PATCH 1/2] Remove enumerate from cut_str inner loop --- src/cut_str.rs | 77 ++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/src/cut_str.rs b/src/cut_str.rs index 7d29f9c..2b7b5ba 100644 --- a/src/cut_str.rs +++ b/src/cut_str.rs @@ -389,47 +389,44 @@ pub fn cut_str( write_maybe_as_json!(stdout, line, opt.json); } _ => { - bounds - .iter() - .enumerate() - .try_for_each(|(i, bof)| -> Result<()> { - let b = match bof { - BoundOrFiller::Filler(f) => { - stdout.write_all(f.as_bytes())?; - return Ok(()); - } - BoundOrFiller::Bound(b) => b, - }; - - let r = b.try_into_range(num_fields); - - let output = if r.is_ok() { - let r = r.unwrap(); - let idx_start = fields[r.start].start; - let idx_end = fields[r.end - 1].end; - &line[idx_start..idx_end] - } else if b.fallback_oob.is_some() { - b.fallback_oob.as_ref().unwrap() - } else if let Some(generic_fallback) = &opt.fallback_oob { - generic_fallback - } else { - return Err(r.unwrap_err()); - }; - - let field_to_print = maybe_replace_delimiter(output, opt); - write_maybe_as_json!(stdout, field_to_print, opt.json); - - if opt.join && i != bounds.len() - 1 { - stdout.write_all( - opt.replace_delimiter - .as_ref() - .unwrap_or(&opt.delimiter) - .as_bytes(), - )?; + bounds.iter().try_for_each(|bof| -> Result<()> { + let b = match bof { + BoundOrFiller::Filler(f) => { + stdout.write_all(f.as_bytes())?; + return Ok(()); } - - Ok(()) - })?; + BoundOrFiller::Bound(b) => b, + }; + + let r = b.try_into_range(num_fields); + + let output = if r.is_ok() { + let r = r.unwrap(); + let idx_start = fields[r.start].start; + let idx_end = fields[r.end - 1].end; + &line[idx_start..idx_end] + } else if b.fallback_oob.is_some() { + b.fallback_oob.as_ref().unwrap() + } else if let Some(generic_fallback) = &opt.fallback_oob { + generic_fallback + } else { + return Err(r.unwrap_err()); + }; + + let field_to_print = maybe_replace_delimiter(output, opt); + write_maybe_as_json!(stdout, field_to_print, opt.json); + + if opt.join && !b.is_last { + stdout.write_all( + opt.replace_delimiter + .as_ref() + .unwrap_or(&opt.delimiter) + .as_bytes(), + )?; + } + + Ok(()) + })?; } } From 66e1bcc5c6ceea409de4a9d87cbdbb4e78eefda5 Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Tue, 27 Feb 2024 21:26:13 -0800 Subject: [PATCH 2/2] Add missing CHANGELOG entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb1cd80..9497d8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), * fields cut is now done on bytes, not strings (as long as your delimiter is proper utf-8 you'll be fine) - feat: display short help when run without arguments +- feat: add the ability to display fallback output when a field is out of bound + (you can set it per-field using `-f =somefallback` or by providing + a generic fallback using `--fallback-oob somefallback`) - feat: --characters now depends on the (default) regex feature - feat: help and short help are colored, as long as output is a tty and unless env var TERM=dumb or NO_COLOR (any value) is set