1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
use crate::{PatternMatch, PatternReplace};
/// Provides methods to match with multiple patterns
/// expressed as arrays of tuples or simple strs (for pattern_match_many_ci and pattern_match_many_cs)
pub trait PatternMatchMany {
/// Matches all of the patterns in case-sensitivity flag
/// with an array of tuples (patterns, case_insensitive)
fn pattern_match_many(&self, patterns: &[&str], case_insensitive: bool) -> bool;
/// Matches all of the patterns in case-insensitive mode
/// with an array of str patterns
fn pattern_match_many_ci(&self, patterns: &[&str]) -> bool;
/// Matches all of the patterns in case-sensitive mode
/// with an array of str patterns
fn pattern_match_many_cs(&self, patterns: &[&str]) -> bool;
/// Matches all of the patterns with case-insensitive flag
/// e.g. ```(r#"a[ck]"#, true)``` matches "ac" or "ak" whether upper, lower or mixed case
/// with an array of tuples (pattern, replacement, case_insensitive)
fn pattern_match_many_mixed(&self, pattern_sets: &[(&str, bool)]) -> bool;
/// string matches all conditional patterns which may be positive / negative and case insensitive or not
fn pattern_match_many_conditional(&self, pattern_sets: &[(bool, &str, bool)]) -> bool;
/// Matches one or more of the patterns in case-sensitivity flag
/// with an array of tuples (patterns, case_insensitive)
fn pattern_match_any(&self, patterns: &[&str], case_insensitive: bool) -> bool;
/// Matches one or more of the patterns in case-insensitive mode
/// with an array of str patterns
fn pattern_match_any_ci(&self, patterns: &[&str]) -> bool;
/// Matches one or more of the patterns in case-sensitive mode
/// with an array of str patterns
fn pattern_match_any_cs(&self, patterns: &[&str]) -> bool;
/// Matches one or more of the patterns with case-insensitive boolean flag
fn pattern_match_any_mixed(&self, pattern_sets: &[(&str, bool)]) -> bool;
/// string matches one or more conditional patterns which may be positive / negative and case insensitive or not
fn pattern_match_any_conditional(&self, pattern_sets: &[(bool, &str, bool)]) -> bool;
}
/// Provides methods to replace with multiple patterns
/// expressed as arrays of tuples
pub trait PatternReplaceMany {
/// Replaces multiple sets of patterns with replacements in case-sensitive mode
/// with an array of tuples (pattern, replacement, case_insensitive)
fn pattern_replace_pairs(&self, replacement_sets: &[(&str, &str)]) -> Self where Self: Sized;
/// Replaces multiple sets of patterns with replacements in case-insensitive mode
/// with an array of tuples (pattern, replacement, case_insensitive)
fn pattern_replace_pairs_ci(&self, replacement_sets: &[(&str, &str)]) -> Self where Self: Sized;
/// Replaces multiple sets of patterns with replacements in case-sensitive mode
/// with an array of simple tuples (pattern, replacement)
fn pattern_replace_sets(&self, replacement_sets: &[(&str, &str, bool)]) -> Self where Self: Sized;
}
impl PatternMatchMany for str {
/// Matches all of the patterns in case-sensitivity flag
/// with an array of tuples (patterns, case_insensitive)
fn pattern_match_many(&self, patterns: &[&str], case_insensitive: bool) -> bool {
let mut num_matched = 0usize;
let num_patterns = patterns.len();
for pattern in patterns {
if self.pattern_match(pattern, case_insensitive) {
num_matched += 1;
}
}
num_matched == num_patterns
}
/// Matches all of the patterns in case-insensitive mode
/// with an array of str patterns
fn pattern_match_many_ci(&self, patterns: &[&str]) -> bool {
self.pattern_match_many(patterns, true)
}
/// Matches all of the patterns in case-sensitive mode
/// with an array of str patterns
fn pattern_match_many_cs(&self, patterns: &[&str]) -> bool {
self.pattern_match_many(patterns, false)
}
/// Matches all of the patterns with case-insensitive flag
/// e.g. ```(r#"a[ck]"#, true)``` "ac" or "ak" whether upper, lower or mixed case
/// with an array of tuples (pattern, replacement, case_insensitive)
fn pattern_match_many_mixed(&self, pattern_sets: &[(&str, bool)]) -> bool {
let mut num_matched = 0usize;
let num_patterns = pattern_sets.len();
for pair in pattern_sets {
let (pattern, case_insensitive) = *pair;
if self.pattern_match(pattern, case_insensitive) {
num_matched += 1;
}
}
num_matched == num_patterns
}
/// Matches all of the patterns with positivity condition and case-insensitive flag
/// e.g. ```(false, "a[ck]", true)``` does not contain "ac" or "ak" whether upper, lower or mixed case
/// with an array of tuples (positive, pattern, case_insensitive)
fn pattern_match_many_conditional(&self, pattern_sets: &[(bool, &str, bool)]) -> bool {
let mut num_matched = 0usize;
let num_patterns = pattern_sets.len();
for pattern_set in pattern_sets {
let (is_positive, pattern, case_insensitive) = *pattern_set;
let is_matched = self.pattern_match(pattern, case_insensitive);
if is_matched == is_positive {
num_matched += 1;
}
}
num_matched == num_patterns
}
/// Matches one or more of the patterns in case-sensitivity flag
/// with an array of tuples (patterns, case_insensitive)
fn pattern_match_any(&self, patterns: &[&str], case_insensitive: bool) -> bool {
for pattern in patterns {
if self.pattern_match(pattern, case_insensitive) {
return true;
}
}
false
}
/// Matches one or more of the patterns in case-insensitive mode
/// with an array of str patterns
fn pattern_match_any_ci(&self, patterns: &[&str]) -> bool {
self.pattern_match_any(patterns, true)
}
/// Matches one or more of the patterns in case-sensitive mode
/// with an array of str patterns
fn pattern_match_any_cs(&self, patterns: &[&str]) -> bool {
self.pattern_match_any(patterns, false)
}
/// Matches one or more of the patterns with case-insensitive flag
/// e.g. ```(r#"a[ck]"#, true)``` matches "ac" or "ak" whether upper, lower or mixed case
/// with an array of tuples (pattern, replacement, case_insensitive)
fn pattern_match_any_mixed(&self, pattern_sets: &[(&str, bool)]) -> bool {
for pair in pattern_sets {
let (pattern, case_insensitive) = *pair;
if self.pattern_match(pattern, case_insensitive) {
return true;
}
}
false
}
/// Matches one or more of the patterns with positivity condition and case-insensitive flag
fn pattern_match_any_conditional(&self, pattern_sets: &[(bool, &str, bool)]) -> bool {
for pattern_set in pattern_sets {
let (is_positive, pattern, case_insensitive) = *pattern_set;
let is_matched = self.pattern_match(pattern, case_insensitive);
if is_matched == is_positive {
return true;
}
}
false
}
}
impl PatternReplaceMany for String {
/// Replaces multiple sets of patterns with replacements and boolean case sensitivity
/// with an array of tuples (pattern, replacement, case_insensitive)
fn pattern_replace_sets(&self, replacement_sets: &[(&str, &str, bool)]) -> String {
let mut return_string = self.clone();
for replacement_set in replacement_sets {
let (pattern, replacement, case_insensitive) = *replacement_set;
if let Ok(new_string) = return_string.pattern_replace_result(pattern, replacement, case_insensitive) {
return_string = new_string;
}
}
return_string
}
/// Replaces multiple sets of patterns with replacements in case-sensitive mode
/// with an array of simple tuples (pattern, replacement)
fn pattern_replace_pairs(&self, replacement_pairs: &[(&str, &str)]) -> String {
let mut return_string = self.clone();
for replacement_pair in replacement_pairs {
let (pattern, replacement) = *replacement_pair;
if let Ok(new_string) = return_string.pattern_replace_result(pattern, replacement, false) {
return_string = new_string;
}
}
return_string
}
/// Replaces multiple sets of patterns with replacements in case-insensitive mode
/// with an array of simple tuples (pattern, replacement)
fn pattern_replace_pairs_ci(&self, replacement_pairs: &[(&str, &str)]) -> String {
let mut return_string = self.clone();
for replacement_pair in replacement_pairs {
let (pattern, replacement) = *replacement_pair;
if let Ok(new_string) = return_string.pattern_replace_result(pattern, replacement, true) {
return_string = new_string;
}
}
return_string
}
}
/// Implement PatternMatchMany for vectors of strings.
impl PatternMatchMany for [String] {
/// Match all from an array of strs with a boolean case_insensitive parameter
fn pattern_match_many(&self, patterns: &[&str], case_insensitive: bool) -> bool {
let mut num_matched = 0usize;
let num_patterns = patterns.len();
for pattern in patterns {
if self.pattern_match(pattern, case_insensitive) {
num_matched += 1;
}
}
num_matched == num_patterns
}
/// Match all from an array of strs with a in case-insensitive mode
fn pattern_match_many_ci(&self, patterns: &[&str]) -> bool {
self.pattern_match_many(patterns, true)
}
/// Match all from an array of strs with a in case-sensitive mode
fn pattern_match_many_cs(&self, patterns: &[&str]) -> bool {
self.pattern_match_many(patterns, false)
}
/// Match all from an array of tuples of strs and boolean case_insensitive flags
fn pattern_match_many_mixed(&self, pattern_sets: &[(&str, bool)]) -> bool {
let mut num_matched = 0usize;
let num_patterns = pattern_sets.len();
for pair in pattern_sets {
let (pattern, case_insensitive) = *pair;
if self.pattern_match(pattern, case_insensitive) {
num_matched += 1;
}
}
num_matched == num_patterns
}
/// Match all from an array of tuples of boolean positive, strs and boolean case_insensitive flags
/// Will check of each pattern is or is not matched with mixed case-sensitity rules
fn pattern_match_many_conditional(&self, pattern_sets: &[(bool, &str, bool)]) -> bool {
let mut num_matched = 0usize;
let num_patterns = pattern_sets.len();
for pattern_set in pattern_sets {
let (is_positive, pattern, case_insensitive) = *pattern_set;
let is_matched = self.pattern_match(pattern, case_insensitive);
if is_matched == is_positive {
num_matched += 1;
}
}
num_matched == num_patterns
}
/// with an array of tuples (patterns, case_insensitive)
/// Matches one or more of the patterns in case-sensitivity flag
fn pattern_match_any(&self, patterns: &[&str], case_insensitive: bool) -> bool {
for pattern in patterns {
if self.pattern_match(pattern, case_insensitive) {
return true;
}
}
false
}
/// Matches one or more of the patterns in case-insensitive mode
/// with an array of str patterns
fn pattern_match_any_ci(&self, patterns: &[&str]) -> bool {
self.pattern_match_any(patterns, true)
}
/// Matches one or more of the patterns in case-sensitive mode
/// with an array of str patterns
fn pattern_match_any_cs(&self, patterns: &[&str]) -> bool {
self.pattern_match_any(patterns, false)
}
/// Matches one or more of the patterns with case-insensitive flag
/// e.g. ```(r#"a[ck]"#, true)``` matches "ac" or "ak" whether upper, lower or mixed case
/// with an array of tuples (pattepattern_match_many_cirn, replacement, case_insensitive)
fn pattern_match_any_mixed(&self, pattern_sets: &[(&str, bool)]) -> bool {
for pair in pattern_sets {
let (pattern, case_insensitive) = *pair;
if self.pattern_match(pattern, case_insensitive) {
return true;
}
}
false
}
/// Matches one or more of the patterns with positivity condition and case-insensitive flag
fn pattern_match_any_conditional(&self, pattern_sets: &[(bool, &str, bool)]) -> bool {
for pattern_set in pattern_sets {
let (is_positive, pattern, case_insensitive) = *pattern_set;
let is_matched = self.pattern_match(pattern, case_insensitive);
if is_matched == is_positive {
return true;
}
}
false
}
}
/// ReplaceMany implementation for vectors of owned strings
impl PatternReplaceMany for Vec<String> {
/// Replace all matched patterns with mixed case-sensitivity flags, expressed as tuples of (pattern: &str, replacement: &str, case_insensitive: bool)
fn pattern_replace_sets(&self, replacement_sets: &[(&str, &str, bool)]) -> Vec<String> {
let mut return_strings = self.clone();
for replacement_set in replacement_sets {
let (pattern, replacement, case_insensitive) = *replacement_set;
if let Ok(new_strings) = return_strings.pattern_replace_result(pattern, replacement, case_insensitive) {
return_strings = new_strings;
}
}
return_strings
}
/// Replace all matched patterns in case-sensitive mode (unless defined in the pattern via (?i)),
/// expressed as tuples of (pattern: &str, replacement: &str)
fn pattern_replace_pairs(&self, replacement_pairs: &[(&str, &str)]) -> Vec<String> {
let mut return_strings = self.clone();
for replacement_pair in replacement_pairs {
let (pattern, replacement) = *replacement_pair;
if let Ok(new_string) = return_strings.pattern_replace_result(pattern, replacement, false) {
return_strings = new_string;
}
}
return_strings
}
/// Replace all matched patterns in case-insensitive,
/// expressed as tuples of (pattern: &str, replacement: &str)
fn pattern_replace_pairs_ci(&self, replacement_pairs: &[(&str, &str)]) -> Vec<String> {
let mut return_strings = self.clone();
for replacement_pair in replacement_pairs {
let (pattern, replacement) = *replacement_pair;
if let Ok(new_string) = return_strings.pattern_replace_result(pattern, replacement, true) {
return_strings = new_string;
}
}
return_strings
}
}