summaryrefslogtreecommitdiff
path: root/bzipper/src/fixed_string/mod.rs
blob: 377937a14f92d8cb3028522bcae0d2a08c0e5769 (plain) (blame)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright 2024 Gabriel Bjørnager Jensen.
//
// This file is part of bzipper.
//
// bzipper is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// Lesser General Public License as published by
// the Free Software Foundation, either version 3
// of the License, or (at your option) any later
// version.
//
// bzipper is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Less-
// er General Public License along with bzipper. If
// not, see <https://www.gnu.org/licenses/>.

#[cfg(test)]
mod test;

use crate::{
	Deserialise,
	Dstream,
	Error,
	Serialise,
	Sstream,
};

use core::borrow::{Borrow, BorrowMut};
use core::cmp::Ordering;
use core::fmt::{Debug, Display, Formatter};
use core::hash::{Hash, Hasher};
use core::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut};
use core::slice::SliceIndex;
use core::str::{Chars, CharIndices, FromStr};

#[cfg(feature = "alloc")]
use alloc::string::String;

#[cfg(feature = "std")]
use std::ffi::OsStr;

#[cfg(feature = "std")]
use std::net::ToSocketAddrs;

#[cfg(feature = "std")]
use std::path::Path;

/// Heap-allocated string with maximum size.
///
/// This is in contrast to [String] -- which has no size limit in practice -- and [str], which is unsized.
///
/// The string itself is encoded in UTF-8 for interoperability wtih Rust's standard string facilities, as well as for memory concerns.
///
/// Keep in mind that the size limit specified by `N` denotes *bytes* and not *characters* -- i.e. a value of `8` may translate to between two and eight characters, depending on their codepoints.
///
/// # Examples
///
/// All instances of this type have the same size if the value of `N` is also the same.
/// Therefore, the following four strings have -- despite their different contents -- the same total size.
///
/// ```rust
/// use bzipper::FixedString;
/// use std::str::FromStr;
///
/// let str0 = FixedString::<0x40>::new(); // Empty string.
/// let str1 = FixedString::<0x40>::from_str("Hello there!").unwrap();
/// let str2 = FixedString::<0x40>::from_str("أنا من أوروپا").unwrap();
/// let str3 = FixedString::<0x40>::from_str("COGITO ERGO SUM").unwrap();
///
/// assert_eq!(size_of_val(&str0), size_of_val(&str1));
/// assert_eq!(size_of_val(&str0), size_of_val(&str2));
/// assert_eq!(size_of_val(&str0), size_of_val(&str3));
/// assert_eq!(size_of_val(&str1), size_of_val(&str2));
/// assert_eq!(size_of_val(&str1), size_of_val(&str3));
/// assert_eq!(size_of_val(&str2), size_of_val(&str3));
/// ```
///
/// These three strings can -- by extend in theory -- also interchange their contents between each other.
#[derive(Clone)]
pub struct FixedString<const N: usize> {
	buf: [u8; N],
	len: usize,
}

impl<const N: usize> FixedString<N> {
	/// Constructs a new, fixed-size string.
	///
	/// Note that it is only the internal buffer that is size-constrained.
	/// The string internally keeps track of the amount of used characters and acts accordingly.
	/// One must therefore only see the value of `N` as a size *limit*.
	///
	/// The constructed string will have a null length.
	/// All characters inside the internal buffer are instanced as `U+0000 NULL`.
	///
	/// For constructing a string with an already defined buffer, see [`from_raw_parts`](Self::from_raw_parts) and [`from_str`](Self::from_str).
	#[inline(always)]
	#[must_use]
	pub const fn new() -> Self { Self { buf: [0x00; N], len: 0x0 } }

	/// Constructs a new, fixed-size string from raw parts.
	///
	/// The provided parts are not tested in any way.
	///
	/// # Safety
	///
	/// The value of `len` may not exceed that of `N`.
	/// Additionally, the octets in `buf` (from index zero up to the value of `len`) must be valid UTF-8 codepoints.
	///
	/// If any of these requirements are violated, behaviour is undefined.
	#[inline(always)]
	#[must_use]
	pub const unsafe fn from_raw_parts(buf: [u8; N], len: usize) -> Self { Self { buf, len } }

