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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/// Represents a growable UTF-8 encoded string.
pub struct String<A: Allocator = GlobalAllocator> {
buffer: Array<u8, A>,
}
impl<A: Allocator> String<A> {
/// Initialises an empty String with the specified allocator `A`
///
/// ```
/// use base::alloc::GlobalAllocator;
/// use base::string::String;
///
/// fn main() {
/// let s = String::new_with(GlobalAllocator);
/// }
/// ```
pub fn new_with(alloc: A) -> Self {
Self {
buffer: Array::new_with(alloc),
}
}
/// Initialises a `String` from a given `&str` using the specified allocator, `A`.
///
///
/// ```
/// use base::alloc::GlobalAllocator;
/// use base::string::String;
///
/// fn main()
/// {
/// let s = String::from_str_with("hello!", GlobalAllocator);
/// }
/// ```
pub fn from_str_with(s: &str, alloc: A) -> Self {
let slice = s.as_bytes();
let mut buf = Array::new_with(alloc);
buf.resize(slice.len(), 0);
unsafe {
ptr::copy_nonoverlapping(s.as_ptr(), buf.as_mut_ptr(), slice.len());
}
return Self { buffer: buf };
}
/// Dereferences to the base `&str`.
#[inline]
pub fn as_str(&self) -> &str {
self
}
/// Pushes a Unicode character point to the current instance of `String`.
pub fn push(&mut self, c: char) {
let mut bytes = [0u8; 4];
c.encode_utf8(&mut bytes);
self.buffer.extend(bytes[0..c.len_utf8()].iter());
}
}
impl<A: Allocator> TryFrom<Array<u8, A>> for String<A> {
type Error = core::str::Utf8Error;
fn try_from(array: Array<u8, A>) -> Result<Self, Self::Error> {
str::from_utf8(&array)?;
Ok(Self { buffer: array })
}
}
impl String<GlobalAllocator> {
/// Initialises an empty `String`.
///
///
/// ```
/// use base::string::String;
///
/// fn main()
/// {
/// let s = String::new();
/// }
/// ```
pub fn new() -> Self {
Self::new_with(GlobalAllocator)
}
/// Initialises a `String` from a given `&str`.
///
///
/// ```
/// use base::string::String;
///
/// fn main()
/// {
/// let s = String::from("hello!");
/// }
/// ```
pub fn from(s: &str) -> Self {
Self::from_str_with(s, GlobalAllocator)
}
}
impl<A: Allocator> AsRef<str> for String<A> {
/// References the current `String` as a `&str`.
#[inline]
fn as_ref(&self) -> &str {
self
}
}
impl<A: Allocator> Borrow<str> for String<A> {
/// Borrows the current `String` as `&str`.
#[inline]
fn borrow(&self) -> &str {
self
}
}
impl<A: Allocator> Deref for String<A> {
type Target = str;
/// Dereferences the current `String` into a `&str`.
///
///
/// ```no_compile
/// use base::string::String;
///
/// fn main()
/// {
/// let s = String::from("hello!");
/// let s = s.deref();
/// }
/// ```
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { str::from_utf8_unchecked(&self.buffer) }
}
}
unsafe impl Send for String<GlobalAllocator> {}
unsafe impl Sync for String<GlobalAllocator> {}
impl<A: Allocator> DerefMut for String<A> {
/// Dereferences the current `String` into a mutable `&str`.
#[inline]
fn deref_mut(&mut self) -> &mut str {
unsafe { str::from_utf8_unchecked_mut(&mut self.buffer) }
}
}
impl<A: Allocator> fmt::Display for String<A> {
// allows println!() to work
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl<A: Allocator> fmt::Debug for String<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl<A, T> PartialEq<T> for String<A>
where
A: Allocator,
T: AsRef<str>,
{
#[inline]
fn eq(&self, other: &T) -> bool {
PartialEq::eq(self.as_str(), other.as_ref())
}
}
impl<A: Allocator> Eq for String<A> {}
impl<A: Allocator> Hash for String<A> {
fn hash<H: Hasher>(&self, h: &mut H) {
Hash::hash(self.as_str(), h);
}
}
//------------------------------------------------------------
//StringWide: A growable UTF-16 string.
/// Represents a growable, UTF-16-encoded String.
pub struct StringWide<A: Allocator = GlobalAllocator> {
buf: Array<u16, A>,
}
impl<A: Allocator> StringWide<A> {
/// Initialises an empty `StringWide` with the specified allocator, `A`.
///
///
/// ```no_run
/// use base::alloc::GlobalAllocator;
/// use base::string::StringWide;
///
/// fn main() {
/// let s = StringWide::new();
/// }
/// ```
pub fn new_with(alloc: A) -> Self {
Self {
buf: Array::new_with(alloc),
}
}
/// Initialises a `StringWide` from the given `&str` using the specified allocator, `A`.
///
///
/// ```no_run
/// use base::alloc::GlobalAllocator;
/// use base::string::StringWide;
///
/// fn main()
/// {
/// let s = StringWide::from_str_with("hello!", GlobalAllocator);
/// }
/// ```
pub fn from_str_with(s: &str, alloc: A) -> Self {
let w_iter = s.encode_utf16();
let mut buf = Array::new_with(alloc);
buf.reserve(w_iter.size_hint().0);
for wchar in w_iter {
buf.push(wchar);
}
Self { buf }
}
/// See `String::push`.
#[inline]
pub fn push(&mut self, c: char) {
let len = c.len_utf16();
self.buf.resize(self.buf.len() + len, 0);
let start = self.buf.len() - len;
c.encode_utf16(&mut self.buf[start..]);
}
}
impl StringWide<GlobalAllocator> {
/// Initialises an empty `StringWide`.
///
///
/// ```
/// use base::string::StringWide;
///
/// fn main() {
/// let s = StringWide::new();
/// }
/// ```
pub fn new() -> Self {
Self::new_with(GlobalAllocator)
}
/// Initialises a `StringWide` from the given `&str`.
///
///
/// ```
/// use base::string::StringWide;
///
/// fn main()
/// {
/// let s = StringWide::from("hello!");
/// }
/// ```
pub fn from(s: &str) -> Self {
Self::from_str_with(s, GlobalAllocator)
}
}
impl<A: Allocator> AsRef<[u16]> for StringWide<A> {
#[inline]
fn as_ref(&self) -> &[u16] {
&self.buf
}
}
impl<A: Allocator> Deref for StringWide<A> {
type Target = [u16];
#[inline]
fn deref(&self) -> &[u16] {
&self.buf
}
}
impl<A: Allocator> DerefMut for StringWide<A> {
#[inline]
fn deref_mut(&mut self) -> &mut [u16] {
&mut self.buf
}
}
unsafe impl Send for StringWide<GlobalAllocator> {}
unsafe impl Sync for StringWide<GlobalAllocator> {}
// IMPORTS //
use {
crate::{
alloc::{Allocator, GlobalAllocator},
array::Array,
},
core::{
borrow::Borrow,
cmp::{Eq, PartialEq},
fmt,
hash::{Hash, Hasher},
ops::{Deref, DerefMut},
ptr, str,
},
};