Skip to content

Commit

Permalink
use CompleteStr in the ini_str benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Feb 12, 2018
1 parent 81f19c1 commit 6381995
Showing 1 changed file with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions benches/ini_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate test;
extern crate nom;

use nom::IResult;
use nom::types::CompleteStr;

use std::collections::HashMap;

Expand All @@ -28,26 +29,26 @@ fn is_line_ending_or_comment(chr: char) -> bool {
chr == ';' || chr == '\n'
}

named!(alphanumeric<&str,&str>, take_while_s!(is_alphanumeric));
named!(not_line_ending<&str,&str>, is_not_s!("\r\n"));
named!(space<&str,&str>, take_while_s!(is_space));
named!(space_or_line_ending<&str,&str>, is_a_s!(" \r\n"));
named!(alphanumeric<CompleteStr,CompleteStr>, take_while_s!(is_alphanumeric));
named!(not_line_ending<CompleteStr,CompleteStr>, is_not_s!("\r\n"));
named!(space<CompleteStr,CompleteStr>, take_while_s!(is_space));
named!(space_or_line_ending<CompleteStr,CompleteStr>, is_a_s!(" \r\n"));

fn right_bracket(c: char) -> bool {
c == ']'
}

named!(category <&str, &str>,
named!(category <CompleteStr, &str>,
do_parse!(
tag_s!("[") >>
name: take_till_s!(right_bracket) >>
tag_s!("]") >>
opt!(space_or_line_ending) >>
(name)
(name.0)
)
);

named!(key_value <&str,(&str,&str)>,
named!(key_value <CompleteStr,(&str,&str)>,
do_parse!(
key: alphanumeric >>
opt!(space) >>
Expand All @@ -57,27 +58,27 @@ named!(key_value <&str,(&str,&str)>,
opt!(space) >>
opt!(pair!(tag_s!(";"), not_line_ending)) >>
opt!(space_or_line_ending) >>
(key, val)
(key.0, val.0)
)
);

named!(keys_and_values_aggregator<&str, Vec<(&str,&str)> >, many0!(key_value));
named!(keys_and_values_aggregator<CompleteStr, Vec<(&str, &str)> >, many0!(key_value));

fn keys_and_values(input: &str) -> IResult<&str, HashMap<&str, &str>> {
fn keys_and_values(input: CompleteStr) -> IResult<CompleteStr, HashMap<&str, &str>> {
match keys_and_values_aggregator(input) {
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
Err(e) => Err(e),
}
}


named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>,
named!(category_and_keys<CompleteStr,(&str,HashMap<&str,&str>)>,
pair!(category, keys_and_values)
);

named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));
named!(categories_aggregator<CompleteStr, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));

fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str>>> {
fn categories(input: CompleteStr) -> IResult<CompleteStr, HashMap<&str, HashMap<&str, &str>>> {
match categories_aggregator(input) {
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
Err(e) => Err(e),
Expand All @@ -87,18 +88,22 @@ fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str>>>

#[test]
fn parse_category_test() {
let ini_file = "[category]
let ini_file = CompleteStr(
"[category]
parameter=value
key = value2";
key = value2",
);

let ini_without_category = "parameter=value
key = value2";
let ini_without_category = CompleteStr(
"parameter=value
key = value2",
);

let res = category(ini_file);
println!("{:?}", res);
match res {
Ok((i, o)) => println!("i: {} | o: {:?}", i, o),
Ok((i, o)) => println!("i: {} | o: {:?}", i.0, o),
_ => println!("error"),
}

Expand All @@ -107,15 +112,17 @@ key = value2";

#[test]
fn parse_key_value_test() {
let ini_file = "parameter=value
key = value2";
let ini_file = CompleteStr(
"parameter=value
key = value2",
);

let ini_without_key_value = "key = value2";
let ini_without_key_value = CompleteStr("key = value2");

let res = key_value(ini_file);
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i.0, o1, o2),
_ => println!("error"),
}

Expand All @@ -124,15 +131,17 @@ key = value2";

#[test]
fn parse_key_value_with_space_test() {
let ini_file = "parameter = value
key = value2";
let ini_file = CompleteStr(
"parameter = value
key = value2",
);

let ini_without_key_value = "key = value2";
let ini_without_key_value = CompleteStr("key = value2");

let res = key_value(ini_file);
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i.0, o1, o2),
_ => println!("error"),
}

Expand All @@ -141,15 +150,17 @@ key = value2";

#[test]
fn parse_key_value_with_comment_test() {
let ini_file = "parameter=value;abc
key = value2";
let ini_file = CompleteStr(
"parameter=value;abc
key = value2",
);

let ini_without_key_value = "key = value2";
let ini_without_key_value = CompleteStr("key = value2");

let res = key_value(ini_file);
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i.0, o1, o2),
_ => println!("error"),
}

Expand All @@ -158,18 +169,20 @@ key = value2";

#[test]
fn parse_multiple_keys_and_values_test() {
let ini_file = "parameter=value;abc
let ini_file = CompleteStr(
"parameter=value;abc
key = value2
[category]";
[category]",
);

let ini_without_key_value = "[category]";
let ini_without_key_value = CompleteStr("[category]");

let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
Ok((i, ref o)) => println!("i: {} | o: {:?}", i.0, o),
_ => println!("error"),
}

Expand All @@ -182,19 +195,21 @@ key = value2
#[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 = "[abcd]
let ini_file = CompleteStr(
"[abcd]
parameter=value;abc
key = value2
[category]";
[category]",
);

let ini_after_parser = "[category]";
let ini_after_parser = CompleteStr("[category]");

let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
Ok((i, ref o)) => println!("i: {} | o: {:?}", i.0, o),
_ => println!("error"),
}

Expand All @@ -206,7 +221,8 @@ key = value2

#[test]
fn parse_multiple_categories_test() {
let ini_file = "[abcd]
let ini_file = CompleteStr(
"[abcd]
parameter=value;abc
Expand All @@ -215,12 +231,13 @@ key = value2
[category]
parameter3=value3
key4 = value4
";
",
);

let res = categories(ini_file);
//println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
Ok((i, ref o)) => println!("i: {} | o: {:?}", i.0, o),
_ => println!("error"),
}

Expand All @@ -233,12 +250,12 @@ 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, Ok(("", expected_h)));
assert_eq!(res, Ok((CompleteStr(""), expected_h)));
}

#[bench]
fn bench_ini_str(b: &mut test::Bencher) {
let str = "[owner]
let s = "[owner]
name=John Doe
organization=Acme Widgets Inc.
Expand All @@ -248,6 +265,7 @@ port=143
file=payroll.dat
";

b.iter(|| categories(str).unwrap());
b.bytes = str.len() as u64;
b.iter(|| categories(CompleteStr(s)).unwrap());
b.bytes = s.len() as u64;
}

0 comments on commit 6381995

Please sign in to comment.