arr_rs/alphanumeric/types/
string.rs1use crate::{
2 alphanumeric::prelude::*,
3 core::prelude::*,
4 extensions::prelude::*,
5};
6use crate::prelude::Numeric;
7
8impl ArrayElement for String {
9
10 fn zero() -> Self {
11 "0".to_string()
12 }
13
14 fn one() -> Self {
15 "1".to_string()
16 }
17
18 fn is_nan(&self) -> bool {
19 false
20 }
21}
22
23impl Alphanumeric for String {
24
25 fn from_str(str: &str) -> Self {
26 str.to_string()
27 }
28
29 fn _append(&self, other: Self) -> Self {
30 let mut result = self.to_string();
31 result.push_str(other.as_str());
32 result
33 }
34
35 fn _multiply(&self, n: usize) -> Self {
36 self.repeat(n)
37 }
38
39 fn _capitalize(&self) -> Self {
40 let mut chars: Vec<char> = self.chars().collect();
41 chars[0] = chars[0].to_uppercase().next().unwrap();
42 chars.into_iter().collect()
43 }
44
45 fn _lower(&self) -> Self {
46 self.to_lowercase()
47 }
48
49 fn _upper(&self) -> Self {
50 self.to_uppercase()
51 }
52
53 fn _swapcase(&self) -> Self {
54 self.chars().map(|c| {
55 if c.is_lowercase() { c.to_uppercase().to_string() }
56 else if c.is_uppercase() { c.to_lowercase().to_string() }
57 else { c.to_string() }
58 }).collect()
59 }
60
61 fn _center(&self, width: usize, fill_char: char) -> Self {
62 if width <= self.len() {
63 self.as_str()[..width].to_string()
64 } else {
65 let char = fill_char.to_string();
66 let diff = (width - self.len()).to_f64() / 2.;
67 format!("{}{}{}", char.repeat(diff.ceil().to_usize()), self, char.repeat(diff.floor().to_usize()))
68 }
69 }
70
71 fn _join(&self, sep: Self) -> Self {
72 self.chars().join(&sep)
73 }
74
75 fn _partition(&self, sep: Self) -> Tuple3<Self, Self, Self> {
76 self.find(&sep).map_or_else(|| Tuple3(self.clone(), Self::new(), Self::new()), |index| {
77 let (before, rest) = self.split_at(index);
78 let (_, after) = rest.split_at(sep.len());
79 Tuple3(before.to_string(), sep, after.to_string())
80 })
81 }
82
83 fn _rpartition(&self, sep: Self) -> Tuple3<Self, Self, Self> {
84 self.rfind(&sep).map_or_else(|| Tuple3(self.clone(), Self::new(), Self::new()), |index| {
85 let (before, rest) = self.split_at(index);
86 let (_, after) = rest.split_at(sep.len());
87 Tuple3(before.to_string(), sep, after.to_string())
88 })
89 }
90
91 fn _split(&self, sep: Self, max_split: Option<usize>) -> List<Self> {
92 let result: Vec<&str> = max_split.map_or_else(
93 || str::split(self, &sep).collect(),
94 |split| self.splitn(split, &sep).collect());
95 List(result.into_iter().map(ToString::to_string).collect())
96 }
97
98 fn _rsplit(&self, sep: Self, max_split: Option<usize>) -> List<Self> {
99 List(self.chars().rev().collect::<Self>()._split(sep, max_split).0.reverse_ext())
100 }
101
102 fn _splitlines(&self, keep_ends: bool) -> List<Self> {
103 let mut text = self.clone();
104 let mut lines: Vec<Self> = Vec::new();
105 let mut i = 0;
106
107 loop {
108 if i >= text.len() { break }
109 if text.chars().nth(i).unwrap() == '\n' || text.chars().nth(i).unwrap() == '\r' {
110 if i + 1 < text.len() && text.chars().nth(i).unwrap() == '\r' && text.chars().nth(i + 1).unwrap() == '\n' {
111 if keep_ends {
112 lines.push(text.drain(0 ..= i + 1).collect());
113 } else {
114 lines.push(text.drain(0..i).collect());
115 text.drain(0..2);
116 }
117 } else if keep_ends {
118 lines.push(text.drain(0 ..= i).collect());
119 } else {
120 lines.push(text.drain(0..i).collect());
121 text.drain(0..1);
122 }
123 i = 0;
124 } else {
125 i += 1;
126 }
127 }
128
129 if !text.is_empty() {
130 lines.push(text.to_string());
131 }
132
133 List(lines)
134 }
135
136 fn _replace(&self, old: Self, new: Self, count: Option<usize>) -> Self {
137 let mut replaced_count = 0;
138 let mut replaced_string = self.clone();
139
140 while let Some(index) = replaced_string.find(old.as_str()) {
141 if count.is_some() && replaced_count >= count.unwrap() {
142 break;
143 }
144
145 replaced_string.replace_range(index..index + old.len(), new.as_str());
146 replaced_count += 1;
147 }
148
149 replaced_string
150 }
151
152 fn _strip(&self, chars: Self) -> Self {
153 self._lstrip(chars.clone())._rstrip(chars)
154 }
155
156 fn _ljust(&self, width: usize, fill_char: char) -> Self {
157 if width <= self.len() {
158 self.as_str()[..width].to_string()
159 } else {
160 format!("{}{}", self, fill_char.to_string().repeat(width - self.len()))
161 }
162 }
163
164 fn _lstrip(&self, chars: Self) -> Self {
165 self.chars().rev().collect::<Self>()._rstrip(chars).chars().rev().collect()
166 }
167
168 fn _rjust(&self, width: usize, fill_char: char) -> Self {
169 if width <= self.len() {
170 self.as_str()[..width].to_string()
171 } else {
172 format!("{}{}", fill_char.to_string().repeat(width - self.len()), self)
173 }
174 }
175
176 fn _rstrip(&self, chars: Self) -> Self {
177 let mut result = self.clone();
178 loop {
179 if result.is_empty() { return result }
180 if chars.contains(result.chars().last().unwrap()) {
181 result.remove(result.len() - 1);
182 } else {
183 return result
184 }
185 }
186 }
187
188 fn _equal(&self, other: Self) -> bool {
189 self._rstrip(" ".to_string()) == other._rstrip(" ".to_string())
190 }
191
192 fn _not_equal(&self, other: Self) -> bool {
193 !self._equal(other)
194 }
195
196 fn _greater_equal(&self, other: Self) -> bool {
197 self._rstrip(" ".to_string()).ge(&other._rstrip(" ".to_string()))
198 }
199
200 fn _less_equal(&self, other: Self) -> bool {
201 self._rstrip(" ".to_string()).le(&other._rstrip(" ".to_string()))
202 }
203
204 fn _greater(&self, other: Self) -> bool {
205 self._rstrip(" ".to_string()).gt(&other._rstrip(" ".to_string()))
206 }
207
208 fn _less(&self, other: Self) -> bool {
209 self._rstrip(" ".to_string()).lt(&other._rstrip(" ".to_string()))
210 }
211
212 fn _count(&self, sub: &str) -> usize {
213 self.match_indices(sub).count()
214 }
215}