const_panic/
array_string.rs

1use crate::{
2    utils::{bytes_up_to, RangedBytes},
3    FmtArg, PanicFmt, PanicVal,
4};
5
6use core::{
7    cmp::PartialEq,
8    fmt::{self, Debug},
9};
10
11/// For precomputing a panic message.
12///
13#[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
14#[derive(Copy, Clone)]
15pub struct ArrayString<const CAP: usize> {
16    pub(crate) len: u32,
17    pub(crate) buffer: [u8; CAP],
18}
19
20/// Equivalent of `ArrayString` which can only be up to 255 bytes long.
21///
22/// This stores the length as a `u8`, while `ArrayString` stores it as a `u32`,
23/// making this 3 bytes smaller and 1-aligned (while ArrayString is aligned to a `u32`).
24#[derive(Copy, Clone)]
25pub(crate) struct TinyString<const CAP: usize> {
26    len: u8,
27    buffer: [u8; CAP],
28}
29
30const fn add_up_lengths(mut strings: &[&str]) -> usize {
31    let mut len = 0;
32    while let [x, rem @ ..] = strings {
33        len += x.len();
34        strings = rem;
35    }
36    len
37}
38
39impl<const CAP: usize> ArrayString<CAP> {
40    /// Constructs an `ArrayString` from a `&str`
41    ///
42    /// # Panics
43    ///
44    /// Panics if `string` is larger than `CAP`.
45    ///
46    /// # Example
47    ///
48    /// ```rust
49    /// use const_panic::ArrayString;
50    ///
51    /// assert_eq!(ArrayString::<16>::new("Hello, world!"), "Hello, world!");
52    /// ```
53    pub const fn new(string: &str) -> Self {
54        Self::concat(&[string])
55    }
56
57    /// Constructs an `ArrayString` by concatenating zero or more `&str`s
58    ///
59    /// # Panics
60    ///
61    /// Panics if the concatenated string would be longer than `CAP`.
62    ///
63    /// # Example
64    ///
65    /// ```rust
66    /// use const_panic::ArrayString;
67    ///
68    /// assert_eq!(
69    ///     ArrayString::<99>::concat(&["This ", "is ", "a string"]),
70    ///     "This is a string"
71    /// );
72    /// ```
73    pub const fn concat(strings: &[&str]) -> Self {
74        let mut len = 0u32;
75        let mut buffer = [0u8; CAP];
76
77        let mut mstrings = strings;
78        while let [string, ref rem @ ..] = *mstrings {
79            mstrings = rem;
80            let mut bytes = string.as_bytes();
81            while let [x, ref rem @ ..] = *bytes {
82                if len == u32::MAX || len as usize >= CAP {
83                    crate::concat_panic(&[&[
84                        PanicVal::write_str("The input strings were longer than "),
85                        PanicVal::from_usize(CAP, FmtArg::DISPLAY),
86                        PanicVal::write_str(", concatenated length: "),
87                        PanicVal::from_usize(add_up_lengths(strings), FmtArg::DISPLAY),
88                        PanicVal::write_str(", strings: "),
89                        PanicVal::from_slice_str(strings, FmtArg::DEBUG),
90                    ]])
91                }
92
93                bytes = rem;
94                buffer[len as usize] = x;
95                len += 1;
96            }
97        }
98
99        Self { len, buffer }
100    }
101
102    /// Constructs this string from a `&[&[PanicVal<'_>]]`.
103    ///
104    /// Returns `None` if the formatted args would be larger than `CAP`.
105    ///
106    /// # Example
107    ///
108    /// ```rust
109    /// use const_panic::{ArrayString, FmtArg, flatten_panicvals};
110    ///
111    /// assert_eq!(
112    ///     ArrayString::<17>::concat_panicvals(&[
113    ///         &flatten_panicvals!(FmtArg::DEBUG; 1u8, ("hello")),
114    ///         &flatten_panicvals!(FmtArg::DEBUG; &[3u8, 5, 8]),
115    ///     ]).unwrap(),
116    ///     "1\"hello\"[3, 5, 8]",
117    /// );
118    ///
119    /// assert!(
120    ///     ArrayString::<16>::concat_panicvals(&[
121    ///         &flatten_panicvals!(FmtArg::DEBUG; 1u8, ("hello")),
122    ///         &flatten_panicvals!(FmtArg::DEBUG; &[3u8, 5, 8]),
123    ///     ]).is_none(),
124    /// );
125    ///
126    /// ```    
127    ///
128    pub const fn concat_panicvals(args: &[&[PanicVal<'_>]]) -> Option<Self> {
129        match crate::concat_panic_::make_panic_string::<CAP>(args) {
130            Ok(x) => Some(x),
131            Err(_) => None,
132        }
133    }
134
135    /// Constructs this string from a `&[PanicVal<'_>]`.
136    ///
137    /// Returns `None` if the formatted args would be larger than `CAP`.
138    ///
139    /// # Example
140    ///
141    /// ```rust
142    /// use const_panic::{ArrayString, FmtArg, flatten_panicvals};
143    ///
144    /// assert_eq!(
145    ///     ArrayString::<8>::from_panicvals(
146    ///         &flatten_panicvals!(FmtArg::DEBUG; 100u8, "hello")
147    ///     ).unwrap(),
148    ///     "100hello",
149    /// );
150    ///
151    /// // trying to format panicvals into too small an ArrayString
152    /// assert!(
153    ///     ArrayString::<7>::from_panicvals(
154    ///         &flatten_panicvals!(FmtArg::DEBUG; 100u8, "hello")
155    ///     ).is_none(),
156    /// );
157    ///
158    /// ```
159    pub const fn from_panicvals(args: &[PanicVal<'_>]) -> Option<Self> {
160        Self::concat_panicvals(&[args])
161    }
162
163    /// How long the string is in bytes.
164    ///
165    /// # Example
166    ///
167    /// ```rust
168    /// use const_panic::ArrayString;
169    ///
170    /// assert_eq!(ArrayString::<16>::new("foo").len(), 3);
171    /// assert_eq!(ArrayString::<16>::new("foo bar").len(), 7);
172    /// assert_eq!(ArrayString::<16>::new("Hello, world!").len(), 13);
173    /// ```
174    pub const fn len(&self) -> usize {
175        self.len as usize
176    }
177
178    /// Accesses the string as a byte slice.
179    ///
180    /// # Performance
181    ///
182    /// When the "rust_1_64" feature is disabled,
183    /// this takes a linear amount of time to run, proportional to `CAP - self.len()`.
184    ///
185    /// When the "rust_1_64" feature is enabled,
186    /// this takes a constant amount of time to run.
187    ///
188    /// # Example
189    ///
190    /// ```rust
191    /// use const_panic::ArrayString;
192    ///
193    /// assert_eq!(ArrayString::<16>::new("foo").as_bytes(), b"foo");
194    /// assert_eq!(ArrayString::<16>::new("foo bar").as_bytes(), b"foo bar");
195    /// assert_eq!(ArrayString::<16>::new("Hello, world!").as_bytes(), b"Hello, world!");
196    /// ```
197    pub const fn as_bytes(&self) -> &[u8] {
198        bytes_up_to(&self.buffer, self.len())
199    }
200
201    /// Gets the string.
202    ///
203    /// # Performance
204    ///
205    /// This takes a linear amount of time to run.
206    ///
207    /// # Example
208    ///
209    /// ```rust
210    /// use const_panic::ArrayString;
211    ///
212    /// assert_eq!(ArrayString::<16>::new("foo").to_str(), "foo");
213    /// assert_eq!(ArrayString::<16>::new("foo bar").to_str(), "foo bar");
214    /// assert_eq!(ArrayString::<16>::new("Hello, world!").to_str(), "Hello, world!");
215    /// ```
216    pub const fn to_str(&self) -> &str {
217        #[cfg(not(feature = "rust_1_64"))]
218        {
219            // safety: make_panic_string delegates formatting to the `write_to_buffer` macro,
220            // which is tested as producing valid utf8.
221            unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
222        }
223
224        #[cfg(feature = "rust_1_64")]
225        match core::str::from_utf8(self.as_bytes()) {
226            Ok(x) => x,
227            Err(_) => panic!("INTERNAL BUG: the string isn't valid utf-8"),
228        }
229    }
230
231    /// Creates a single element `PanicVal` borrowing from this string.
232    pub const fn to_panicvals(&self, f: FmtArg) -> [PanicVal<'_>; 1] {
233        [PanicVal::from_str(self.to_str(), f)]
234    }
235
236    /// Creates a `PanicVal` borrowing from this string.
237    pub const fn to_panicval(&self, f: FmtArg) -> PanicVal<'_> {
238        PanicVal::from_str(self.to_str(), f)
239    }
240}
241
242impl<const CAP: usize> ArrayString<CAP> {
243    pub(crate) const fn to_compact(self) -> TinyString<CAP> {
244        if self.len() <= 255 {
245            TinyString {
246                len: self.len as u8,
247                buffer: self.buffer,
248            }
249        } else {
250            crate::concat_panic(&[&[
251                PanicVal::write_str(
252                    "The input string is too long, expected `length <= 255`, found length: ",
253                ),
254                PanicVal::from_usize(self.len(), FmtArg::DISPLAY),
255            ]])
256        }
257    }
258}
259
260impl<const CAP: usize> Debug for ArrayString<CAP> {
261    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
262        Debug::fmt(self.to_str(), f)
263    }
264}
265
266impl<const CAP: usize> PartialEq<str> for ArrayString<CAP> {
267    fn eq(&self, str: &str) -> bool {
268        self.to_str() == str
269    }
270}
271impl<const CAP: usize> PartialEq<&str> for ArrayString<CAP> {
272    fn eq(&self, str: &&str) -> bool {
273        self == *str
274    }
275}
276
277impl<const CAP: usize> PanicFmt for ArrayString<CAP> {
278    type This = Self;
279    type Kind = crate::fmt::IsCustomType;
280    const PV_COUNT: usize = 1;
281}
282
283////////////////////////////////////////////////////////////////////////////////
284
285impl<const CAP: usize> TinyString<CAP> {
286    pub(crate) const fn ranged(&self) -> RangedBytes<&[u8]> {
287        RangedBytes {
288            start: 0,
289            end: self.len as usize,
290            bytes: &self.buffer,
291        }
292    }
293}