Skip to content

Commit

Permalink
Be able to trim whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Sep 19, 2018
1 parent 9f834b2 commit 3b050f4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/framework/standard/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b050f4

Please sign in to comment.