diff options
| author | acdenisSK <[email protected]> | 2018-09-19 17:45:57 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2018-09-19 17:45:57 +0200 |
| commit | 3b050f49fbc2055c3940b4cacd05b3068856c7b5 (patch) | |
| tree | 121fce80f2ed909f360dcd0a3bbba647b8bd78a8 /src/framework | |
| parent | Compile without `cache`-feature (#393) (diff) | |
| download | serenity-3b050f49fbc2055c3940b4cacd05b3068856c7b5.tar.xz serenity-3b050f49fbc2055c3940b4cacd05b3068856c7b5.zip | |
Be able to trim whitespace
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/standard/args.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 2700bfb..036db7a 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -348,6 +348,50 @@ impl Args { self.args.get(self.offset).map(|t| t.lit.as_str()) } + /// Trims the current argument off leading and trailing whitespace. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new(" 42 ", &[]); + /// + /// args.trim(); + /// assert_eq!(args.current(), Some("42")); + /// ``` + pub fn trim(&mut self) { + if self.is_empty() { + return; + } + + self.args[self.offset].lit = self.args[self.offset].lit.trim().to_string(); + } + + /// Trims all of the arguments after the offset off leading and trailing whitespace. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new(" 42 , 84 ,\t168\t", &[",".to_string()]); + /// + /// args.trim_all(); + /// assert_eq!(args.single::<String>().unwrap(), "42"); + /// assert_eq!(args.single::<String>().unwrap(), "84"); + /// assert_eq!(args.single::<String>().unwrap(), "168"); + /// ``` + pub fn trim_all(&mut self) { + if self.is_empty() { + return; + } + + for token in &mut self.args[self.offset..] { + token.lit = token.lit.trim().to_string(); + } + } + /// Parses the current argument and advances. /// /// # Examples |