Created
December 14, 2021 01:25
-
-
Save jtt9340/9357a7150f4695095718c3c1c17bb5a9 to your computer and use it in GitHub Desktop.
Convenient text manipulation functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile with: rustc -L dependency=fastrand/target/release/deps --extern fastrand=fastrand/target/release/deps/libfastrand-0ce9eb3dc1d79119.rlib text.rs | |
extern crate fastrand; | |
use std::{env, io}; | |
/// Given a string slice, return a string where each character in the given slice has a 60% chance | |
/// of being upper case, regardless of the case of the corresponding character in the original | |
/// string. Otherwise, the character is lower case. | |
/// | |
/// # Example | |
/// | |
/// ``` | |
/// let phrase = "i don't know how to describe this function"; | |
/// | |
/// println!("{}", random_case(phrase)); | |
/// // Possible output: | |
/// // i don'T kNOW hOW To DeScRIbe THis FuNCTIOn | |
/// ``` | |
pub fn random_case(text: &str) -> String { | |
let text = text.to_lowercase(); | |
let mut random_case_string = String::with_capacity(text.len()); | |
for chr in text.chars() { | |
if fastrand::u8(0..=10) > 6 { | |
random_case_string.extend(chr.to_uppercase()); | |
} else { | |
random_case_string.push(chr); | |
} | |
} | |
random_case_string | |
/* | |
* Here is an alternate, more functional implementation of this function, but I am not sure how | |
* efficient it is with regard to memory allocations. | |
* | |
* text.chars().fold(random_case_string, |acc, c| { | |
* let c = if fastrand::u8(0..=10) > 6 { | |
* c.to_uppercase().to_string() | |
* } else { | |
* c.to_string() | |
* }; | |
* | |
* acc + &c | |
* }) | |
*/ | |
} | |
/// Given a string slice, return a string where each character is upper case and separate by a | |
/// space. | |
/// | |
/// # Example | |
/// | |
/// ``` | |
/// let phrase = "i don't know how to describe this function"; | |
/// | |
/// assert_eq!(shout(phrase), "I D O N ' T K N O W H O W T O D E S C R I B E T H I S F U N C T I O N".to_string()); | |
/// ``` | |
pub fn shout(text: &str) -> String { | |
let text = text.to_uppercase(); | |
let mut shouted = String::with_capacity(text.len() * 2); | |
for chr in text.chars() { | |
shouted.push(chr); | |
if !chr.is_whitespace() { | |
shouted.push(' '); | |
} | |
} | |
shouted | |
/* | |
* Here is an alternate, more functional implementation of this funciton, but I am not sure how | |
* efficient it is with regard to memory allocation. | |
* | |
* text.chars().fold(shouted, |acc, c| { | |
* if c.is_whitespace() { | |
* acc + &c.to_string() | |
* } else { | |
* acc + &format!("{} ", c) | |
* } | |
* }) | |
*/ | |
} | |
fn main() { | |
let mut args = env::args(); | |
let _binary_name = args | |
.next() | |
.expect("Program name not passed in command line arguments"); | |
let mut input = String::new(); | |
for arg in args { | |
input.push_str(&arg); | |
input.push(' '); | |
} | |
if input.is_empty() { | |
print!("Enter a phrase: "); | |
if io::stdin().read_line(&mut input).is_err() { | |
input.push_str("Could not read from standard input"); | |
} | |
} | |
println!("{}\n{}", random_case(&input), shout(&input)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment