Removes control characters from a string to make it suitable for printing.
(s: S)
| 421 | |
| 422 | /// Removes control characters from a string to make it suitable for printing. |
| 423 | pub fn remove_control_chars<S: Into<String>>(s: S) -> String { |
| 424 | let s = s.into(); |
| 425 | |
| 426 | // Handle the expected common case first. We use this function to strip control characters |
| 427 | // before printing them to the console, and thus we expect such input strings to rarely include |
| 428 | // control characters. |
| 429 | if !has_control_chars(&s) { |
| 430 | return s; |
| 431 | } |
| 432 | |
| 433 | let mut o = String::with_capacity(s.len()); |
| 434 | for ch in s.chars() { |
| 435 | if ch.is_control() { |
| 436 | o.push(' '); |
| 437 | } else { |
| 438 | o.push(ch); |
| 439 | } |
| 440 | } |
| 441 | o |
| 442 | } |
| 443 | |
| 444 | /// Gets the value of the environment variable `name` and interprets it as a `u16`. Returns |
| 445 | /// `None` if the variable is not set or if its contents are invalid. |