From 5ba521b4e8bb5ff10bd83303436cd454f27c93ab Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Sun, 27 May 2018 16:22:44 +0200 Subject: [PATCH] Do some little optimizations on `find(_n)` --- src/framework/standard/args.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 7148e3e6efd..701b7f2822e 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -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::().is_ok()) { Some(index) => { - let mut vec = self.message.split(self.delimiters[0].as_str()).map(|s| s.to_string()).collect::>(); - let mut ss = vec.remove(index); - let res = parse::(&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::>(); + + 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::(&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), @@ -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::>(); - let mut ss = vec.remove(index); - parse::(&mut ss, &self.delimiters) + let ss = self.message.split(&self.delimiters[0]).nth(index).unwrap(); + parse::(&mut ss.to_string(), &self.delimiters) }, None => Err(Error::Eos), }