Skip to content

Commit

Permalink
Filter the entries directly without collecting twice
Browse files Browse the repository at this point in the history
Signed-off-by: Constantin Nickel <constantin.nickel@gmail.com>
  • Loading branch information
nickelc committed Aug 13, 2021
1 parent 74cb7cf commit 335f752
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ license = "MIT"
name="tar"

[dependencies]
nom = { default-features = false, features = ["alloc"], git = "https://github.com/Geal/nom.git", rev = "ef5e0bbc" }
nom = { default-features = false, git = "https://github.com/Geal/nom.git", rev = "ef5e0bbc" }

[badges]
travis-ci = { repository = "Keruspe/tar-parser.rs" }
15 changes: 8 additions & 7 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nom::bytes::complete::{tag, take, take_until};
use nom::character::complete::{oct_digit0, space0};
use nom::combinator::{all_consuming, map, map_parser, map_res, opt};
use nom::error::ErrorKind;
use nom::multi::many0;
use nom::multi::fold_many0;
use nom::sequence::{pair, terminated};

/*
Expand Down Expand Up @@ -375,13 +375,14 @@ fn parse_entry(i: &[u8]) -> IResult<&[u8], TarEntry<'_>> {
* Tar archive parsing
*/

fn filter_entries(entries: Vec<TarEntry<'_>>) -> Vec<TarEntry<'_>> {
/* Filter out empty entries */
entries.into_iter().filter(|e| !e.header.name.is_empty()).collect()
}

pub fn parse_tar(i: &[u8]) -> IResult<&[u8], Vec<TarEntry<'_>>> {
all_consuming(map(many0(parse_entry), filter_entries))(i)
let entries = fold_many0(parse_entry, Vec::new, |mut vec, e| {
if !e.header.name.is_empty() {
vec.push(e)
}
vec
});
all_consuming(entries)(i)
}

/*
Expand Down

0 comments on commit 335f752

Please sign in to comment.