	/// Destructs the provided string into its raw parts.
	///
	/// The returned values are valid to pass on to [`from_raw_parts`](Self::from_raw_parts).
	///
	/// The returned byte array is guaranteed to be fully initialised.
	/// However, only octets up to an index of [`len`](Self::len) are also guaranteed to be valid UTF-8 codepoints.
	#[inline(always)]
	#[must_use]
	pub const fn into_raw_parts(self) -> ([u8; N], usize) { (self.buf, self.len) }

	/// Gets a pointer to the first octet.
	#[inline(always)]
	#[must_use]
	pub const fn as_ptr(&self) -> *const u8 { self.buf.as_ptr() }

	// This function can only be marked as `const` when
	// `const_mut_refs` is implemented. See tracking
	// issue #57349 for more information.
	/// Gets a mutable pointer to the first octet.
	///
	#[inline(always)]
	#[must_use]
	pub fn as_mut_ptr(&mut self) -> *mut u8 { self.buf.as_mut_ptr() }

	/// Borrows the string as a byte slice.
	///
	/// The range of the returned slice only includes characters that are "used."
	#[inline(always)]
	#[must_use]
	pub const fn as_bytes(&self) -> &[u8] {
		// We need to use `from_raw_parts` to mark this
		// function `const`.

		unsafe { core::slice::from_raw_parts(self.as_ptr(), self.len()) }
	}

	/// Borrows the string as a string slice.
	///
	/// The range of the returned slice only includes characters that are "used."
	#[inline(always)]
	#[must_use]
	pub const fn as_str(&self) -> &str { unsafe { core::str::from_utf8_unchecked(self.as_bytes()) } }

	/// Mutably borrows the string as a string slice.
	///
	/// The range of the returned slice only includes characters that are "used."
	#[inline(always)]
	#[must_use]
	pub fn as_mut_str(&mut self) -> &mut str {
		let range = 0x0..self.len();

		unsafe { core::str::from_utf8_unchecked_mut(&mut self.buf[range]) }
	}

	/// Returns the length of the string.
	///
	/// This does not necessarily equate to the value of `N`, as the internal buffer may be used but partially.
	#[inline(always)]
	#[must_use]
	pub const fn len(&self) -> usize { self.len }

	/// Checks if the string is empty, i.e. no characters are contained.
	#[inline(always)]
	#[must_use]
	pub const fn is_empty(&self) -> bool { self.len() == 0x0 }

	/// Checks if the string is full, i.e. it cannot hold any more characters.
	#[inline(always)]
	#[must_use]
	pub const fn is_full(&self) -> bool { self.len() == N }

	/// Returns the total capacity of the string.
	///
	/// This is defined as being exactly the value of `N`.
	#[inline(always)]
	#[must_use]
	pub const fn capacity(&self) -> usize { N }

	/// Gets a substring of the string.
	#[inline(always)]
	#[must_use]
	pub fn get<I: SliceIndex<str>>(&self, index: I) -> Option<&I::Output> { self.as_str().get(index) }

	/// Gets a mutable substring of the string.
	#[inline(always)]
	#[must_use]
	pub fn get_mut<I: SliceIndex<str>>(&mut self, index: I) -> Option<&mut I::Output> { self.as_mut_str().get_mut(index) }

	/// Pushes a character into the string.
	///
	/// The internal length is updated accordingly.
	///
	/// # Panics
	///
	/// If the string cannot hold the provided character *after* encoding, this method will panic.
	#[inline(always)]
	pub fn push(&mut self, c: char) {
		let mut buf = [0x00; 0x4];
		let s = c.encode_utf8(&mut buf);

		self.push_str(s);
	}

	/// Pushes a string slice into the string.
	///
	/// The internal length is updated accordingly.
	///
	/// # Panics
	///
	/// If the string cannot hold the provided slice, this method will panic.
	#[inline(always)]
	pub fn push_str(&mut self, s: &str) {
		let rem = self.buf.len() - self.len;
		let req = s.len();

		assert!(rem >= req, "cannot push string beyond fixed length");

		let start = self.len;
		let stop  = start + req;

		let buf = &mut self.buf[start..stop];
		buf.copy_from_slice(s.as_bytes());
	}

