Skip to main content

cheetah_string/
builder.rs

1use alloc::string::String;
2use core::fmt;
3
4use crate::{CheetahStr, CheetahString};
5
6/// Append-heavy builder for constructing Cheetah string values.
7///
8/// `CheetahBuilder` keeps mutable construction separate from immutable
9/// clone-cheap `CheetahStr` values and stable string values.
10#[derive(Clone, Default)]
11pub struct CheetahBuilder {
12    inner: String,
13}
14
15impl CheetahBuilder {
16    /// Creates an empty builder.
17    #[inline]
18    pub fn new() -> Self {
19        Self {
20            inner: String::new(),
21        }
22    }
23
24    /// Creates an empty builder with at least `capacity` bytes.
25    #[inline]
26    pub fn with_capacity(capacity: usize) -> Self {
27        Self {
28            inner: String::with_capacity(capacity),
29        }
30    }
31
32    /// Creates a builder from existing owned storage.
33    #[inline]
34    pub fn from_string(value: String) -> Self {
35        Self { inner: value }
36    }
37
38    /// Appends a string slice.
39    #[inline]
40    pub fn push_str(&mut self, value: &str) {
41        self.inner.push_str(value);
42    }
43
44    /// Appends a character.
45    #[inline]
46    pub fn push(&mut self, value: char) {
47        self.inner.push(value);
48    }
49
50    /// Reserves capacity for at least `additional` more bytes.
51    #[inline]
52    pub fn reserve(&mut self, additional: usize) {
53        self.inner.reserve(additional);
54    }
55
56    /// Clears the current contents while preserving capacity.
57    #[inline]
58    pub fn clear(&mut self) {
59        self.inner.clear();
60    }
61
62    /// Returns the current contents.
63    #[inline]
64    pub fn as_str(&self) -> &str {
65        self.inner.as_str()
66    }
67
68    /// Returns the current length in bytes.
69    #[inline]
70    pub fn len(&self) -> usize {
71        self.inner.len()
72    }
73
74    /// Returns whether the builder is empty.
75    #[inline]
76    pub fn is_empty(&self) -> bool {
77        self.inner.is_empty()
78    }
79
80    /// Returns the allocated capacity in bytes.
81    #[inline]
82    pub fn capacity(&self) -> usize {
83        self.inner.capacity()
84    }
85
86    /// Finishes into a mutable string value, preserving spare capacity when it
87    /// is useful for subsequent mutation.
88    #[inline]
89    pub fn finish_string(self) -> CheetahString {
90        CheetahString::from_string_owned(self.inner)
91    }
92
93    /// Finishes into an immutable clone-cheap string value.
94    #[inline]
95    pub fn finish_str(self) -> CheetahStr {
96        CheetahStr::from_string(self.inner)
97    }
98
99    /// Returns the owned `String` backing this builder.
100    #[inline]
101    pub fn into_string(self) -> String {
102        self.inner
103    }
104}
105
106impl From<String> for CheetahBuilder {
107    #[inline]
108    fn from(value: String) -> Self {
109        Self::from_string(value)
110    }
111}
112
113impl From<&str> for CheetahBuilder {
114    #[inline]
115    fn from(value: &str) -> Self {
116        let mut builder = Self::with_capacity(value.len());
117        builder.push_str(value);
118        builder
119    }
120}
121
122impl Extend<char> for CheetahBuilder {
123    #[inline]
124    fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) {
125        self.inner.extend(iter);
126    }
127}
128
129impl<'a> Extend<&'a str> for CheetahBuilder {
130    #[inline]
131    fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
132        for item in iter {
133            self.push_str(item);
134        }
135    }
136}
137
138impl fmt::Debug for CheetahBuilder {
139    #[inline]
140    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141        f.debug_struct("CheetahBuilder")
142            .field("value", &self.inner)
143            .field("capacity", &self.inner.capacity())
144            .finish()
145    }
146}