diff --git a/crates/rand/src/global/string.rs b/crates/rand/src/global/string.rs index 0a687a8..885f4ef 100644 --- a/crates/rand/src/global/string.rs +++ b/crates/rand/src/global/string.rs @@ -18,17 +18,19 @@ pub fn replace_chars_prob( input: String, replacement: String, prob: f32, + skip_whitespace: Option, secure: Option, ) -> String { if !prob.is_normal() || !prob.is_sign_positive() { return input; } + let skip_whitespace = skip_whitespace.unwrap_or(false); let mut rng = global(secure); let distro = Bernoulli::new((prob as f64 / 100.0).clamp(0.0, 1.0)).expect("invalid probability, wtf???"); let mut output = String::with_capacity(input.len() * replacement.len()); // Allocate for worst case scenario. input.chars().for_each(|c| { - if distro.sample(&mut rng) { + if (!skip_whitespace || !c.is_whitespace()) && distro.sample(&mut rng) { output.push_str(&replacement); } else { output.push(c); diff --git a/crates/rand/src/instance/string.rs b/crates/rand/src/instance/string.rs index 1b896a7..755c307 100644 --- a/crates/rand/src/instance/string.rs +++ b/crates/rand/src/instance/string.rs @@ -21,16 +21,18 @@ pub fn instnaced_replace_chars_prob( input: String, replacement: String, prob: f32, + skip_whitespace: Option, ) -> Option { if !prob.is_normal() || !prob.is_sign_positive() { return Some(input); } + let skip_whitespace = skip_whitespace.unwrap_or(false); INSTANCES.lock().get_mut(src).map(|rng| { let distro = Bernoulli::new((prob as f64 / 100.0).clamp(0.0, 1.0)) .expect("invalid probability, wtf???"); let mut output = String::with_capacity(input.len() * replacement.len()); // Allocate for worst case scenario. input.chars().for_each(|c| { - if distro.sample(rng) { + if (!skip_whitespace || !c.is_whitespace()) && distro.sample(rng) { output.push_str(&replacement); } else { output.push(c);