front_vec/front_string/
front_string.rs

1use std::{fmt, mem::MaybeUninit, ops::Deref};
2
3use crate::FrontVec;
4
5pub struct FrontString {
6    /// Must always contain valid UTF8 sequence of bytes.
7    buf: FrontVec<u8>,
8}
9
10impl FrontString {
11    pub fn new() -> Self {
12        Self::with_capacity(0)
13    }
14
15    pub fn with_capacity(capacity: usize) -> Self {
16        Self {
17            buf: FrontVec::with_capacity(capacity),
18        }
19    }
20
21    pub fn capacity(&self) -> usize {
22        self.buf.capacity()
23    }
24
25    /// Ensures capacity has at least `additional` more bytes of capacity.
26    ///
27    /// Returns `true` if a reallocation happened, `false` otherwise.
28    pub fn reserve_front(&mut self, additional: usize) -> bool {
29        self.buf.reserve_front(additional)
30    }
31
32    pub fn push_char_front(&mut self, ch: char) {
33        let mut buf = [0; 4];
34        let bytes = ch.encode_utf8(&mut buf).bytes();
35        self.buf.extend_front(bytes);
36    }
37
38    pub fn pop_char_front(&mut self) -> Option<char> {
39        self.chars().next().map(|first_char: char| {
40            // Pop off all of first_char's bytes.
41            for _ in 0..first_char.len_utf8() {
42                // SAFETY:
43                // TODO[safety argument omitted]
44                unsafe {
45                    self.buf.pop_front().unwrap_unchecked();
46                }
47            }
48
49            first_char
50        })
51    }
52
53    pub fn push_str_front<S: AsRef<str>>(&mut self, s: S) {
54        self.buf.extend_front(s.as_ref().bytes());
55    }
56
57    /// Returns a mutable slice that references the uninitialized portion of the
58    /// underlying buffer.
59    pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
60        self.buf.spare_capacity_mut()
61    }
62
63    /// # Safety
64    /// * `new_len` must be less than or equal to `capacity()`.
65    /// * The elements at `old_len..new_len` must be initialized.
66    pub unsafe fn set_len(&mut self, new_len: usize) {
67        unsafe { self.buf.set_len(new_len) }
68    }
69
70    /// Writes the bytes from an `ExactSizeIterator` of bytes onto the front of
71    /// the string.
72    ///
73    /// Returns `true` if a reallocation happened, `false` otherwise.
74    ///
75    /// # Example
76    ///
77    /// ```
78    /// # use front_vec::FrontString;
79    /// # use assert2::assert;
80    /// let mut s = FrontString::from("world!");
81    /// let prefix = "Hello, ";
82    /// s.prepend_from_bytes_iter(prefix.bytes());
83    /// assert!(s == "Hello, world!");
84    /// ```
85    pub fn prepend_from_bytes_iter<Bs>(&mut self, bytes: Bs) -> bool
86    where
87        Bs: ExactSizeIterator<Item = u8>,
88    {
89        let did_realloc = self.reserve_front(bytes.len());
90
91        let bytes_len = bytes.len();
92        let spare = self.spare_capacity_mut();
93        let spare_len = spare.len();
94        let begin_write = spare_len - bytes_len;
95        let reserved_space = &mut spare[begin_write..];
96
97        // MEMORY DIAGRAM:
98        //
99        // [ ???????????????????????????? |  reserved_space | initialized front vec ]
100        // |<- (spare_len - bytes_len) -> |<-- bytes_len -->|<--------- len ------->|
101        // |<-------- spare_len = (cap - len) ------------->|
102        // |<-------------------------------- cap --------------------------------->|
103
104        for (byte, slot) in bytes.zip(reserved_space.iter_mut()) {
105            slot.write(byte);
106        }
107
108        unsafe {
109            self.set_len(self.len() + bytes_len);
110        }
111
112        did_realloc
113    }
114
115    /// Shortens the `FrontString`, keeping the **last** `len` bytes and
116    /// dropping the rest.
117    /// If `len` is greater than the current length, this has no effect.
118    /// Note that this method has no effect on the allocated capacity of the
119    /// `FrontString`.
120    ///
121    /// # Panics
122    /// Panics if `new_len` does not lie on a `char` boundary.
123    pub fn truncate(&mut self, new_len: usize) {
124        let new_len = usize::min(self.len(), new_len);
125
126        if !self.is_char_boundary(new_len) {
127            panic!("new length is not on a char boundary");
128        }
129
130        self.buf.truncate(new_len);
131    }
132}
133
134impl From<&str> for FrontString {
135    fn from(s: &str) -> Self {
136        let mut fs = FrontString::new();
137        fs.push_str_front(s);
138        fs
139    }
140}
141
142impl From<String> for FrontString {
143    fn from(s: String) -> Self {
144        // SAFETY:
145        //  1. A String is valid utf8 bytes.
146        //  2. The Vec<u8> produced from a String contains valid utf8 bytes.
147        //  3. A FrontVec<u8> produced from the Vec<u8> contains valid utf8 bytes.
148        //  4. Therefore Self contains valid utf8 bytes.
149        let byte_vec: Vec<u8> = s.into();
150        Self {
151            buf: byte_vec.into(),
152        }
153    }
154}
155
156impl Deref for FrontString {
157    type Target = str;
158
159    fn deref(&self) -> &Self::Target {
160        let byte_slice = self.buf.as_ref();
161        // SAFETY: Because `self.buf` always contains valid UTF8, this is safe.
162        unsafe { std::str::from_utf8_unchecked(byte_slice) }
163    }
164}
165
166impl AsRef<str> for FrontString {
167    fn as_ref(&self) -> &str {
168        self.deref()
169    }
170}
171
172impl AsRef<[u8]> for FrontString {
173    fn as_ref(&self) -> &[u8] {
174        self.buf.as_ref()
175    }
176}
177
178impl<S: AsRef<str>> PartialEq<S> for FrontString {
179    fn eq(&self, other: &S) -> bool {
180        <Self as AsRef<str>>::as_ref(self) == other.as_ref()
181    }
182}
183
184impl Eq for FrontString {}
185
186impl fmt::Debug for FrontString {
187    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188        let slice: &str = self.as_ref();
189        write!(f, "{slice:?}")
190    }
191}
192
193impl fmt::Display for FrontString {
194    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195        let slice: &str = self.as_ref();
196        write!(f, "{slice}")
197    }
198}
199
200impl Default for FrontString {
201    fn default() -> Self {
202        FrontString::new()
203    }
204}
205
206impl Clone for FrontString {
207    fn clone(&self) -> Self {
208        Self {
209            buf: self.buf.clone(),
210        }
211    }
212}