use super::InternedStr;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixedString {
contents: String,
}
impl Default for FixedString {
#[inline]
fn default() -> Self {
Self {
contents: String::new(),
}
}
}
impl FixedString {
#[inline]
pub fn with_capacity(cap: usize) -> Self {
Self {
contents: String::with_capacity(cap),
}
}
#[inline]
pub fn finish(self) -> String {
self.contents
}
#[inline]
pub fn capacity(&self) -> usize {
self.contents.capacity()
}
#[inline]
pub fn len(&self) -> usize {
self.contents.len()
}
#[inline]
pub fn push_str(&mut self, string: &str) -> Option<InternedStr> {
let len = self.len();
if self.capacity() < len + string.len() {
return None
}
self.contents.push_str(string);
debug_assert_eq!(self.contents.len(), len + string.len());
Some(InternedStr::new(
unsafe {
core::str::from_utf8_unchecked(
&self.contents.as_bytes()[len..len + string.len()],
)
},
))
}
pub fn shrink_to_fit(&mut self) {
self.contents.shrink_to_fit();
}
}