Skip to content

Commit

Permalink
Refactor Filler to use bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
riquito committed Mar 2, 2024
1 parent b4dde0c commit b43fdb9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
22 changes: 12 additions & 10 deletions src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum BoundsType {
#[derive(Clone, Debug, PartialEq)]
pub enum BoundOrFiller {
Bound(UserBounds),
Filler(String),
Filler(Vec<u8>),
}

/**
Expand Down Expand Up @@ -57,7 +57,8 @@ pub fn parse_bounds_list(s: &str) -> Result<Vec<BoundOrFiller>> {
.replace("{{", "{")
.replace("}}", "}")
.replace("\\n", "\n")
.replace("\\t", "\t"),
.replace("\\t", "\t")
.into_bytes(),
));
}

Expand All @@ -83,7 +84,8 @@ pub fn parse_bounds_list(s: &str) -> Result<Vec<BoundOrFiller>> {
.replace("{{", "{")
.replace("}}", "}")
.replace("\\n", "\n")
.replace("\\t", "\t"),
.replace("\\t", "\t")
.into_bytes(),
));
}

Expand Down Expand Up @@ -877,29 +879,29 @@ mod tests {
assert_eq!(
parse_bounds_list("hello {1,2} {{world}}").unwrap(),
vec![
BoundOrFiller::Filler(String::from("hello ")),
BoundOrFiller::Filler("hello ".into()),
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(2))),
BoundOrFiller::Filler(String::from(" {world}")),
BoundOrFiller::Filler(" {world}".into()),
],
);

assert_eq!(
parse_bounds_list("{1}😎{2}").unwrap(),
vec![
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
BoundOrFiller::Filler(String::from("😎")),
BoundOrFiller::Filler("😎".into()),
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(2)))
],
);

assert_eq!(
parse_bounds_list("\\n\\t{{}}{1,2}\\n\\t{{}}").unwrap(),
vec![
BoundOrFiller::Filler(String::from("\n\t{}")),
BoundOrFiller::Filler("\n\t{}".into()),
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(2))),
BoundOrFiller::Filler(String::from("\n\t{}")),
BoundOrFiller::Filler("\n\t{}".into()),
],
);
}
Expand Down Expand Up @@ -1051,10 +1053,10 @@ mod tests {
assert_eq!(
UserBoundsList::from_str("a{1:2}b").unwrap().unpack(4).list,
vec![
BoundOrFiller::Filler(String::from("a")),
BoundOrFiller::Filler("a".into()),
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1))),
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(2))),
BoundOrFiller::Filler(String::from("b")),
BoundOrFiller::Filler("b".into()),
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cut_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn cut_bytes<W: Write>(data: &[u8], opt: &Opt, stdout: &mut W) -> Result<()> {
let r = b.try_into_range(data.len())?;
&data[r.start..r.end]
}
BoundOrFiller::Filler(f) => f.as_bytes(),
BoundOrFiller::Filler(f) => f,
};

stdout.write_all(output)?;
Expand Down
2 changes: 1 addition & 1 deletion src/cut_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn cut_lines_forward_only<A: BufRead, B: Write>(

let b = match bof {
BoundOrFiller::Filler(f) => {
stdout.write_all(f.as_bytes())?;
stdout.write_all(f)?;
bounds_idx += 1;

if opt.join && bounds_idx != opt.bounds.len() {
Expand Down
4 changes: 2 additions & 2 deletions src/fast_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn cut_str_fast_lane<W: Write>(
bounds.iter().try_for_each(|bof| -> Result<()> {
match bof {
BoundOrFiller::Filler(f) => {
stdout.write_all(f.as_bytes())?;
stdout.write_all(f)?;
}
BoundOrFiller::Bound(b) => {
output_parts(buffer, b, fields, stdout, opt)?;
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<'a> TryFrom<&'a Opt> for FastOpt<'a> {
);
}

let delimiter = value.delimiter.as_bytes().first().unwrap().to_owned();
let delimiter: u8 = *value.delimiter.as_bytes().first().unwrap();
Ok(FastOpt {
delimiter,
join: value.join,
Expand Down

0 comments on commit b43fdb9

Please sign in to comment.