From beebff50eb4f447f41162299fde81cb6fa9b336d Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Sat, 24 Feb 2018 21:04:12 +0100 Subject: Properly check if the input is empty --- src/framework/standard/args.rs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'src/framework') diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 87cc0a5..1a819b6 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -159,7 +159,7 @@ impl Args { /// ``` pub fn single(&mut self) -> Result where T::Err: StdError { - if self.message.is_empty() { + if self.is_empty() { return Err(Error::Eos); } @@ -186,7 +186,7 @@ impl Args { /// [`single`]: #method.single pub fn single_n(&self) -> Result where T::Err: StdError { - if self.message.is_empty() { + if self.is_empty() { return Err(Error::Eos); } @@ -221,7 +221,7 @@ impl Args { if let Some(len) = self.len { len - } else if self.message.is_empty() { + } else if self.is_empty() { 0 } else { let mut words: Box> = Box::new(Some(&self.message[..]).into_iter()); @@ -263,7 +263,7 @@ impl Args { /// assert_eq!(args.len_quoted(), 2); // `2` because `["42", "69"]` /// ``` pub fn len_quoted(&mut self) -> usize { - if self.message.is_empty() { + if self.is_empty() { 0 } else if let Some(len_quoted) = self.len_quoted { len_quoted @@ -291,6 +291,10 @@ impl Args { /// assert_eq!(args.full(), "69"); /// ``` pub fn skip(&mut self) -> Option { + if self.is_empty() { + return None; + } + if let Some(ref mut val) = self.len { if 1 <= *val { @@ -315,6 +319,10 @@ impl Args { /// /// [`skip`]: #method.skip pub fn skip_for(&mut self, i: u32) -> Option> { + if self.is_empty() { + return None; + } + let mut vec = Vec::with_capacity(i as usize); for _ in 0..i { @@ -349,6 +357,10 @@ impl Args { /// [`single`]: #method.single pub fn single_quoted(&mut self) -> Result where T::Err: StdError { + if self.is_empty() { + return Err(Error::Eos); + } + if let Some(ref mut val) = self.len_quoted { *val -= 1 }; @@ -372,6 +384,10 @@ impl Args { /// [`single_quoted`]: #method.single_quoted pub fn single_quoted_n(&self) -> Result where T::Err: StdError { + if self.is_empty() { + return Err(Error::Eos); + } + parse_quotes::(&mut self.message.clone(), &self.delimiters) } @@ -390,6 +406,10 @@ impl Args { /// [`multiple`]: #method.multiple pub fn multiple_quoted(mut self) -> Result, T::Err> where T::Err: StdError { + if self.is_empty() { + return Err(Error::Eos); + } + IterQuoted::::new(&mut self).collect() } @@ -425,6 +445,10 @@ impl Args { /// ``` pub fn multiple(mut self) -> Result, T::Err> where T::Err: StdError { + if self.is_empty() { + return Err(Error::Eos); + } + Iter::::new(&mut self).collect() } @@ -441,7 +465,7 @@ impl Args { /// assert!(args.is_empty()); /// ``` pub fn iter(&mut self) -> Iter - where T::Err: StdError { + where T::Err: StdError { Iter::new(self) } @@ -462,7 +486,7 @@ impl Args { /// ``` pub fn find(&mut self) -> Result where T::Err: StdError { - if self.message.is_empty() { + if self.is_empty() { return Err(Error::Eos); } @@ -521,7 +545,7 @@ impl Args { /// ``` pub fn find_n(&mut self) -> Result where T::Err: StdError { - if self.message.is_empty() { + if self.is_empty() { return Err(Error::Eos); } -- cgit v1.2.3