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
use crate::pool::{ self, RawString };
use std::alloc::{ alloc, Layout };
use std::borrow::Cow;
use std::fmt;
use std::mem::MaybeUninit;
use std::ops::{ Range, RangeBounds };
use std::ptr;
use std::slice;
use std::str as std_str;
use std::string::{ self as std_string, String as StdString };
use std::sync::Arc;

pub struct String {
	arc: RawString
}

impl String {
	#[inline]
	pub fn new() -> Self {
		Self { arc: pool::from_str("") }
	}

	#[inline]
	pub fn from_utf8(vec: Vec<u8>) -> Result<Self, std_str::Utf8Error> {
		match std_str::from_utf8(&vec) {
			Ok(_) => {
				let arc = unsafe { pool::from_vec(vec) };
				Ok(Self { arc })
			}
			Err(e) => { Err(e) }
		}
	}

	pub fn from_utf8_slice(s: &[u8]) -> Result<Self, std_str::Utf8Error> {
		match std_str::from_utf8(s) {
			Ok(_) => {
				let arc = unsafe { pool::from_slice(s) };
				Ok(Self { arc })
			}
			Err(e) => { Err(e) }
		}
	}

	pub fn from_utf8_lossy(v: &[u8]) -> Self {
		let arc = match StdString::from_utf8_lossy(v) {
			Cow::Borrowed(s) => { pool::from_str(s) }
			Cow::Owned(s) => {
				// SAFETY: std String is guaranteed to be valid utf8
				unsafe { pool::from_vec(s.into_bytes()) }
			}
		};

		Self { arc }
	}

	pub fn from_utf16(v: &[u16]) -> Result<String, std_string::FromUtf16Error> {
		match StdString::from_utf16(v) {
			Ok(s) => {
				let arc = pool::from_str(&s);
				Ok(Self { arc })
			}
			Err(e) => { Err(e) }
		}
	}

	pub fn from_utf16_lossy(v: &[u16]) -> String {
		let arc = pool::from_str(&StdString::from_utf16_lossy(v));
		Self { arc }
	}

	#[inline]
	pub unsafe fn from_utf8_unchecked(bytes: Vec<u8>) -> String {
		let arc = pool::from_vec(bytes);
		Self { arc }
	}

	#[inline]
	pub fn as_str(&self) -> &str {
		// TODO: Deref impl like std String
		// SAFETY: strings in string pool guaranteed to be valid utf8
		unsafe { std_str::from_utf8_unchecked(&self.arc) }
	}

	#[inline]
	pub fn as_bytes(&self) -> &[u8] {
		&self.arc
	}

	#[inline]
	pub fn len(&self) -> usize {
		self.arc.len()
	}

	#[inline]
	pub fn is_empty(&self) -> bool {
		self.arc.is_empty()
	}

	#[must_use = "e"]
	pub fn truncate(&self, new_len: usize) -> Self {
		if self.len() > new_len {
			assert!(self.as_str().is_char_boundary(new_len));

			let layout = Layout::array::<u8>(new_len).unwrap();
			let new_ptr = unsafe { alloc(layout) };
			unsafe { new_ptr.copy_from_nonoverlapping(self.arc.as_ptr(), new_len) };

			let s = unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(new_ptr, new_len)) };
			let arc = unsafe { pool::from_boxed_slice(s) };
			Self { arc }
		} else {
			self.clone()
		}
	}
}

impl Clone for String {
	fn clone(&self) -> Self {
		Self { arc: Arc::clone(&self.arc) }
	}
}

impl fmt::Debug for String {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Debug::fmt(self.as_str(), f)
	}
}

impl fmt::Display for String {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		fmt::Display::fmt(self.as_str(), f)
	}
}