	/// Pops a character from the string.
	///
	/// The internal length is updated accordingly.
	///
	/// If no characters are left (i.e. the string is empty), an instance of [`None`] is returned.
	///
	/// **Note that this method is currently unimplemented.**
	#[deprecated = "temporarily unimplemented"]
	#[inline(always)]
	pub fn pop(&mut self) -> Option<char> { todo!() }

	/// Returns an iterator of the string's characters.
	#[inline(always)]
	pub fn chars(&self) -> Chars { self.as_str().chars() }

	/// Returns an iterator of the string's characters along with their positions.
	#[inline(always)]
	pub fn char_indices(&self) -> CharIndices { self.as_str().char_indices() }
}

impl<const N: usize> Add<&str> for FixedString<N> {
	type Output = Self;

	fn add(mut self, rhs: &str) -> Self::Output {
		self.push_str(rhs);
		self
	}
}

impl<const N: usize> AddAssign<&str> for FixedString<N> {
	fn add_assign(&mut self, rhs: &str) { self.push_str(rhs) }
}

impl<const N: usize> AsMut<str> for FixedString<N> {
	#[inline(always)]
	fn as_mut(&mut self) -> &mut str { self.as_mut_str() }
}

#[cfg(feature = "std")]
#[cfg_attr(doc, doc(cfg(feature = "std")))]
impl<const N: usize> AsRef<OsStr> for FixedString<N> {
	#[inline(always)]
	fn as_ref(&self) -> &OsStr { self.as_str().as_ref() }
}

#[cfg(feature = "std")]
#[cfg_attr(doc, doc(cfg(feature = "std")))]
impl<const N: usize> AsRef<Path> for FixedString<N> {
	#[inline(always)]
	fn as_ref(&self) -> &Path { self.as_str().as_ref() }
}

impl<const N: usize> AsRef<str> for FixedString<N> {
	#[inline(always)]
	fn as_ref(&self) -> &str { self.as_str() }
}

impl<const N: usize> AsRef<[u8]> for FixedString<N> {
	#[inline(always)]
	fn as_ref(&self) -> &[u8] { self.as_bytes() }
}

impl<const N: usize> Borrow<str> for FixedString<N> {
	#[inline(always)]
	fn borrow(&self) -> &str { self.as_str() }
}

impl<const N: usize> BorrowMut<str> for FixedString<N> {
	#[inline(always)]
	fn borrow_mut(&mut self) -> &mut str { self.as_mut_str() }
}

impl<const N: usize> Debug for FixedString<N> {
	#[inline]
	fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { Debug::fmt(self.as_str(), f) }
}

impl<const N: usize> Default for FixedString<N> {
	#[inline(always)]
	fn default() -> Self { Self { buf: [Default::default(); N], len: 0x0 } }
}

impl<const N: usize> Deref for FixedString<N> {
	type Target = str;

	#[inline(always)]
	fn deref(&self) -> &Self::Target { self.as_str() }
}

impl<const N: usize> DerefMut for FixedString<N> {
	#[inline(always)]
	fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut_str() }
}

impl<const N: usize> Deserialise for FixedString<N> {
	#[inline]
	fn deserialise(stream: &Dstream) -> Result<Self, Error> {
		let len = Deserialise::deserialise(stream)?;
		if len > N { return Err(Error::ArrayTooShort { req: len, len: N }) };

		let bytes = stream.read(len)?;

		let s = core::str::from_utf8(bytes)
			.map_err(|e| Error::BadString { source: e })?;

		Self::from_str(s)
	}
}

impl<const N: usize> Display for FixedString<N> {
	#[inline]
	fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { Display::fmt(self.as_str(), f) }
}

impl<const N: usize> Eq for FixedString<N> { }

impl<const N: usize> FromStr for FixedString<N> {
	type Err = Error;

