Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bounds match #131

Merged
merged 8 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/bin/tuc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn parse_args() -> Result<Opt, pico_args::Error> {
};

if bounds_type == BoundsType::Fields
&& (maybe_fields.is_none() || maybe_fields.as_ref().unwrap().0.is_empty())
&& (maybe_fields.is_none() || maybe_fields.as_ref().unwrap().is_empty())
{
eprintln!("tuc: invariant error. At this point we expected to find at least 1 field bound");
std::process::exit(1);
Expand Down Expand Up @@ -209,12 +209,7 @@ fn parse_args() -> Result<Opt, pico_args::Error> {
.or(maybe_lines)
.unwrap();

if has_json
&& bounds
.0
.iter()
.any(|s| matches!(s, BoundOrFiller::Filler(_)))
{
if has_json && bounds.iter().any(|s| matches!(s, BoundOrFiller::Filler(_))) {
eprintln!("tuc: runtime error. Cannot format fields when using --json");
std::process::exit(1);
}
Expand Down
133 changes: 98 additions & 35 deletions src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,65 @@ pub fn parse_bounds_list(s: &str) -> Result<Vec<BoundOrFiller>> {
}
}

#[derive(Debug)]
pub struct UserBoundsList(pub Vec<BoundOrFiller>);
#[derive(Debug, Clone)]
pub struct UserBoundsList {
pub list: Vec<BoundOrFiller>,
/// Optimization that we can use to stop searching for fields.
/// It's available only when every bound uses positive indexes.
/// When conditions do not apply, its value is `Side::Continue`.
pub last_interesting_field: Side,
}

impl Deref for UserBoundsList {
type Target = Vec<BoundOrFiller>;

fn deref(&self) -> &Self::Target {
&self.0
&self.list
}
}

impl From<Vec<BoundOrFiller>> for UserBoundsList {
fn from(list: Vec<BoundOrFiller>) -> Self {
let mut ubl = UserBoundsList {
list,
last_interesting_field: Side::Continue,
};

let mut rightmost_bound: Option<Side> = None;
let mut last_bound: Option<&mut UserBounds> = None;

let is_sortable = ubl.is_sortable();

ubl.list.iter_mut().for_each(|bof| {
if let BoundOrFiller::Bound(b) = bof {
if rightmost_bound.is_none() || b.r > rightmost_bound.unwrap() {
rightmost_bound = Some(b.r);
}

last_bound = Some(b);
}
});

if !is_sortable {
rightmost_bound = None;
}

last_bound
.expect("UserBoundsList must contain at least one UserBounds")
.is_last = true;

ubl.last_interesting_field = rightmost_bound.unwrap_or(Side::Continue);
ubl
}
}

impl FromStr for UserBoundsList {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(UserBoundsList(parse_bounds_list(s)?))
if s.trim().is_empty() {
bail!("UserBoundsList must contain at least one UserBounds");
}
Ok(parse_bounds_list(s)?.into())
}
}

Expand Down Expand Up @@ -142,7 +186,7 @@ impl UserBoundsList {
}

fn get_userbounds_only(&self) -> impl Iterator<Item = &UserBounds> + '_ {
self.0.iter().flat_map(|b| match b {
self.list.iter().flat_map(|b| match b {
BoundOrFiller::Bound(x) => Some(x),
_ => None,
})
Expand Down Expand Up @@ -192,17 +236,18 @@ impl UserBoundsList {
* and with every ranged bound converted into single slot bounds.
*/
pub fn unpack(&self, num_fields: usize) -> UserBoundsList {
UserBoundsList(
self.0
.iter()
.filter_map(|x| match x {
BoundOrFiller::Filler(_) => None,
BoundOrFiller::Bound(b) => Some(b.unpack(num_fields)),
})
.flatten()
.map(BoundOrFiller::Bound)
.collect(),
)
let list: Vec<BoundOrFiller> = self
.list
.iter()
.filter_map(|x| match x {
BoundOrFiller::Filler(_) => None,
BoundOrFiller::Bound(b) => Some(b.unpack(num_fields)),
})
.flatten()
.map(BoundOrFiller::Bound)
.collect();

list.into()
}
}

Expand Down Expand Up @@ -256,6 +301,7 @@ impl PartialOrd for Side {
pub struct UserBounds {
pub l: Side,
pub r: Side,
pub is_last: bool,
}

impl fmt::Display for UserBounds {
Expand Down Expand Up @@ -314,9 +360,20 @@ impl FromStr for UserBounds {
}
}

impl UserBounds {
pub fn new(l: Side, r: Side) -> Self {
UserBounds { l, r }
pub trait UserBoundsTrait<T> {
fn new(l: Side, r: Side) -> Self;
fn try_into_range(&self, parts_length: usize) -> Result<Range<usize>>;
fn matches(&self, idx: T) -> Result<bool>;
fn unpack(&self, num_fields: usize) -> Vec<UserBounds>;
}

impl UserBoundsTrait<i32> for UserBounds {
fn new(l: Side, r: Side) -> Self {
UserBounds {
l,
r,
is_last: false,
}
}
/**
* Check if a field is between the bounds.
Expand All @@ -328,7 +385,7 @@ impl UserBounds {
* Fields are 1-indexed.
*/
#[inline(always)]
pub fn matches(&self, idx: i32) -> Result<bool> {
fn matches(&self, idx: i32) -> Result<bool> {
match (self.l, self.r) {
(Side::Some(left), _) if (left * idx).is_negative() => {
bail!(
Expand Down Expand Up @@ -363,21 +420,22 @@ impl UserBounds {
/// e.g.
///
/// ```rust
/// # use tuc::bounds::UserBounds;
/// # use tuc::bounds::{UserBounds, UserBoundsTrait};
/// # use std::ops::Range;
/// # use tuc::bounds::Side;
/// # use std::str::FromStr;
///
/// assert_eq!(
/// (UserBounds { l: Side::Some(1), r: Side::Some(2) }).try_into_range(5).unwrap(),
/// UserBounds::from_str("1:2").unwrap().try_into_range(5).unwrap(),
/// Range { start: 0, end: 2} // 2, not 1, because it's exclusive
/// );
///
/// assert_eq!(
/// (UserBounds { l: Side::Some(1), r: Side::Continue }).try_into_range(5).unwrap(),
/// UserBounds::from_str("1:").unwrap().try_into_range(5).unwrap(),
/// Range { start: 0, end: 5}
/// );
/// ```
pub fn try_into_range(&self, parts_length: usize) -> Result<Range<usize>> {
fn try_into_range(&self, parts_length: usize) -> Result<Range<usize>> {
let start: usize = match self.l {
Side::Continue => 0,
Side::Some(v) => {
Expand Down Expand Up @@ -414,11 +472,9 @@ impl UserBounds {
Ok(Range { start, end })
}

/**
* Transform a ranged bound into a list of one or more
* 1 slot bound
*/
pub fn unpack(&self, num_fields: usize) -> Vec<UserBounds> {
/// Transform a ranged bound into a list of one or more
/// slot bound
fn unpack(&self, num_fields: usize) -> Vec<UserBounds> {
let mut bounds = Vec::new();
let n: i32 = num_fields
.try_into()
Expand Down Expand Up @@ -714,9 +770,12 @@ mod tests {
}

#[test]
fn test_user_bounds_is_sortable() {
assert!(UserBoundsList(Vec::new()).is_sortable());
fn test_user_bounds_cannot_be_empty() {
assert!(UserBoundsList::from_str("").is_err());
}

#[test]
fn test_user_bounds_is_sortable() {
assert!(UserBoundsList::from_str("1").unwrap().is_sortable());

assert!(UserBoundsList::from_str("1,2").unwrap().is_sortable());
Expand All @@ -732,8 +791,6 @@ mod tests {

#[test]
fn test_vec_of_bounds_is_sorted() {
assert!(UserBoundsList::from_str("").unwrap().is_sorted());

assert!(UserBoundsList::from_str("1").unwrap().is_sorted());

assert!(UserBoundsList::from_str("1,2").unwrap().is_sorted());
Expand Down Expand Up @@ -763,7 +820,10 @@ mod tests {
#[test]
fn test_vec_of_bounds_can_unpack() {
assert_eq!(
UserBoundsList::from_str("1,:1,2:3,4:").unwrap().unpack(4).0,
UserBoundsList::from_str("1,:1,2:3,4:")
.unwrap()
.unpack(4)
.list,
vec![
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
Expand All @@ -774,7 +834,10 @@ mod tests {
);

assert_eq!(
UserBoundsList::from_str("a{1}b{2}c").unwrap().unpack(4).0,
UserBoundsList::from_str("a{1}b{2}c")
.unwrap()
.unpack(4)
.list,
vec![
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(2))),
Expand Down
4 changes: 2 additions & 2 deletions src/cut_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use std::io::{Read, Write};

use crate::bounds::BoundOrFiller;
use crate::bounds::{BoundOrFiller, UserBoundsTrait};
use crate::options::Opt;
use crate::read_utils::read_bytes_to_end;

Expand All @@ -10,7 +10,7 @@ fn cut_bytes<W: Write>(data: &[u8], opt: &Opt, stdout: &mut W) -> Result<()> {
return Ok(());
}

opt.bounds.0.iter().try_for_each(|bof| -> Result<()> {
opt.bounds.iter().try_for_each(|bof| -> Result<()> {
let output = match bof {
BoundOrFiller::Bound(b) => {
let r = b.try_into_range(data.len())?;
Expand Down
Loading
Loading