-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#3 Implement string fakeit #27
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad7a81d
Implementes fakeit::string
abbott37 115ffe3
Updates lib.rs to be alphabetical
abbott37 29d4690
update to readme
abbott37 2db4a66
fixed typos in readme
abbott37 451f2b0
moved helpers.rs to data::string.rs
abbott37 fb010b1
added string to data::mod.rs
abbott37 c21921d
Ran cargo fmt
abbott37 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ pub mod lorem; | |
pub mod payment; | ||
pub mod person; | ||
pub mod status_code; | ||
pub mod string; | ||
pub mod vehicle; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[allow(dead_code)] | ||
pub static LOWER_CASE: &'static [&str] = &[ | ||
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", | ||
"t", "u", "v", "w", "x", "y", "z", | ||
]; | ||
|
||
#[allow(dead_code)] | ||
pub static UPPER_CASE: &'static [&str] = &[ | ||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", | ||
"T", "U", "V", "W", "X", "Y", "Z", | ||
]; | ||
|
||
#[allow(dead_code)] | ||
pub static LOWER_VOWELS: &'static [&str] = &["a", "e", "i", "o", "u"]; | ||
|
||
#[allow(dead_code)] | ||
pub static UPPER_VOWELS: &'static [&str] = &["A", "E", "I", "O", "U"]; | ||
|
||
#[allow(dead_code)] | ||
pub static DIGITS: &'static [&str] = &["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
use rand::seq::SliceRandom; | ||
use rand::Rng; | ||
|
||
use crate::data::string; | ||
use crate::misc; | ||
|
||
// letter will generate a single random lower case ASCII letter | ||
pub fn letter() -> String { | ||
rand_letter() | ||
} | ||
|
||
fn rand_letter() -> String { | ||
String::from(misc::random_data(string::LOWER_CASE)) | ||
} | ||
|
||
// letter_n will generate a random ASCII string with length N. Note that this function returns a string with a length of 1 when 0 is passed. | ||
pub fn letter_n(n: i32) -> String { | ||
rand_letter_n(n) | ||
} | ||
|
||
fn rand_letter_n(n: i32) -> String { | ||
let mut values: String = String::from(""); | ||
let counter: i32 = if n == 0 { 1 } else { n }; | ||
|
||
for _ in 0..counter { | ||
values = values + &*rand_letter(); | ||
} | ||
String::from(values) | ||
} | ||
|
||
// vowel will generate a single random lower case vowel | ||
pub fn vowel() -> String { | ||
rand_vowel() | ||
} | ||
|
||
fn rand_vowel() -> String { | ||
String::from(misc::random_data(string::LOWER_VOWELS)) | ||
} | ||
|
||
// Digit will generate a single ASCII digit | ||
pub fn digit() -> String { | ||
rand_digit() | ||
} | ||
|
||
fn rand_digit() -> String { | ||
String::from(misc::random_data(string::DIGITS)) | ||
} | ||
|
||
// digit_n will generate a random string of length N consists of ASCII digits. Note that the string generated can start with 0 and this function returns a string with a length of 1 when 0 is passed. | ||
pub fn digit_n(n: i32) -> String { | ||
rand_digit_n(n) | ||
} | ||
|
||
fn rand_digit_n(n: i32) -> String { | ||
let mut values: String = String::from(""); | ||
let counter: i32 = if n == 0 { 1 } else { n }; | ||
|
||
for _ in 0..counter { | ||
values = values + &*rand_digit(); | ||
} | ||
String::from(values) | ||
} | ||
|
||
// numerify will replace # with random numerical values | ||
pub fn numerify(initial_string: String) -> String { | ||
misc::replace_with_numbers(initial_string) | ||
} | ||
|
||
// lexify will replace ? with random generated letters | ||
pub fn lexify(initial_string: String) -> String { | ||
misc::replace_with_letter(initial_string) | ||
} | ||
|
||
// shuffle_strings will randomize a slice of strings | ||
pub fn shuffle_strings(s: &mut [String]) { | ||
let mut rng = rand::thread_rng(); | ||
s.shuffle(&mut rng) | ||
} | ||
|
||
// random_string will take in a slice of string and return a randomly selected value | ||
pub fn random_string(s: &mut [String]) -> String { | ||
return_random_string(s) | ||
} | ||
|
||
fn return_random_string(s: &mut [String]) -> String { | ||
let mut rng = rand::thread_rng(); | ||
let rand_index = rng.gen_range(0, s.len() - 1); | ||
s[rand_index].clone() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::string; | ||
use crate::testify::exec_mes; | ||
|
||
#[test] | ||
fn letter() { | ||
exec_mes("string::letter", || string::letter()); | ||
} | ||
|
||
#[test] | ||
fn letter_n() { | ||
exec_mes("string::letter_n", || string::letter_n(7)); | ||
exec_mes("string::letter_n", || string::letter_n(3)); | ||
} | ||
|
||
#[test] | ||
fn vowel() { | ||
exec_mes("string::vowel", || string::vowel()); | ||
} | ||
|
||
#[test] | ||
fn digit() { | ||
exec_mes("string::digit", || string::digit()); | ||
} | ||
|
||
#[test] | ||
fn digit_n() { | ||
exec_mes("string::digit_n", || string::digit_n(5)); | ||
exec_mes("string::digit_n", || string::digit_n(11)); | ||
} | ||
|
||
#[test] | ||
fn numerify() { | ||
exec_mes("string::numerify", || { | ||
string::numerify("H#LL# W#RLD!!".to_owned()) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn lexify() { | ||
exec_mes("string::lexify", || { | ||
string::lexify("H?LL? W?RLD!!".to_owned()) | ||
}) | ||
} | ||
|
||
#[test] | ||
fn shuffle_strings() { | ||
let stable_strings: &mut [String] = &mut [ | ||
"first".to_string(), | ||
"second".to_string(), | ||
"third".to_string(), | ||
"fourth".to_string(), | ||
"fifth".to_string(), | ||
]; | ||
let test_strings: &mut [String] = &mut [ | ||
"first".to_string(), | ||
"second".to_string(), | ||
"third".to_string(), | ||
"fourth".to_string(), | ||
"fifth".to_string(), | ||
]; | ||
string::shuffle_strings(test_strings); | ||
println!("{:?}", test_strings); | ||
assert_ne!(stable_strings, test_strings) | ||
} | ||
|
||
#[test] | ||
fn random_string() { | ||
let stable_strings: &mut [String] = &mut [ | ||
"first".to_string(), | ||
"second".to_string(), | ||
"third".to_string(), | ||
"fourth".to_string(), | ||
"fifth".to_string(), | ||
]; | ||
let return_string: String = string::random_string(stable_strings); | ||
println!("return string: {:?}", return_string); | ||
assert!(stable_strings.contains(&return_string)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't use rand package because the whole package using simplerand. It is because the rand package is an overkill for this library. Also if you are using rand package and I add the custom seed value functionality to the code-base (one of the requested feature), it won't work for string functions, because it will use the rand package's seed value.
Please use simplerand, or write a wrapper/helper function for that in the misc.rs.