simple-string-patterns 0.4.0

Makes it easier to match, split and extract strings in Rust without regular expressions. The parallel string-patterns crate provides extensions to work with regular expressions via the Regex library
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use crate::utils::{pairs_to_string_bounds, strs_to_string_bounds};
use crate::{enums::StringBounds, BoundsBuilder};
use crate::{BoundsPosition, CaseMatchMode, SimpleMatch};
use alphanumeric::StripCharacters;

/// Test for any of multiple pattern rules and return boolean
pub trait SimpleMatchAny
where
    Self: SimpleMatchesMany,
{
    /// test for multiple conditions. All other trait methods are derived from this
    fn match_any_conditional(&self, pattern_sets: &[StringBounds]) -> bool;

    /// test for multiple conditions with simple tuple pairs of pattern + case-insenitive flag
    fn contains_any_conditional(&self, pattern_sets: &[(&str, bool)]) -> bool {
        let pattern_sets: Vec<StringBounds> =
            pairs_to_string_bounds(pattern_sets, BoundsPosition::Contains);
        self.match_any_conditional(&pattern_sets)
    }

    /// Test for presecnce of simple patterns in case-insensitive mode
    fn contains_any_conditional_ci(&self, patterns: &[&str]) -> bool {
        let pattern_sets: Vec<StringBounds> = strs_to_string_bounds(
            patterns,
            CaseMatchMode::Insensitive,
            BoundsPosition::Contains,
        );
        self.match_any_conditional(&pattern_sets)
    }

    /// Test for presecnce of simple patterns in case-sensitive mode
    fn contains_any_conditional_cs(&self, patterns: &[&str]) -> bool {
        let pattern_sets: Vec<StringBounds> =
            strs_to_string_bounds(patterns, CaseMatchMode::Sensitive, BoundsPosition::Contains);
        self.match_any_conditional(&pattern_sets)
    }
}

/// Test multiple patterns and return vector of booleans with the results for each item
pub trait SimpleMatchesMany
where
    Self: SimpleMatch,
{
    /// test for multiple conditions. All other trait methods are derived from this
    fn matched_conditional(&self, pattern_sets: &[StringBounds]) -> Vec<bool>;

    /// test for multiple conditions with simple tuple pairs of pattern + case-insenitive flag
    fn contains_conditional(&self, pattern_sets: &[(&str, bool)]) -> Vec<bool> {
        let pattern_sets: Vec<StringBounds> =
            pairs_to_string_bounds(pattern_sets, BoundsPosition::Contains);
        self.matched_conditional(&pattern_sets)
    }

    /// Test for presecnce of simple patterns in case-insensitive mode
    fn contains_conditional_ci(&self, patterns: &[&str]) -> Vec<bool> {
        let pattern_sets: Vec<StringBounds> = strs_to_string_bounds(
            patterns,
            CaseMatchMode::Insensitive,
            BoundsPosition::Contains,
        );
        self.matched_conditional(&pattern_sets)
    }

    /// Test for presecnce of simple patterns in case-sensitive mode
    fn contains_conditional_cs(&self, patterns: &[&str]) -> Vec<bool> {
        let pattern_sets: Vec<StringBounds> =
            strs_to_string_bounds(patterns, CaseMatchMode::Sensitive, BoundsPosition::Contains);
        self.matched_conditional(&pattern_sets)
    }
}


/// Common function to match scalar StringBounds rules
pub(crate) fn match_bounds_rule(txt: &str, item: &StringBounds) -> bool {
    let cm = item.case_mode();
    let ci = item.case_insensitive();
    // cast the sample string to lowercase for case-insenitive matches
    let base = if ci {
        match cm {
            CaseMatchMode::AlphanumInsensitive => txt.to_lowercase().strip_non_alphanum(),
            _ => txt.to_lowercase(),
        }
    } else {
        txt.to_owned()
    };
    // cast the simple pattern to lowercase for case-insenitive matches
    let pattern = if ci {
        item.pattern().to_lowercase()
    } else {
        item.pattern().to_owned()
    };
    // check if outcome of starts_with, ends_with or contains test matches the positivity value
    let is_matched = if item.starts_with() {
        base.starts_with(&pattern)
    } else if item.ends_with() {
        base.ends_with(&pattern)
    } else if item.matches_whole() {
        base == pattern
    } else {
        base.contains(&pattern)
    } == item.is_positive();
    is_matched
}

