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
use crate::{enums::StringBounds, utils::{pairs_to_string_bounds, strs_to_string_bounds}, StripCharacters};

/// Regex-free matcher methods for common use cases
pub trait SimpleMatch {
  /// Starts with a case-insensitive alphanumeric sequence
  fn starts_with_ci(&self, pattern: &str) -> bool;
  
  /// Starts with a case-insensitive alphanumeric sequence
  fn starts_with_ci_alphanum(&self, pattern: &str) -> bool;
  
  /// Ends with a case-insensitive alphanumeric sequence
  fn ends_with_ci(&self, pattern: &str) -> bool;
  
  /// Ends with a case-insensitive alphanumeric sequence
  fn ends_with_ci_alphanum(&self, pattern: &str) -> bool;

  /// Contains a case-insensitive alphanumeric sequence
  fn contains_ci(&self, pattern: &str) -> bool;
  
  /// Contains a case-insensitive alphanumeric sequence
  fn contains_ci_alphanum(&self, pattern: &str) -> bool;
}

/// Implementation for &str/String 
impl SimpleMatch for str {
  /// Starts with a case-insensitive sequence
  fn starts_with_ci(&self, pattern: &str) -> bool {
    self.to_lowercase().starts_with(&pattern.to_lowercase())
  }
  
  /// Starts with a case-insensitive alphanumeric sequence
  fn starts_with_ci_alphanum(&self, pattern: &str) -> bool {
    self.to_lowercase().strip_non_alphanum().starts_with(&pattern.to_lowercase())
  }
  
  /// Ends with a case-insensitive sequence
  fn ends_with_ci(&self, pattern: &str) -> bool {
    self.to_lowercase().ends_with(&pattern.to_lowercase())
  }
  
  /// Ends with a case-insensitive alphanumeric sequence
  fn ends_with_ci_alphanum(&self, pattern: &str) -> bool {
    self.to_lowercase().strip_non_alphanum().ends_with(&pattern.to_lowercase())
  }

  /// Contains a case-insensitive sequence
  fn contains_ci(&self, pattern: &str) -> bool {
    self.to_lowercase().contains(&pattern.to_lowercase())
  }
  
  /// Contains a case-insensitive alphanumeric sequence
  fn contains_ci_alphanum(&self, pattern: &str) -> bool {
    self.to_lowercase().strip_non_alphanum().contains(&pattern.to_lowercase())
  }
}

/// Return the indices of all ocurrences of a string
pub trait MatchOccurrences {
  /// Return the indices only of all matches of a given string pattern (not a regular expression)
  /// Builds on match_indices in the Rust standard library
  fn find_matched_indices(&self, pat: &str) -> Vec<usize>;
}


impl MatchOccurrences for str {
    /// Return the indices only of all matches of a given regular expression
  fn find_matched_indices(&self, pat: &str) -> Vec<usize> {
    self.match_indices(pat).into_iter().map(|pair| pair.0).collect::<Vec<usize>>()
  }
}


/// 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, 2);
    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, true, 2);
    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, false, 2);
    self.matched_conditional(&pattern_sets)
  }
  
}

impl SimpleMatchesMany for str {

  // test for multiple conditions. All other trait methods are derived from this
  fn matched_conditional(&self, pattern_sets: &[StringBounds]) -> Vec<bool> {
    let mut matched_items: Vec<bool> = Vec::with_capacity(pattern_sets.len());
    for item in pattern_sets {
      let ci = item.case_insensitive();
      // cast the sample string to lowercase for case-insenitive matches
      let base = if ci {
        self.to_lowercase()
      } else {
        self.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 {
        base.contains(&pattern)
      } == item.is_positive();
       matched_items.push(is_matched);
     }
     matched_items
   }
}

/// 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, 2);
    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, true, 2);
    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, false, 2);
    self.match_all_conditional(&pattern_sets)
  }
  
}

impl SimpleMatchAll for str {

  // test for multiple conditions. All other 'many' trait methods are derived from this
  fn match_all_conditional(&self, pattern_sets: &[StringBounds]) -> bool {
    self.matched_conditional(pattern_sets).into_iter().all(|matched| matched)
  }

}


/// Test multiple patterns and return a filtered vector of string slices
pub trait SimpleFilterAll {

  /// test for multiple conditions. All other trait methods are derived from this
  fn filter_all_conditional(&self, pattern_sets: &[StringBounds]) -> Vec<&str>;
  
}

/// Filter strings by one or more StringBounds rules
impl SimpleFilterAll for [&str] {

  // filter string slices by multiple conditions
  fn filter_all_conditional(&self, pattern_sets: &[StringBounds]) -> Vec<&str> {
    self.into_iter().map(|s| s.to_owned()).filter(|s| s.match_all_conditional(pattern_sets)).collect::<Vec<&str>>()
  }

}


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

}