arr_rs/alphanumeric/operations/manipulate.rs
1use crate::{
2 alphanumeric::prelude::*,
3 core::prelude::*,
4 errors::prelude::*,
5};
6
7/// `ArrayTrait` - Alphanumeric Array operations
8pub trait ArrayStringManipulate<N: Alphanumeric> where Self: Sized + Clone {
9
10 /// Return element-wise string concatenation for two arrays of String
11 ///
12 /// # Arguments
13 ///
14 /// * `other` - array to perform the operation with
15 ///
16 /// # Examples
17 ///
18 /// ```
19 /// use arr_rs::prelude::*;
20 ///
21 /// let expected = Array::flat(vec!["abcdd".to_string(), "cdeff".to_string()]);
22 /// let arr = Array::flat(vec!["abc".to_string(), "cde".to_string()]);
23 /// let other = Array::flat(vec!["dd".to_string(), "ff".to_string()]).unwrap();
24 /// assert_eq!(expected, arr.add(&other));
25 /// ```
26 ///
27 /// # Errors
28 ///
29 /// may returns `ArrayError`
30 fn add(&self, other: &Array<N>) -> Result<Array<N>, ArrayError>;
31
32 /// Return (a * i), that is string multiple concatenation, element-wise
33 ///
34 /// # Arguments
35 ///
36 /// * `counts` - array to perform the operation with
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// use arr_rs::prelude::*;
42 ///
43 /// let expected = Array::flat(vec!["aaa".to_string(), "bbbbb".to_string()]);
44 /// let arr = Array::flat(vec!["a".to_string(), "b".to_string()]);
45 /// let counts = Array::flat(vec![3, 5]).unwrap();
46 /// assert_eq!(expected, arr.multiply(&counts));
47 /// ```
48 ///
49 /// # Errors
50 ///
51 /// may returns `ArrayError`
52 fn multiply(&self, counts: &Array<usize>) -> Result<Array<N>, ArrayError>;
53
54 /// Capitalizes first character of each element
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use arr_rs::prelude::*;
60 ///
61 /// let expected = Array::flat(vec!["A1a".to_string(), "2bb".to_string()]);
62 /// let arr = Array::flat(vec!["a1a".to_string(), "2bb".to_string()]);
63 /// assert_eq!(expected, arr.capitalize());
64 /// ```
65 ///
66 /// # Errors
67 ///
68 /// may returns `ArrayError`
69 fn capitalize(&self) -> Result<Array<N>, ArrayError>;
70
71 /// Turns characters to lower-case
72 ///
73 /// # Examples
74 ///
75 /// ```
76 /// use arr_rs::prelude::*;
77 ///
78 /// let expected = Array::flat(vec!["a1a".to_string(), "2bb".to_string()]);
79 /// let arr = Array::flat(vec!["A1a".to_string(), "2bB".to_string()]);
80 /// assert_eq!(expected, arr.lower());
81 /// ```
82 ///
83 /// # Errors
84 ///
85 /// may returns `ArrayError`
86 fn lower(&self) -> Result<Array<N>, ArrayError>;
87
88 /// Turns characters to upper-case
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use arr_rs::prelude::*;
94 ///
95 /// let expected = Array::flat(vec!["A1A".to_string(), "2BB".to_string()]);
96 /// let arr = Array::flat(vec!["a1a".to_string(), "2bb".to_string()]);
97 /// assert_eq!(expected, arr.upper());
98 /// ```
99 ///
100 /// # Errors
101 ///
102 /// may returns `ArrayError`
103 fn upper(&self) -> Result<Array<N>, ArrayError>;
104
105 /// Swap characters case
106 ///
107 /// # Examples
108 ///
109 /// ```
110 /// use arr_rs::prelude::*;
111 ///
112 /// let expected = Array::flat(vec!["A1a".to_string(), "2bB".to_string()]);
113 /// let arr = Array::flat(vec!["a1A".to_string(), "2Bb".to_string()]);
114 /// assert_eq!(expected, arr.swapcase());
115 /// ```
116 ///
117 /// # Errors
118 ///
119 /// may returns `ArrayError`
120 fn swapcase(&self) -> Result<Array<N>, ArrayError>;
121
122 /// Centers elements in a string of length of `width`
123 ///
124 /// # Arguments
125 ///
126 /// * `width` - length of the resulting strings
127 /// * `fill_char` - padding character to use. defaults to space
128 ///
129 /// # Examples
130 ///
131 /// ```
132 /// use arr_rs::prelude::*;
133 ///
134 /// let expected = Array::flat(vec!["***aaa***".to_string(), "***bbbb**".to_string()]);
135 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbb".to_string()]);
136 /// assert_eq!(expected, arr.center(&Array::single(9).unwrap(), Some(Array::single('*').unwrap())));
137 /// ```
138 ///
139 /// # Errors
140 ///
141 /// may returns `ArrayError`
142 fn center(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Result<Array<N>, ArrayError>;
143
144 /// Concatenate the strings in the sequence
145 ///
146 /// # Arguments
147 ///
148 /// * `sep` - array of separators to concatenate strings with
149 ///
150 /// # Examples
151 ///
152 /// ```
153 /// use arr_rs::prelude::*;
154 ///
155 /// let expected = Array::flat(vec!["a-a-a".to_string(), "b.b.b.b".to_string()]);
156 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbb".to_string()]);
157 /// let sep = Array::flat(vec!["-".to_string(), ".".to_string()]).unwrap();
158 /// assert_eq!(expected, arr.join(&sep));
159 /// ```
160 ///
161 /// # Errors
162 ///
163 /// may returns `ArrayError`
164 fn join(&self, sep: &Array<N>) -> Result<Array<N>, ArrayError>;
165
166 /// Partition each element around first occurrence of sep
167 ///
168 /// # Arguments
169 ///
170 /// * `sep` - separator to split each string element
171 ///
172 /// # Examples
173 ///
174 /// ```
175 /// use arr_rs::prelude::*;
176 ///
177 /// let expected = Array::flat(vec![Tuple3("a".to_string(), "-".to_string(), "a-a".to_string()), Tuple3("b".to_string(), "-".to_string(), "b-b-b".to_string())]);
178 /// let arr = Array::flat(vec!["a-a-a".to_string(), "b-b-b-b".to_string()]);
179 /// assert_eq!(expected, ArrayStringManipulate::partition(&arr, &Array::single("-".to_string()).unwrap()));
180 /// ```
181 ///
182 /// # Errors
183 ///
184 /// may returns `ArrayError`
185 fn partition(&self, sep: &Array<N>) -> Result<Array<Tuple3<N, N, N>>, ArrayError>;
186
187 /// Partition each element around last occurrence of sep
188 ///
189 /// # Arguments
190 ///
191 /// * `sep` - separator to split each string element
192 ///
193 /// # Examples
194 ///
195 /// ```
196 /// use arr_rs::prelude::*;
197 ///
198 /// let expected = Array::flat(vec![Tuple3("a-a".to_string(), "-".to_string(), "a".to_string()), Tuple3("b-b-b".to_string(), "-".to_string(), "b".to_string())]);
199 /// let arr = Array::flat(vec!["a-a-a".to_string(), "b-b-b-b".to_string()]);
200 /// assert_eq!(expected, arr.rpartition(&Array::single("-".to_string()).unwrap()));
201 /// ```
202 ///
203 /// # Errors
204 ///
205 /// may returns `ArrayError`
206 fn rpartition(&self, sep: &Array<N>) -> Result<Array<Tuple3<N, N, N>>, ArrayError>;
207
208 /// Returns a list of the words in the string, using sep as the delimiter string.
209 ///
210 /// # Arguments
211 ///
212 /// * `sep` - separator to split each string element. defaults to space
213 /// * `max_split` - at most <`max_split`> splits are done
214 ///
215 /// # Examples
216 ///
217 /// ```
218 /// use arr_rs::prelude::*;
219 ///
220 /// let expected = Array::flat(vec![List(vec!["a".to_string(), "a".to_string(), "a".to_string()]), List(vec!["b".to_string(), "b".to_string(), "b".to_string(), "b".to_string()])]);
221 /// let arr = Array::flat(vec!["a-a-a".to_string(), "b-b-b-b".to_string()]).unwrap();
222 /// assert_eq!(expected, ArrayStringManipulate::split(&arr, Some(Array::single("-".to_string()).unwrap()), None));
223 /// ```
224 ///
225 /// # Errors
226 ///
227 /// may returns `ArrayError`
228 fn split(&self, sep: Option<Array<N>>, max_split: Option<Array<usize>>) -> Result<Array<List<N>>, ArrayError>;
229
230 /// Returns a list of the words in the string, using sep as the delimiter string.
231 ///
232 /// # Arguments
233 ///
234 /// * `sep` - separator to split each string element. defaults to space
235 /// * `max_split` - at most <`max_split`> splits are done
236 ///
237 /// # Examples
238 ///
239 /// ```
240 /// use arr_rs::prelude::*;
241 ///
242 /// let expected = Array::flat(vec![List(vec!["a".to_string(), "a".to_string(), "a".to_string()]), List(vec!["b".to_string(), "b".to_string(), "b".to_string(), "b".to_string()])]);
243 /// let arr = Array::flat(vec!["a-a-a".to_string(), "b-b-b-b".to_string()]).unwrap();
244 /// assert_eq!(expected, ArrayStringManipulate::rsplit(&arr, Some(Array::single("-".to_string()).unwrap()), None));
245 /// ```
246 ///
247 /// # Errors
248 ///
249 /// may returns `ArrayError`
250 fn rsplit(&self, sep: Option<Array<N>>, max_split: Option<Array<usize>>) -> Result<Array<List<N>>, ArrayError>;
251
252 /// Returns a list of the words in the string, using line break character as the delimiter string.
253 ///
254 /// # Arguments
255 ///
256 /// * `keep_ends` - if true, line break character will be kept
257 ///
258 /// # Examples
259 ///
260 /// ```
261 /// use arr_rs::prelude::*;
262 ///
263 /// let expected = Array::flat(vec![List(vec!["aa".to_string(), "a".to_string()]), List(vec!["bb".to_string(), "bb".to_string()])]);
264 /// let arr = Array::flat(vec!["aa\na".to_string(), "bb\nbb".to_string()]).unwrap();
265 /// assert_eq!(expected, arr.splitlines(None));
266 /// ```
267 ///
268 /// # Errors
269 ///
270 /// may returns `ArrayError`
271 fn splitlines(&self, keep_ends: Option<Array<bool>>) -> Result<Array<List<N>>, ArrayError>;
272
273 /// Replaces all occurrences of <old> with <new>
274 ///
275 /// # Arguments
276 ///
277 /// * `old` - string to replace
278 /// * `new` - new string
279 /// * `count` - maximum occurrences to replace
280 ///
281 /// # Examples
282 ///
283 /// ```
284 /// use arr_rs::prelude::*;
285 ///
286 /// let expected = Array::flat(vec!["new string".to_string()]);
287 /// let arr = Array::flat(vec!["old string".to_string()]);
288 /// let new = Array::flat(vec!["new".to_string()]).unwrap();
289 /// let old = Array::flat(vec!["old".to_string()]).unwrap();
290 /// assert_eq!(expected, arr.replace(&old, &new, None));
291 /// ```
292 ///
293 /// # Errors
294 ///
295 /// may returns `ArrayError`
296 fn replace(&self, old: &Array<N>, new: &Array<N>, count: Option<usize>) -> Result<Array<N>, ArrayError>;
297
298 /// Trims leading and trailing characters
299 ///
300 /// # Arguments
301 ///
302 /// * `chars` - characters to trim. defaults to whitespace
303 ///
304 /// # Examples
305 ///
306 /// ```
307 /// use arr_rs::prelude::*;
308 ///
309 /// let expected = Array::flat(vec!["b".to_string(), "bbbb".to_string()]);
310 /// let arr = Array::flat(vec!["aaba".to_string(), "ccbbbbc".to_string()]);
311 /// assert_eq!(expected, arr.strip(Some(Array::flat(vec!["a".to_string(), "c".to_string()]).unwrap())));
312 /// ```
313 ///
314 /// # Errors
315 ///
316 /// may returns `ArrayError`
317 fn strip(&self, chars: Option<Array<N>>) -> Result<Array<N>, ArrayError>;
318
319 /// Trims leading characters
320 ///
321 /// # Arguments
322 ///
323 /// * `chars` - characters to trim. defaults to whitespace
324 ///
325 /// # Examples
326 ///
327 /// ```
328 /// use arr_rs::prelude::*;
329 ///
330 /// let expected = Array::flat(vec!["ba".to_string(), "c".to_string()]);
331 /// let arr = Array::flat(vec!["aaba".to_string(), "bbbbc".to_string()]);
332 /// assert_eq!(expected, arr.lstrip(Some(Array::flat(vec!["a".to_string(), "b".to_string()]).unwrap())));
333 /// ```
334 ///
335 /// # Errors
336 ///
337 /// may returns `ArrayError`
338 fn lstrip(&self, chars: Option<Array<N>>) -> Result<Array<N>, ArrayError>;
339
340 /// Trims trailing characters
341 ///
342 /// # Arguments
343 ///
344 /// * `chars` - characters to trim. defaults to whitespace
345 ///
346 /// # Examples
347 ///
348 /// ```
349 /// use arr_rs::prelude::*;
350 ///
351 /// let expected = Array::flat(vec!["ba".to_string(), "c".to_string()]);
352 /// let arr = Array::flat(vec!["aaba".to_string(), "bbbbc".to_string()]);
353 /// assert_eq!(expected, arr.lstrip(Some(Array::flat(vec!["a".to_string(), "b".to_string()]).unwrap())));
354 /// ```
355 ///
356 /// # Errors
357 ///
358 /// may returns `ArrayError`
359 fn rstrip(&self, chars: Option<Array<N>>) -> Result<Array<N>, ArrayError>;
360
361 /// Left-justifies elements in a string of length of `width`
362 ///
363 /// # Arguments
364 ///
365 /// * `width` - length of the resulting strings
366 /// * `fill_char` - padding character to use. defaults to space
367 ///
368 /// # Examples
369 ///
370 /// ```
371 /// use arr_rs::prelude::*;
372 ///
373 /// let expected = Array::flat(vec!["aaa******".to_string(), "bbbb*****".to_string()]);
374 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbb".to_string()]);
375 /// assert_eq!(expected, arr.ljust(&Array::single(9).unwrap(), Some(Array::single('*').unwrap())));
376 /// ```
377 ///
378 /// # Errors
379 ///
380 /// may returns `ArrayError`
381 fn ljust(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Result<Array<N>, ArrayError>;
382
383 /// Right-justifies elements in a string of length of `width`
384 ///
385 /// # Arguments
386 ///
387 /// * `width` - length of the resulting strings
388 /// * `fill_char` - padding character to use. defaults to space
389 ///
390 /// # Examples
391 ///
392 /// ```
393 /// use arr_rs::prelude::*;
394 ///
395 /// let expected = Array::flat(vec!["******aaa".to_string(), "*****bbbb".to_string()]);
396 /// let arr = Array::flat(vec!["aaa".to_string(), "bbbb".to_string()]);
397 /// assert_eq!(expected, arr.rjust(&Array::single(9).unwrap(), Some(Array::single('*').unwrap())));
398 /// ```
399 ///
400 /// # Errors
401 ///
402 /// may returns `ArrayError`
403 fn rjust(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Result<Array<N>, ArrayError>;
404
405 /// Return the numeric string left-filled with zeros
406 ///
407 /// # Arguments
408 ///
409 /// * `width` - length of the resulting strings
410 ///
411 /// # Examples
412 ///
413 /// ```
414 /// use arr_rs::prelude::*;
415 ///
416 /// let expected = Array::flat(vec!["0005".to_string(), "-005".to_string()]);
417 /// let arr = Array::flat(vec!["5".to_string(), "-5".to_string()]);
418 /// assert_eq!(expected, arr.zfill(4));
419 /// ```
420 ///
421 /// # Errors
422 ///
423 /// may returns `ArrayError`
424 fn zfill(&self, width: usize) -> Result<Array<N>, ArrayError>;
425
426 /// Remove elements from `delete_char` vec and translate string remaining characters through `table`
427 ///
428 /// # Arguments
429 ///
430 /// * `table` - chars to replace
431 /// * `delete_chars` - chars to delete
432 ///
433 /// # Examples
434 ///
435 /// ```
436 /// use arr_rs::prelude::*;
437 ///
438 /// let expected = Array::flat(vec!["hewwd".to_string(), "wdrwd".to_string()]);
439 /// let arr = Array::flat(vec!["hello".to_string(), "world".to_string()]);
440 /// assert_eq!(expected, arr.translate(vec![('l', 'w'), ('o', 'd')]));
441 /// ```
442 ///
443 /// # Errors
444 ///
445 /// may returns `ArrayError`
446 fn translate(&self, table: Vec<(char, char)>) -> Result<Array<N>, ArrayError>;
447}
448
449impl <N: Alphanumeric> ArrayStringManipulate<N> for Array<N> {
450
451 fn add(&self, other: &Self) -> Result<Self, ArrayError> {
452 let broadcasted = self.broadcast(other)?;
453 let elements = broadcasted.clone().into_iter()
454 .map(|tuple| tuple.0._append(tuple.1))
455 .collect();
456 Self::new(elements, broadcasted.get_shape()?)
457 }
458
459 fn multiply(&self, counts: &Array<usize>) -> Result<Self, ArrayError> {
460 let (array, counts) = self.broadcast_h2(counts)?;
461 let elements = array.clone().into_iter().zip(counts)
462 .map(|tuple| tuple.0._multiply(tuple.1))
463 .collect();
464 Self::new(elements, array.get_shape()?)
465 }
466
467 fn capitalize(&self) -> Result<Self, ArrayError> {
468 let elements = self.clone().into_iter()
469 .map(|s| s._capitalize())
470 .collect();
471 Self::new(elements, self.get_shape()?)
472 }
473
474 fn lower(&self) -> Result<Self, ArrayError> {
475 let elements = self.clone().into_iter()
476 .map(|s| s._lower())
477 .collect();
478 Self::new(elements, self.get_shape()?)
479 }
480
481 fn upper(&self) -> Result<Self, ArrayError> {
482 let elements = self.clone().into_iter()
483 .map(|s| s._upper())
484 .collect();
485 Self::new(elements, self.get_shape()?)
486 }
487
488 fn swapcase(&self) -> Result<Self, ArrayError> {
489 let elements = self.clone().into_iter()
490 .map(|s| s._swapcase())
491 .collect();
492 Self::new(elements, self.get_shape()?)
493 }
494
495 fn center(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Result<Self, ArrayError> {
496 let fill_char = fill_char.unwrap_or(Array::single(' ')?);
497 let (array, width, fill_char) = self.broadcast_h3(width, &fill_char)?;
498
499 let elements = array.into_iter().enumerate()
500 .map(|(idx, s)| s._center(width[idx], fill_char[idx]))
501 .collect();
502 Self::new(elements, self.get_shape()?)
503 }
504
505 fn join(&self, sep: &Self) -> Result<Self, ArrayError> {
506 let broadcasted = self.broadcast(sep)?;
507 let elements = broadcasted.clone().into_iter()
508 .map(|tuple| tuple.0._join(tuple.1))
509 .collect();
510 Self::new(elements, broadcasted.get_shape()?)
511 }
512
513 fn partition(&self, sep: &Self) -> Result<Array<Tuple3<N, N, N>>, ArrayError> {
514 let broadcasted = self.broadcast(sep)?;
515 let elements = broadcasted.clone().into_iter()
516 .map(|tuple| tuple.0._partition(tuple.1))
517 .collect();
518 Array::new(elements, broadcasted.get_shape()?)
519 }
520
521 fn rpartition(&self, sep: &Self) -> Result<Array<Tuple3<N, N, N>>, ArrayError> {
522 let broadcasted = self.broadcast(sep)?;
523 let elements = broadcasted.clone().into_iter()
524 .map(|tuple| tuple.0._rpartition(tuple.1))
525 .collect();
526 Array::new(elements, broadcasted.get_shape()?)
527 }
528
529 fn split(&self, sep: Option<Self>, max_split: Option<Array<usize>>) -> Result<Array<List<N>>, ArrayError> {
530 let sep = sep.unwrap_or(Self::single(N::from_str(" "))?);
531 let broadcasted = self.broadcast(&sep)?;
532 let max_split =
533 if let Some(counts) = max_split { Some(counts.broadcast_to(broadcasted.get_shape()?)?) }
534 else { None };
535 let elements = broadcasted.clone().into_iter().enumerate()
536 .map(|(idx, tuple)| tuple.0._split(tuple.1, max_split.clone().map(|s| s[idx])))
537 .collect();
538 Array::new(elements, broadcasted.get_shape()?)
539 }
540
541 fn rsplit(&self, sep: Option<Self>, max_split: Option<Array<usize>>) -> Result<Array<List<N>>, ArrayError> {
542 let sep = sep.unwrap_or(Self::single(N::from_str(" "))?);
543 let broadcasted = self.broadcast(&sep)?;
544 let max_split =
545 if let Some(counts) = max_split { Some(counts.broadcast_to(broadcasted.get_shape()?)?) } else { None };
546 let elements = broadcasted.clone().into_iter().enumerate()
547 .map(|(idx, tuple)| tuple.0._rsplit(tuple.1, max_split.clone().map(|s| s[idx])))
548 .collect();
549 Array::new(elements, broadcasted.get_shape()?)
550 }
551
552 fn splitlines(&self, keep_ends: Option<Array<bool>>) -> Result<Array<List<N>>, ArrayError> {
553 let keep_ends = keep_ends.unwrap_or(Array::single(false)?);
554 let (array, keep_ends) = self.broadcast_h2(&keep_ends)?;
555 let elements = array.clone().into_iter().enumerate()
556 .map(|(idx, elem)| elem._splitlines(keep_ends[idx]))
557 .collect();
558 Array::new(elements, array.get_shape()?)
559 }
560
561 fn replace(&self, old: &Self, new: &Self, count: Option<usize>) -> Result<Self, ArrayError> {
562 let broadcasted = Self::broadcast_arrays(vec![self.clone(), old.clone(), new.clone()])?;
563 let tupled = (0..broadcasted[0].len()?).map(|i| Tuple3(
564 broadcasted[0][i].clone(),
565 broadcasted[1][i].clone(),
566 broadcasted[2][i].clone()))
567 .map(|tuple| tuple.0._replace(tuple.1, tuple.2, count))
568 .collect();
569 Self::new(tupled, broadcasted[0].get_shape()?)
570 }
571
572 fn strip(&self, chars: Option<Self>) -> Result<Self, ArrayError> {
573 self.lstrip(chars.clone()).rstrip(chars)
574 }
575
576 fn lstrip(&self, chars: Option<Self>) -> Result<Self, ArrayError> {
577 let chars = chars.unwrap_or(Self::single(N::from_str(" "))?);
578 let broadcasted = self.broadcast(&chars)?;
579 let elements = broadcasted.clone().into_iter()
580 .map(|tuple| tuple.0._lstrip(tuple.1))
581 .collect();
582 Self::new(elements, broadcasted.get_shape()?)
583 }
584
585 fn rstrip(&self, chars: Option<Self>) -> Result<Self, ArrayError> {
586 let chars = chars.unwrap_or(Self::single(N::from_str(" "))?);
587 let broadcasted = self.broadcast(&chars)?;
588 let elements = broadcasted.clone().into_iter()
589 .map(|tuple| tuple.0._rstrip(tuple.1))
590 .collect();
591 Self::new(elements, broadcasted.get_shape()?)
592 }
593
594 fn ljust(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Result<Self, ArrayError> {
595 let fill_char = fill_char.unwrap_or(Array::single(' ')?);
596 let (array, width, fill_char) = self.broadcast_h3(width, &fill_char)?;
597
598 let elements = array.into_iter().enumerate()
599 .map(|(idx, s)| s._ljust(width[idx], fill_char[idx]))
600 .collect();
601 Self::new(elements, self.get_shape()?)
602 }
603
604 fn rjust(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Result<Self, ArrayError> {
605 let fill_char = fill_char.unwrap_or(Array::single(' ')?);
606 let tmp_fill_char = Self::single(N::from_str(" ")).broadcast_to(fill_char.get_shape()?)?;
607 let tmp_width = Self::single(N::from_str(" ")).broadcast_to(width.get_shape()?)?;
608 let broadcasted = Self::broadcast_arrays(vec![self.clone(), tmp_width, tmp_fill_char])?;
609
610 let array = broadcasted[0].clone();
611 let width = width.broadcast_to(array.get_shape()?)?;
612 let fill_char = fill_char.broadcast_to(array.get_shape()?)?;
613
614 let elements = array.into_iter().enumerate()
615 .map(|(idx, s)| s._rjust(width[idx], fill_char[idx]))
616 .collect();
617 Self::new(elements, self.get_shape()?)
618 }
619
620 fn zfill(&self, width: usize) -> Result<Self, ArrayError> {
621 if self.get_elements()?.iter().any(|item| item.to_string().parse::<f64>().is_err()) {
622 return Err(ArrayError::ParameterError { param: "`array`", message: "must contain numeric strings only" })
623 }
624 self.map(|item| {
625 let (mut prefix, mut str) = ("", item.to_string());
626 if str.starts_with('-') {
627 str.remove(0);
628 prefix = "-";
629 }
630 let zeros_len = width - prefix.len();
631 if str.len() < zeros_len {
632 for _ in 0..zeros_len - str.len() { str.insert(0, '0') }
633 }
634 if prefix == "-" { str.insert(0, prefix.chars().next().unwrap()); }
635 N::from_str(&str)
636 })
637 }
638
639 fn translate(&self, table: Vec<(char, char)>) -> Result<Self, ArrayError> {
640 self.map(|item| {
641 let str = item.to_string().chars()
642 .map(|c| table.iter()
643 .find(|(t, _)| c == *t)
644 .map_or(c, |t| t.1))
645 .collect::<String>();
646 N::from_str(&str)
647 })
648 }
649}
650
651impl <N: Alphanumeric> ArrayStringManipulate<N> for Result<Array<N>, ArrayError> {
652
653 fn add(&self, other: &Array<N>) -> Self {
654 self.clone()?.add(other)
655 }
656
657 fn multiply(&self, counts: &Array<usize>) -> Self {
658 self.clone()?.multiply(counts)
659 }
660
661 fn capitalize(&self) -> Self {
662 self.clone()?.capitalize()
663 }
664
665 fn lower(&self) -> Self {
666 self.clone()?.lower()
667 }
668
669 fn upper(&self) -> Self {
670 self.clone()?.upper()
671 }
672
673 fn swapcase(&self) -> Self {
674 self.clone()?.swapcase()
675 }
676
677 fn center(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Self {
678 self.clone()?.center(width, fill_char)
679 }
680
681 fn join(&self, sep: &Array<N>) -> Self {
682 self.clone()?.join(sep)
683 }
684
685 fn partition(&self, sep: &Array<N>) -> Result<Array<Tuple3<N, N, N>>, ArrayError> {
686 ArrayStringManipulate::partition(&self.clone()?, sep)
687 }
688
689 fn rpartition(&self, sep: &Array<N>) -> Result<Array<Tuple3<N, N, N>>, ArrayError> {
690 self.clone()?.rpartition(sep)
691 }
692
693 fn split(&self, sep: Option<Array<N>>, max_split: Option<Array<usize>>) -> Result<Array<List<N>>, ArrayError> {
694 ArrayStringManipulate::split(&self.clone()?, sep, max_split)
695 }
696
697 fn rsplit(&self, sep: Option<Array<N>>, max_split: Option<Array<usize>>) -> Result<Array<List<N>>, ArrayError> {
698 self.clone()?.rsplit(sep, max_split)
699 }
700
701 fn splitlines(&self, keep_ends: Option<Array<bool>>) -> Result<Array<List<N>>, ArrayError> {
702 self.clone()?.splitlines(keep_ends)
703 }
704
705 fn replace(&self, old: &Array<N>, new: &Array<N>, count: Option<usize>) -> Self {
706 self.clone()?.replace(old, new, count)
707 }
708
709 fn strip(&self, chars: Option<Array<N>>) -> Self {
710 self.clone()?.strip(chars)
711 }
712
713 fn lstrip(&self, chars: Option<Array<N>>) -> Self {
714 self.clone()?.lstrip(chars)
715 }
716
717 fn rstrip(&self, chars: Option<Array<N>>) -> Self {
718 self.clone()?.rstrip(chars)
719 }
720
721 fn ljust(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Self {
722 self.clone()?.ljust(width, fill_char)
723 }
724
725 fn rjust(&self, width: &Array<usize>, fill_char: Option<Array<char>>) -> Self {
726 self.clone()?.rjust(width, fill_char)
727 }
728
729 fn zfill(&self, width: usize) -> Self {
730 self.clone()?.zfill(width)
731 }
732
733 fn translate(&self, table: Vec<(char, char)>) -> Self {
734 self.clone()?.translate(table)
735 }
736}