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
use std::fmt;
use std::slice;
pub struct UString {
inner: Vec<char>,
}
impl UString {
pub fn len_utf8(&self) -> usize {
self.iter().map(|c| c.len_utf8()).sum()
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn iter(&self) -> slice::Iter<'_, char> {
self.inner.iter()
}
pub fn codePointAt(&self, index: usize) -> Option<u32> {
self.inner.get(index).map(|c| *c as u32)
}
pub fn startsWith(&self, other: &Self) -> bool {
self.inner.starts_with(&other.inner)
}
pub fn endsWith(&self, other: &Self) -> bool {
self.inner.ends_with(&other.inner)
}
#[cfg(nightly)]
pub fn repeat(&self, num: usize) -> Self {
Self { inner: self.inner.repeat(num) }
}
#[cfg(not(nightly))]
pub fn repeat(&mut self, num: usize) -> Self {
let mut data = Vec::with_capacity(self.inner.len() * num);
for _ in 0..num {
data.extend_from_slice(&self.inner);
}
Self { inner: data }
}
pub fn indexOf(&self, s: &Self) -> Option<usize> {
let mut idx = 0usize;
while idx < self.len() {
let data = &self.inner[idx..];
if data.starts_with(&s.inner) {
return Some(idx);
}
idx += 1;
}
return None;
}
pub fn toUpperCase(&self) -> Self {
let acc = Vec::with_capacity(self.len());
let acc: Vec<char> = self.iter().fold(acc, |mut acc, c| {
c.to_uppercase().for_each(|c| acc.push(c));
acc
});
Self { inner: acc }
}
pub fn toLowerCase(&self) -> Self {
let acc = Vec::with_capacity(self.len());
let acc: Vec<char> = self.iter().fold(acc, |mut acc, c| {
c.to_uppercase().for_each(|c| acc.push(c));
acc
});
Self { inner: acc }
}
pub fn trimStart(&self) -> Self {
let mut idx = 0usize;
while idx < self.len() {
let c = self.inner[idx];
if c.is_whitespace() {
idx += 1;
} else {
break;
}
}
Self { inner: self.inner[idx..].to_vec() }
}
pub fn trimLeft(&self) -> Self {
self.trimStart()
}
pub fn trimEnd(&self) -> Self {
let mut idx = self.len() - 1;
loop {
let c = self.inner[idx];
if c.is_whitespace() {
if idx == 0 {
break;
}
idx -= 1;
} else {
break;
}
}
Self { inner: self.inner[..idx].to_vec() }
}
pub fn trimRight(&self) -> Self {
self.trimEnd()
}
pub fn trim(&self) -> Self {
self.trimStart().trimEnd()
}
pub fn slice(&self, start: usize, size: usize) -> Self {
if size == 0 {
return Self { inner: vec![] };
}
Self { inner: self.inner[start..size].to_vec() }
}
pub fn replace(&self, s: &Self, replace_with: &Self) -> Self {
match self.indexOf(s) {
Some(idx) => {
let mut data = self.inner[..idx].to_vec();
data.extend_from_slice(&replace_with.inner);
data.extend_from_slice(&self.inner[idx + s.len()..]);
Self { inner: data }
},
None => {
Self { inner: self.inner.clone() }
},
}
}
}
impl fmt::Debug for UString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.inner.iter().collect::<String>())
}
}
impl fmt::Display for UString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inner.iter().collect::<String>())
}
}