arr_rs/alphanumeric/operations/indexing.rs
1use crate::{
2 alphanumeric::prelude::*,
3 core::prelude::*,
4 errors::prelude::*,
5};
6use crate::prelude::Numeric;
7
8/// `ArrayTrait` - Alphanumeric Array operations
9pub trait ArrayStringIndexing<N: Alphanumeric> where Self: Sized + Clone {
10
11 /// Return string.len() element-wise
12 ///
13 /// # Examples
14 ///
15 /// ```
16 /// use arr_rs::prelude::*;
17 ///
18 /// let expected = Array::flat(vec![6, 9, 3]);
19 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaAacc".to_string(), "abc".to_string()]);
20 /// assert_eq!(expected, arr.str_len());
21 /// ```
22 ///
23 /// # Errors
24 ///
25 /// may returns `ArrayError`
26 fn str_len(&self) -> Result<Array<usize>, ArrayError>;
27
28 /// Returns an array with the number of non-overlapping occurrences of substring sub
29 ///
30 /// # Arguments
31 ///
32 /// * `sub` - substring to search for
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// use arr_rs::prelude::*;
38 ///
39 /// let expected = Array::flat(vec![3, 2, 1]);
40 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
41 /// assert_eq!(expected, arr.count(&Array::single("Aa".to_string()).unwrap()));
42 ///
43 /// let expected = Array::flat(vec![1]);
44 /// let arr = Array::flat(vec!["AaAaAa".to_string()]);
45 /// assert_eq!(expected, arr.count(&Array::single("AaAa".to_string()).unwrap()));
46 /// ```
47 ///
48 /// # Errors
49 ///
50 /// may returns `ArrayError`
51 fn count(&self, sub: &Array<N>) -> Result<Array<usize>, ArrayError>;
52
53 /// Checks if string element starts with prefix
54 ///
55 /// # Arguments
56 ///
57 /// * `prefix` - substring to search for
58 ///
59 /// # Examples
60 ///
61 /// ```
62 /// use arr_rs::prelude::*;
63 ///
64 /// let expected = Array::flat(vec![true, false, false]);
65 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
66 /// assert_eq!(expected, arr.starts_with(&Array::single("Aa".to_string()).unwrap()));
67 /// ```
68 ///
69 /// # Errors
70 ///
71 /// may returns `ArrayError`
72 fn starts_with(&self, prefix: &Array<N>) -> Result<Array<bool>, ArrayError>;
73
74 /// Checks if string element ends with suffix
75 ///
76 /// # Arguments
77 ///
78 /// * `suffix` - substring to search for
79 ///
80 /// # Examples
81 ///
82 /// ```
83 /// use arr_rs::prelude::*;
84 ///
85 /// let expected = Array::flat(vec![false, true, false]);
86 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
87 /// assert_eq!(expected, arr.ends_with(&Array::single("aA".to_string()).unwrap()));
88 /// ```
89 ///
90 /// # Errors
91 ///
92 /// may returns `ArrayError`
93 fn ends_with(&self, suffix: &Array<N>) -> Result<Array<bool>, ArrayError>;
94
95 /// For each element, return the lowest index in the string where substring sub is found
96 ///
97 /// # Arguments
98 ///
99 /// * `sub` - substring to search for
100 ///
101 /// # Examples
102 ///
103 /// ```
104 /// use arr_rs::prelude::*;
105 ///
106 /// let expected = Array::flat(vec![1, 0, -1]);
107 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
108 /// assert_eq!(expected, arr.find(&Array::single("aA".to_string()).unwrap()));
109 /// ```
110 ///
111 /// # Errors
112 ///
113 /// may returns `ArrayError`
114 fn find(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError>;
115
116 /// For each element, return the highest index in the string where substring sub is found
117 ///
118 /// # Arguments
119 ///
120 /// * `sub` - substring to search for
121 ///
122 /// # Examples
123 ///
124 /// ```
125 /// use arr_rs::prelude::*;
126 ///
127 /// let expected = Array::flat(vec![3, 4, -1]);
128 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
129 /// assert_eq!(expected, arr.rfind(&Array::single("aA".to_string()).unwrap()));
130 /// ```
131 ///
132 /// # Errors
133 ///
134 /// may returns `ArrayError`
135 fn rfind(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError>;
136
137 /// For each element, return the lowest index in the string where substring sub is found;
138 /// alias on `find`
139 ///
140 /// # Arguments
141 ///
142 /// * `sub` - substring to search for
143 ///
144 /// # Examples
145 ///
146 /// ```
147 /// use arr_rs::prelude::*;
148 ///
149 /// let expected = Array::flat(vec![1, 0, -1]);
150 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
151 /// assert_eq!(expected, arr.index(&Array::single("aA".to_string()).unwrap()));
152 /// ```
153 ///
154 /// # Errors
155 ///
156 /// may returns `ArrayError`
157 fn index(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError>;
158
159 /// For each element, return the highest index in the string where substring sub is found;
160 /// alias on `rfind`
161 ///
162 /// # Arguments
163 ///
164 /// * `sub` - substring to search for
165 ///
166 /// # Examples
167 ///
168 /// ```
169 /// use arr_rs::prelude::*;
170 ///
171 /// let expected = Array::flat(vec![3, 4, -1]);
172 /// let arr = Array::flat(vec!["AaAaAa".to_string(), "aAaAaA".to_string(), "bbAabb".to_string()]);
173 /// assert_eq!(expected, arr.rindex(&Array::single("aA".to_string()).unwrap()));
174 /// ```
175 ///
176 /// # Errors
177 ///
178 /// may returns `ArrayError`
179 fn rindex(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError>;
180}
181
182impl <N: Alphanumeric> ArrayStringIndexing<N> for Array<N> {
183
184 fn str_len(&self) -> Result<Array<usize>, ArrayError> {
185 let elements = self.clone().into_iter()
186 .map(|item| item.to_string().len())
187 .collect();
188 Array::new(elements, self.get_shape()?)
189 }
190
191 fn count(&self, sub: &Self) -> Result<Array<usize>, ArrayError> {
192 let broadcasted = self.broadcast(sub)?;
193 let elements = broadcasted.clone().into_iter()
194 .map(|item| item.0._count(item.1.to_string().as_str()))
195 .collect();
196 Array::new(elements, broadcasted.get_shape()?)
197 }
198
199 fn starts_with(&self, prefix: &Self) -> Result<Array<bool>, ArrayError> {
200 let broadcasted = self.broadcast(prefix)?;
201 let elements = broadcasted.clone().into_iter()
202 .map(|item| item.0.to_string().starts_with(&item.1.to_string()))
203 .collect();
204 Array::new(elements, broadcasted.get_shape()?)
205 }
206
207 fn ends_with(&self, suffix: &Self) -> Result<Array<bool>, ArrayError> {
208 let broadcasted = self.broadcast(suffix)?;
209 let elements = broadcasted.clone().into_iter()
210 .map(|item| item.0.to_string().ends_with(&item.1.to_string()))
211 .collect();
212 Array::new(elements, broadcasted.get_shape()?)
213 }
214
215 fn find(&self, sub: &Self) -> Result<Array<isize>, ArrayError> {
216 let broadcasted = self.broadcast(sub)?;
217 let elements = broadcasted.clone().into_iter()
218 .map(|item| item.0
219 .to_string()
220 .find(&item.1.to_string())
221 .map_or(-1, |idx| idx.to_isize()))
222 .collect();
223 Array::new(elements, broadcasted.get_shape()?)
224 }
225
226 fn rfind(&self, sub: &Self) -> Result<Array<isize>, ArrayError> {
227 let broadcasted = self.broadcast(sub)?;
228 let elements = broadcasted.clone().into_iter()
229 .map(|item| item.0
230 .to_string()
231 .rfind(&item.1.to_string())
232 .map_or(-1, |idx| idx.to_isize()))
233 .collect();
234 Array::new(elements, broadcasted.get_shape()?)
235 }
236
237 fn index(&self, sub: &Self) -> Result<Array<isize>, ArrayError> {
238 self.find(sub)
239 }
240
241 fn rindex(&self, sub: &Self) -> Result<Array<isize>, ArrayError> {
242 self.rfind(sub)
243 }
244}
245
246impl <N: Alphanumeric> ArrayStringIndexing<N> for Result<Array<N>, ArrayError> {
247
248 fn str_len(&self) -> Result<Array<usize>, ArrayError> {
249 self.clone()?.str_len()
250 }
251
252 fn count(&self, sub: &Array<N>) -> Result<Array<usize>, ArrayError> {
253 self.clone()?.count(sub)
254 }
255
256 fn starts_with(&self, prefix: &Array<N>) -> Result<Array<bool>, ArrayError> {
257 self.clone()?.starts_with(prefix)
258 }
259
260 fn ends_with(&self, suffix: &Array<N>) -> Result<Array<bool>, ArrayError> {
261 self.clone()?.ends_with(suffix)
262 }
263
264 fn find(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError> {
265 self.clone()?.find(sub)
266 }
267
268 fn rfind(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError> {
269 self.clone()?.rfind(sub)
270 }
271
272 fn index(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError> {
273 self.clone()?.index(sub)
274 }
275
276 fn rindex(&self, sub: &Array<N>) -> Result<Array<isize>, ArrayError> {
277 self.clone()?.rindex(sub)
278 }
279}