/// Common function to match StringBounds rule sets handling both and/or sub rules and scalar rules
pub(crate) fn match_bounds_rule_set(txt: &str, item: &StringBounds) -> bool {
    match item {
        StringBounds::And(inner_rules) => txt
            .matched_conditional(&inner_rules)
            .into_iter()
            .all(|result| result),
        StringBounds::Or(inner_rules) => txt
            .matched_conditional(&inner_rules)
            .into_iter()
            .any(|result| result),
        _ => match_bounds_rule(txt, item),
    }
}

impl<T: AsRef<str>> SimpleMatchesMany for T {
    fn matched_conditional(&self, pattern_sets: &[StringBounds]) -> Vec<bool> {
        let s = self.as_ref();
        let mut matched_items: Vec<bool> = Vec::with_capacity(pattern_sets.len());
        for item in pattern_sets {
            matched_items.push(match_bounds_rule_set(s, item));
        }
        matched_items
    }
}

impl<T: AsRef<str>> SimpleMatchAny for T {
    fn match_any_conditional(&self, pattern_sets: &[StringBounds]) -> bool {
        let s = self.as_ref();
        for item in pattern_sets {
            if match_bounds_rule_set(s, item) {
                return true;
            }
        }
        false
    }
}

/// Test multiple patterns and return boolean
pub trait SimpleMatchAll
where
    Self: SimpleMatchesMany,
{
    /// test for multiple conditions. All other trait methods are derived from this
    fn match_all_conditional(&self, pattern_sets: &[StringBounds]) -> bool;

    /// test for multiple conditions with simple tuple pairs of pattern + case-insenitive flag
    fn contains_all_conditional(&self, pattern_sets: &[(&str, bool)]) -> bool {
        let pattern_sets: Vec<StringBounds> =
            pairs_to_string_bounds(pattern_sets, BoundsPosition::Contains);
        self.match_all_conditional(&pattern_sets)
    }

    /// Test for presecnce of simple patterns in case-insensitive mode
    fn contains_all_conditional_ci(&self, patterns: &[&str]) -> bool {
        let pattern_sets: Vec<StringBounds> = strs_to_string_bounds(
            patterns,
            CaseMatchMode::Insensitive,
            BoundsPosition::Contains,
        );
        self.match_all_conditional(&pattern_sets)
    }

    /// Test for presecnce of simple patterns in case-sensitive mode
    fn contains_all_conditional_cs(&self, patterns: &[&str]) -> bool {
        let pattern_sets: Vec<StringBounds> =
            strs_to_string_bounds(patterns, CaseMatchMode::Sensitive, BoundsPosition::Contains);
        self.match_all_conditional(&pattern_sets)
    }
}

impl<T: AsRef<str>> SimpleMatchAll for T {
    fn match_all_conditional(&self, pattern_sets: &[StringBounds]) -> bool {
        let s = self.as_ref();
        if pattern_sets.len() > 0 {
            for item in pattern_sets {
                if !match_bounds_rule_set(s, item) {
                    return false;
                }
            }
            true
        } else {
            false
        }
    }
}

/// Test multiple patterns and return a filtered vector of string slices by all pattern rules
pub trait SimpleFilterAll<'a, T> {
    /// test for multiple conditions. All other trait methods are derived from this
    fn filter_all_conditional(&'a self, pattern_sets: &[StringBounds]) -> Vec<T>;

    fn filter_all_rules(&'a self, rules: &BoundsBuilder) -> Vec<T> {
        self.filter_all_conditional(&rules.as_vec())
    }
}

/// Filter strings by one or more StringBounds rules
impl<'a> SimpleFilterAll<'a, &'a str> for [&str] {
    // filter string slices by multiple conditions
    fn filter_all_conditional(&'a self, pattern_sets: &[StringBounds]) -> Vec<&'a str> {
        self.into_iter()
            .map(|s| s.to_owned())
            .filter(|s| s.match_all_conditional(pattern_sets))
            .collect::<Vec<&'a str>>()
    }
}

/// Variant implementation for owned strings
impl<'a> SimpleFilterAll<'a, String> for [String] {
    // filter strings by multiple conditions
    fn filter_all_conditional(&'a self, pattern_sets: &[StringBounds]) -> Vec<String> {
        self.into_iter()
            .filter(|s| s.match_all_conditional(pattern_sets))
            .map(|s| s.to_owned())
            .collect::<Vec<String>>()
    }
}

