Skip to content

Commit

Permalink
replace the macros in the ini benches with functions
Browse files Browse the repository at this point in the history
- ini_complete is obsolete since nom 5
- removed the copied tests from tests/ini*
  • Loading branch information
nickelc authored and Geal committed Aug 5, 2021
1 parent ff95cff commit c866032
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 688 deletions.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ name = "ini"
path = "benches/ini.rs"
harness = false

[[bench]]
name = "ini_complete"
path = "benches/ini_complete.rs"
harness = false

[[bench]]
name = "ini_str"
path = "benches/ini_str.rs"
Expand Down
228 changes: 24 additions & 204 deletions benches/ini.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[macro_use]
extern crate nom;
extern crate criterion;
extern crate jemallocator;

Expand All @@ -13,8 +11,9 @@ use nom::{
character::complete::{
alphanumeric1 as alphanumeric, char, multispace1 as multispace, space1 as space,
},
combinator::map_res,
sequence::delimited,
combinator::{map, map_res, opt},
multi::many0,
sequence::{delimited, pair, separated_pair, terminated, tuple},
IResult,
};
use std::collections::HashMap;
Expand All @@ -27,208 +26,27 @@ fn category(i: &[u8]) -> IResult<&[u8], &str> {
)(i)
}

named!(key_value <&[u8],(&str,&str)>,
do_parse!(
key: map_res!(alphanumeric, str::from_utf8)
>> opt!(space)
>> char!('=')
>> opt!(space)
>> val: map_res!(
take_while!(call!(|c| c != '\n' as u8 && c != ';' as u8)),
str::from_utf8
)
>> opt!(pair!(char!(';'), take_while!(call!(|c| c != '\n' as u8))))
>> (key, val)
)
);

named!(keys_and_values<&[u8], HashMap<&str, &str> >,
map!(
many0!(terminated!(key_value, opt!(multispace))),
|vec: Vec<_>| vec.into_iter().collect()
)
);

named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>,
do_parse!(
category: category >>
opt!(multispace) >>
keys: keys_and_values >>
(category, keys)
)
);

named!(categories<&[u8], HashMap<&str, HashMap<&str,&str> > >,
map!(
many0!(
separated_pair!(
category,
opt!(multispace),
map!(
many0!(terminated!(key_value, opt!(multispace))),
|vec: Vec<_>| vec.into_iter().collect()
)
)
),
|vec: Vec<_>| vec.into_iter().collect()
)
);

/*
#[test]
fn parse_category_test() {
let ini_file = &b"[category]
parameter=value
key = value2"[..];
let ini_without_category = &b"parameter=value
key = value2"[..];
let res = category(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_category, "category"));
fn key_value(i: &[u8]) -> IResult<&[u8], (&str, &str)> {
let (i, key) = map_res(alphanumeric, str::from_utf8)(i)?;
let (i, _) = tuple((opt(space), char('='), opt(space)))(i)?;
let (i, val) = map_res(take_while(|c| c != b'\n' && c != b';'), str::from_utf8)(i)?;
let (i, _) = opt(pair(char(';'), take_while(|c| c != b'\n')))(i)?;
Ok((i, (key, val)))
}

#[test]
fn parse_key_value_test() {
let ini_file = &b"parameter=value
key = value2"[..];
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_space_test() {
let ini_file = &b"parameter = value
key = value2"[..];
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_comment_test() {
let ini_file = &b"parameter=value;abc
key = value2"[..];
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_multiple_keys_and_values_test() {
let ini_file = &b"parameter=value;abc
key = value2
[category]"[..];
let ini_without_key_value = &b"[category]"[..];
let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected: HashMap<&str, &str> = HashMap::new();
expected.insert("parameter", "value");
expected.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_without_key_value, expected));
}
#[test]
fn parse_category_then_multiple_keys_and_values_test() {
//FIXME: there can be an empty line or a comment line after a category
let ini_file = &b"[abcd]
parameter=value;abc
key = value2
[category]"[..];
let ini_after_parser = &b"[category]"[..];
let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_h: HashMap<&str, &str> = HashMap::new();
expected_h.insert("parameter", "value");
expected_h.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h)));
}
#[test]
fn parse_multiple_categories_test() {
let ini_file = &b"[abcd]
parameter=value;abc
key = value2
[category]
parameter3=value3
key4 = value4
\0"[..];
let ini_after_parser = &b"\0"[..];
let res = categories(ini_file);
//println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_1: HashMap<&str, &str> = HashMap::new();
expected_1.insert("parameter", "value");
expected_1.insert("key", "value2");
let mut expected_2: HashMap<&str, &str> = HashMap::new();
expected_2.insert("parameter3", "value3");
expected_2.insert("key4", "value4");
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done(ini_after_parser, expected_h));
fn categories(i: &[u8]) -> IResult<&[u8], HashMap<&str, HashMap<&str, &str>>> {
map(
many0(separated_pair(
category,
opt(multispace),
map(
many0(terminated(key_value, opt(multispace))),
|vec: Vec<_>| vec.into_iter().collect(),
),
)),
|vec: Vec<_>| vec.into_iter().collect(),
)(i)
}
*/

fn bench_ini(c: &mut Criterion) {
let str = "[owner]
Expand Down Expand Up @@ -256,7 +74,9 @@ port=143
file=payroll.dat
\0";

named!(acc<Vec<(&str, &str)>>, many0!(key_value));
fn acc(i: &[u8]) -> IResult<&[u8], Vec<(&str, &str)>> {
many0(key_value)(i)
}

c.bench(
"bench ini keys and values",
Expand Down
Loading

0 comments on commit c866032

Please sign in to comment.