Skip to content

Commit

Permalink
Do some little optimizations on find(_n)
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed May 27, 2018
1 parent 2603063 commit 5ba521b
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/framework/standard/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,24 @@ impl Args {
where T::Err: StdError {
// TODO: Make this efficient

if self.delimiters.len() == 1 as usize {
if self.delimiters.len() == 1 {
match self.message.split(&self.delimiters[0]).position(|e| e.parse::<T>().is_ok()) {
Some(index) => {
let mut vec = self.message.split(self.delimiters[0].as_str()).map(|s| s.to_string()).collect::<Vec<_>>();
let mut ss = vec.remove(index);
let res = parse::<T>(&mut ss, &self.delimiters);
self.message = vec.join(&self.delimiters[0]);
fn do_stuff(msg: &str, delim: &str, index: usize) -> (String, String) {
let mut vec = msg.split(delim).collect::<Vec<_>>();

let found = vec.remove(index);
let new_state = vec.join(delim);

(found.to_string(), new_state)
}

let (mut s, msg) = do_stuff(&self.message, &self.delimiters[0], index);
let res = parse::<T>(&mut s, &self.delimiters);
self.message = msg;

if let Some(ref mut val) = self.len { if 1 <= *val { *val -= 1 } };

res
},
None => Err(Error::Eos),
Expand Down Expand Up @@ -605,9 +615,8 @@ impl Args {

match pos {
Some(index) => {
let mut vec = self.message.split(&self.delimiters[0]).map(|s| s.to_string()).collect::<Vec<_>>();
let mut ss = vec.remove(index);
parse::<T>(&mut ss, &self.delimiters)
let ss = self.message.split(&self.delimiters[0]).nth(index).unwrap();
parse::<T>(&mut ss.to_string(), &self.delimiters)
},
None => Err(Error::Eos),
}
Expand Down

0 comments on commit 5ba521b

Please sign in to comment.