/// Test multiple patterns and return a filtered vector of string slices by any of the pattern rules
pub trait SimpleFilterAny<'a, T> {
    /// test for multiple conditions. All other trait methods are derived from this
    fn filter_any_conditional(&'a self, pattern_sets: &[StringBounds]) -> Vec<T>;

    fn filter_any_rules(&'a self, rules: &BoundsBuilder) -> Vec<T> {
        self.filter_any_conditional(&rules.as_vec())
    }
}

/// Filter strings by one or more StringBounds rules
impl<'a> SimpleFilterAny<'a, &'a str> for [&str] {
    // filter string slices by multiple conditions
    fn filter_any_conditional(&'a self, pattern_sets: &[StringBounds]) -> Vec<&'a str> {
        self.into_iter()
            .map(|s| s.to_owned())
            .filter(|s| s.match_any_conditional(pattern_sets))
            .collect::<Vec<&'a str>>()
    }
}

/// Variant implementation for owned strings
impl<'a> SimpleFilterAny<'a, String> for [String] {
    // filter strings by multiple conditions
    fn filter_any_conditional(&'a self, pattern_sets: &[StringBounds]) -> Vec<String> {
        self.into_iter()
            .filter(|s| s.match_any_conditional(pattern_sets))
            .map(|s| s.to_owned())
            .collect::<Vec<String>>()
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn test_simple_filter_all() {
        let source_strs = [
            "Cat image",
            "dog picture",
            "elephant image",
            "CAT_Video",
            "cat Picture",
        ];
        let target_strs = ["Cat image", "cat Picture"];

        let conditions = bounds_builder()
            .starting_with_ci("cat")
            .not_containing_ci("video")
            .as_vec();
        assert_eq!(source_strs.filter_all_conditional(&conditions), target_strs);
    }

    #[test]
    fn test_nested_rules_with_filter_all() {
        let source_strs = [
            "_Cat image.jpg",
            "-dog picture.png",
            "_DOG pc.jpg",
            "elephant image.psd",
            "CAT_Video.mp4",
            "lion Picture.jpg",
        ];
        let target_strs = ["_Cat image.jpg", "_DOG pc.jpg"];
        let patterns = vec!["cat", "dog"];
        let conditions = bounds_builder()
            .or_starting_with_ci_alphanum(&patterns)
            .ending_with_ci(".jpg");
        assert_eq!(source_strs.filter_all_rules(&conditions), target_strs);
    }

    #[test]
    fn test_nested_rules_with_filter_any() {
        let source_strs = [
            "_Cat image.jpg",
            "-dog picture.png",
            "_DOG pc.jpg",
            "elephant image.psd",
            "CAT_Video.mp4",
            "lion Picture.jpg",
        ];
        let target_strs = ["_Cat image.jpg", "elephant image.psd"];
        let conditions = bounds_builder()
            .and(
                bounds_builder()
                    .starting_with_ci_alphanum("cat")
                    .ending_with_ci(".jpg"),
            )
            .ending_with_ci(".psd");
        assert_eq!(source_strs.filter_any_rules(&conditions), target_strs);
    }

    #[test]
    fn test_matched_conditional() {
        let conditions = [
            StringBounds::StartsWith("jan", true, CaseMatchMode::Insensitive),
            StringBounds::EndsWith("images", true, CaseMatchMode::Insensitive),
            StringBounds::Contains("2023", true, CaseMatchMode::Insensitive),
        ];

        let folder_1 = "Jan_2023_IMAGES";
        let folder_2 = "january_2024_Images";

        assert_eq!(
            folder_1.matched_conditional(&conditions),
            vec![true, true, true]
        );
        assert!(folder_1.match_all_conditional(&conditions));
        assert_eq!(
            folder_2.matched_conditional(&conditions),
            vec![true, true, false]
        );

        let test_strs = ["image", "cat", "garden"];
        let folder_3 = "cat-IMAGES_Garden";
        let folder_4 = "images-of-cats-and-dogs-in-the-park";

        assert!(folder_3.contains_all_conditional_ci(&test_strs));
        assert_eq!(folder_4.contains_all_conditional_ci(&test_strs), false);

        let file_names = [
            "edited-img-Nepal-Feb-2003.psd",
            "image-Thailand-Mar-2003.jpg",
            "photo_Nepal_Jan-2005.jpg",
            "image-India-Mar-2003.jpg",
            "pic_nepal_Dec-2004.png",
        ];

        let mixed_conditions = bounds_builder()
            .containing_ci("nepal")
            .not_ending_with_ci(".psd")
            .as_vec();

        let file_name_a = file_names[0];
        let file_name_b = file_names[2];

        assert!(file_name_a.match_all_conditional(&mixed_conditions) == false);
        assert!(file_name_b.match_all_conditional(&mixed_conditions));

        let nepal_jpg_files: Vec<&str> = file_names.filter_all_conditional(&mixed_conditions);
        assert_eq!(nepal_jpg_files.len(), 2);
        assert_eq!(nepal_jpg_files[0], file_name_b);

        let file_names_vector = file_names
            .into_iter()
            .map(|s| s.to_string())
            .collect::<Vec<String>>();

        let nepal_jpg_files_vector: Vec<String> =
            file_names_vector.filter_all_conditional(&mixed_conditions);
        assert_eq!(nepal_jpg_files_vector.len(), 2);
    }

    #[test]
    fn test_matched_any_conditional() {
        let false_conditions = [
            StringBounds::Whole("no", true, CaseMatchMode::Insensitive),
            StringBounds::Whole("false", true, CaseMatchMode::Insensitive),
            StringBounds::Contains("not", true, CaseMatchMode::Insensitive),
            StringBounds::Contains("negative", true, CaseMatchMode::Insensitive),
        ];

        let boolean_strs_1 = ["NO", "FALSE", "not at all", "negative result", "Noon"];
        let falsy_strs = ["NO", "FALSE", "not at all", "negative result"];

        assert_eq!(
            boolean_strs_1.filter_any_conditional(&false_conditions),
            falsy_strs
        );

        let true_conditions = bounds_builder()
            .is_ci("yes")
            .is_ci("y")
            .starting_with_ci("ok")
            .containing_ci("positive")
            .as_vec();
        let boolean_strs_2 = ["Yes", "y", "Yep", "okay", "positive result", "good"];
        let truthy_strs = ["Yes", "y", "okay", "positive result"];

        assert_eq!(
            boolean_strs_2.filter_any_conditional(&true_conditions),
            truthy_strs
        );
    }

    #[test]
    fn test_bounds_builder() {
        let rules = bounds_builder()
            .starting_with_ci("cat")
            .not_ending_with_ci(".jpg");

        let sample_strs = [
            "cat-picture.jpg",
            "Dog-picture.png",
            "CAT-image.png",
            "rabbit-photo.png",
            "cAt-pic.webp",
        ];

        let filtered_lines = sample_strs.filter_all_rules(&rules);
        let expected_lines = vec!["CAT-image.png", "cAt-pic.webp"];
        assert_eq!(filtered_lines, expected_lines);

        let rules_2 = bounds_builder()
            .starting_with_ci("cat")
            .or_ending_with_ci(&[".jpg", ".png"]);

        let filtered_lines_2 = sample_strs.filter_all_rules(&rules_2);
        let expected_lines_2 = vec!["cat-picture.jpg", "CAT-image.png"];
        assert_eq!(filtered_lines_2, expected_lines_2);
    }

    #[test]
    fn test_and_starting_with_ci_positive() {
        // and_starting_with_ci pushes a positive StartsWith AND-group, so it should behave
        // like starting_with_ci for a single pattern, not like its not_-prefixed counterpart.
        let source_strs = ["cat-picture.jpg", "Dog-picture.png", "CAT-image.png"];
        let rules = bounds_builder().and_starting_with_ci(&["cat"]);
        let filtered = source_strs.filter_all_rules(&rules);
        assert_eq!(filtered, vec!["cat-picture.jpg", "CAT-image.png"]);
    }

    #[test]
    fn test_and_or_inner_bounds_builder() {
        let sample_strs = [
            "cat-picture.jpg",
            "Dog-picture.png",
            "DOG_portrait-2018.webp",
            "lion-image-2009.webp",
            "CAT-image.png",
            "rabbit-photo.png",
            "cAt-pic.webp",
        ];

        let rules_3 = bounds_builder()
            .and_not_starting_with_ci(&["cat", "lion"])
            .or_ending_with_ci(&[".png", ".webp"]);

        let filtered_lines_3 = sample_strs.filter_all_rules(&rules_3);
        let expected_lines_3 = vec![
            "Dog-picture.png",
            "DOG_portrait-2018.webp",
            "rabbit-photo.png",
        ];
        assert_eq!(filtered_lines_3, expected_lines_3);
    }
}