summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorRyan Geary <rtgnj42@gmail.com>2020-02-24 23:59:51 -0500
committerRyan Geary <rtgnj42@gmail.com>2020-03-10 00:03:14 -0400
commit389c29822b2af5f4a828734eb0a2d70b4f82a900 (patch)
tree7983809185cf846c2182f0eac89654359860d91a /src/config.rs
parentc729ad3f004eb2313a4dfa206b7103939abeaac4 (diff)
Improve performance and add profiling tooling
Add tags, todo, *.bench, *.svg and bench_output to .gitignore Add test/bench.sh script. bench.sh runs the `bench` command on each test/long*txt file with range 3:5 and saves the output to a file for comparing performance across file sizes. Inline printing in get_choice_slice Change BufWriter<..stdout..> to BufWriter<T> Add MockStdout for testing printing Add more reverse range tests Simplify word finding with a more uniform bounds check. Add Makefile for generating flamegraphs Redefine Choice struct as a start and end integer Improve algorithm for finding words to print Settle exclusivity at Config construction time Add tests for nonexistant field_seps Add regression test for preceding separator Use handle.write instead of write! macro for tremendous speed up
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs72
1 files changed, 30 insertions, 42 deletions
diff --git a/src/config.rs b/src/config.rs
index 8dfba5e..b435d02 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -38,7 +38,17 @@ pub struct Config {
}
impl Config {
- pub fn new(opt: Opt) -> Self {
+ pub fn new(mut opt: Opt) -> Self {
+ if opt.exclusive {
+ for mut choice in &mut opt.choice {
+ if choice.is_reverse_range() {
+ choice.start = choice.start - 1;
+ } else {
+ choice.end = choice.end - 1;
+ }
+ }
+ }
+
let separator = Regex::new(match &opt.field_separator {
Some(s) => s,
None => "[[:space:]]",
@@ -57,7 +67,7 @@ impl Config {
let cap = match re.captures_iter(src).next() {
Some(v) => v,
None => match src.parse() {
- Ok(x) => return Ok(Choice::Field(x)),
+ Ok(x) => return Ok(Choice::new(x, x)),
Err(_) => {
eprintln!("failed to parse choice argument: {}", src);
// Exit code of 2 means failed to parse choice argument
@@ -67,10 +77,10 @@ impl Config {
};
let start = if cap[1].is_empty() {
- None
+ usize::min_value()
} else {
match cap[1].parse() {
- Ok(x) => Some(x),
+ Ok(x) => x,
Err(_) => {
eprintln!("failed to parse range start: {}", &cap[1]);
process::exit(2);
@@ -79,10 +89,10 @@ impl Config {
};
let end = if cap[2].is_empty() {
- None
+ usize::max_value()
} else {
match cap[2].parse() {
- Ok(x) => Some(x),
+ Ok(x) => x,
Err(_) => {
eprintln!("failed to parse range end: {}", &cap[2]);
process::exit(2);
@@ -90,7 +100,7 @@ impl Config {
}
};
- return Ok(Choice::FieldRange((start, end)));
+ return Ok(Choice::new(start, end));
}
}
@@ -102,62 +112,41 @@ mod tests {
use super::*;
#[test]
- fn parse_single_choice() {
+ fn parse_single_choice_start() {
let result = Config::parse_choice("6").unwrap();
- assert_eq!(
- 6,
- match result {
- Choice::Field(x) => x,
- _ => panic!(),
- }
- )
+ assert_eq!(6, result.start)
+ }
+
+ #[test]
+ fn parse_single_choice_end() {
+ let result = Config::parse_choice("6").unwrap();
+ assert_eq!(6, result.end)
}
#[test]
fn parse_none_started_range() {
let result = Config::parse_choice(":5").unwrap();
- assert_eq!(
- (None, Some(5)),
- match result {
- Choice::FieldRange(x) => x,
- _ => panic!(),
- }
- )
+ assert_eq!((usize::min_value(), 5), (result.start, result.end))
}
#[test]
fn parse_none_terminated_range() {
let result = Config::parse_choice("5:").unwrap();
- assert_eq!(
- (Some(5), None),
- match result {
- Choice::FieldRange(x) => x,
- _ => panic!(),
- }
- )
+ assert_eq!((5, usize::max_value()), (result.start, result.end))
}
#[test]
fn parse_full_range() {
let result = Config::parse_choice("5:7").unwrap();
- assert_eq!(
- (Some(5), Some(7)),
- match result {
- Choice::FieldRange(x) => x,
- _ => panic!(),
- }
- )
+ assert_eq!((5, 7), (result.start, result.end))
}
#[test]
fn parse_beginning_to_end_range() {
let result = Config::parse_choice(":").unwrap();
assert_eq!(
- (None, None),
- match result {
- Choice::FieldRange(x) => x,
- _ => panic!(),
- }
+ (usize::min_value(), usize::max_value()),
+ (result.start, result.end)
)
}
@@ -173,5 +162,4 @@ mod tests {
//assert!(Config::parse_choice("d:i").is_err());
//}
}
-
}