	#[inline]
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let len = s.len();
		if len > N { return Err(Error::ArrayTooShort { req: len, len: N }) };

		let mut buf = [0x00; N];
		unsafe { core::ptr::copy_nonoverlapping(s.as_ptr(), buf.as_mut_ptr(), len) };

		// The remaining bytes are already initialised to
		// null.

		Ok(Self { buf, len })
	}
}

impl<const N: usize> Hash for FixedString<N> {
	#[inline(always)]
	fn hash<H: Hasher>(&self, state: &mut H) { self.as_str().hash(state) }
}

impl<I: SliceIndex<str>, const N: usize> Index<I> for FixedString<N> {
	type Output	= I::Output;

	#[inline(always)]
	fn index(&self, index: I) -> &Self::Output { self.get(index).unwrap() }
}

impl<I: SliceIndex<str>, const N: usize> IndexMut<I> for FixedString<N> {
	#[inline(always)]
	fn index_mut(&mut self, index: I) -> &mut Self::Output { self.get_mut(index).unwrap() }
}

impl<const N: usize> Ord for FixedString<N> {
	#[inline(always)]
	fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) }
}

impl<const N: usize, const M: usize> PartialEq<FixedString<M>> for FixedString<N> {
	#[inline(always)]
	fn eq(&self, other: &FixedString<M>) -> bool { self.as_str() == other.as_str() }
}

impl<const N: usize> PartialEq<&str> for FixedString<N> {
	#[inline(always)]
	fn eq(&self, other: &&str) -> bool { self.as_str() == *other }
}

impl<const N: usize, const M: usize> PartialOrd<FixedString<M>> for FixedString<N> {
	#[inline(always)]
	fn partial_cmp(&self, other: &FixedString<M>) -> Option<Ordering> { self.as_str().partial_cmp(other.as_str()) }
}

impl<const N: usize> PartialOrd<&str> for FixedString<N> {
	#[inline(always)]
	fn partial_cmp(&self, other: &&str) -> Option<Ordering> { self.as_str().partial_cmp(*other) }
}

impl<const N: usize> Serialise for FixedString<N> {
	const MAX_SERIALISED_SIZE: usize = N + usize::MAX_SERIALISED_SIZE;

	fn serialise(&self, stream: &mut Sstream) -> Result<(), Error> {
		self.len().serialise(stream)?;
		stream.write(self.as_bytes())?;

		Ok(())
	}
}

#[cfg(feature = "std")]
#[cfg_attr(doc, doc(cfg(feature = "std")))]
impl<const N: usize> ToSocketAddrs for FixedString<N> {
	type Iter = <str as ToSocketAddrs>::Iter;

	#[inline(always)]
	fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> { self.as_str().to_socket_addrs() }
}

impl<const N: usize> TryFrom<char> for FixedString<N> {
	type Error = <Self as FromStr>::Err;

	#[inline(always)]
	fn try_from(value: char) -> Result<Self, Self::Error> {
		let mut buf = [0x00; 0x4];
		let s = value.encode_utf8(&mut buf);

		s.parse()
	}
}

impl<const N: usize> TryFrom<&str> for FixedString<N> {
	type Error = <Self as FromStr>::Err;

	#[inline(always)]
	fn try_from(value: &str) -> Result<Self, Self::Error> { Self::from_str(value) }
}

#[cfg(feature = "alloc")]
#[cfg_attr(doc, doc(cfg(feature = "alloc")))]
impl<const N: usize> TryFrom<String> for FixedString<N> {
	type Error = <Self as FromStr>::Err;

	#[inline(always)]
	fn try_from(value: String) -> Result<Self, Self::Error> { Self::from_str(&value) }
}

/// Converts the fixed-size string into a dynamic string.
///
/// The capacity of the resulting [`String`] object is equal to the value of `N`.
#[cfg(feature = "alloc")]
#[cfg_attr(doc, doc(cfg(feature = "alloc")))]
impl<const N: usize> From<FixedString<N>> for String {
	#[inline(always)]
	fn from(value: FixedString<N>) -> Self {
		let mut s = Self::with_capacity(N);
		s.push_str(value.as_str());

		s
	}
}