front_vec/front_string/
front_string.rs1use std::{fmt, mem::MaybeUninit, ops::Deref};
2
3use crate::FrontVec;
4
5pub struct FrontString {
6 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 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 for _ in 0..first_char.len_utf8() {
42 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 pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
60 self.buf.spare_capacity_mut()
61 }
62
63 pub unsafe fn set_len(&mut self, new_len: usize) {
67 unsafe { self.buf.set_len(new_len) }
68 }
69
70 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 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 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 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 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}