|
| 1 | +use core::mem::MaybeUninit; |
| 2 | + |
| 3 | +use crate::ArrayString; |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +/// Creates a const ArrayString from a str slice. |
| 8 | +pub const fn str<const CAP: usize>(s: &str) -> ArrayString<CAP> { |
| 9 | + assert_capacity_limit_const!(CAP); |
| 10 | + |
| 11 | + let bytes = s.as_bytes(); |
| 12 | + let len = bytes.len(); |
| 13 | + |
| 14 | + // Check that capacity is not exceeded |
| 15 | + if len > CAP { |
| 16 | + panic!("ArrayString: capacity exceeded in const_str"); |
| 17 | + } |
| 18 | + |
| 19 | + let mut xs = [MaybeUninit::<u8>::uninit(); CAP]; |
| 20 | + let mut i = 0; |
| 21 | + while i < len { |
| 22 | + xs[i] = MaybeUninit::new(bytes[i]); |
| 23 | + i += 1; |
| 24 | + } |
| 25 | + |
| 26 | + // Safety: We have initialized `len` bytes in `xs` |
| 27 | + // and ensured that `len <= CAP` before |
| 28 | + // and s is a valid UTF-8 string. |
| 29 | + unsafe { ArrayString::from_raw_parts(xs, len) } |
| 30 | +} |
| 31 | + |
| 32 | +/// Create a const ArrayString from a byte slice. |
| 33 | +pub const fn byte_str<const CAP: usize>(bytes: &[u8]) -> ArrayString<CAP> { |
| 34 | + // for the const_helper feature MSRV 1.63.0 is required |
| 35 | + #[allow(clippy::incompatible_msrv)] |
| 36 | + let Ok(str) = core::str::from_utf8(bytes) else { |
| 37 | + panic!("ArrayString: invalid UTF-8 in const_byte_str"); |
| 38 | + }; |
| 39 | + crate::const_helper::str(str) |
| 40 | +} |
| 41 | + |
| 42 | +/// Creates a const ArrayString from a str slice. |
| 43 | +/// |
| 44 | +/// # Examples |
| 45 | +/// ```rust |
| 46 | +/// use arrayvec::array_str; |
| 47 | +/// // With inferred capacity |
| 48 | +/// const S: arrayvec::ArrayString<5> = array_str!("hello"); |
| 49 | +/// assert_eq!(&S, "hello"); |
| 50 | +/// // With specified capacity |
| 51 | +/// const S2: arrayvec::ArrayString<10> = array_str!("hello", 10); |
| 52 | +/// assert_eq!(&S2, "hello"); |
| 53 | +/// assert_eq!(S2.capacity(), 10); |
| 54 | +/// ``` |
| 55 | +#[macro_export] |
| 56 | +macro_rules! array_str { |
| 57 | + ($str:expr) => { |
| 58 | + $crate::const_helper::str::<{ $str.len() }>($str) |
| 59 | + }; |
| 60 | + ($str:expr, $cap:expr) => { |
| 61 | + $crate::const_helper::str::<$cap>($str) |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +/// Creates a const ArrayString from a byte slice. |
| 66 | +/// |
| 67 | +/// # Examples |
| 68 | +/// ```rust |
| 69 | +/// use arrayvec::array_bstr; |
| 70 | +/// // With inferred capacity |
| 71 | +/// const B: arrayvec::ArrayString<5> = array_bstr!(b"hello"); |
| 72 | +/// assert_eq!(&B, "hello"); |
| 73 | +/// // With specified capacity |
| 74 | +/// const B2: arrayvec::ArrayString<10> = array_bstr!(b"hello", 10); |
| 75 | +/// assert_eq!(&B2, "hello"); |
| 76 | +/// assert_eq!(B2.capacity(), 10); |
| 77 | +/// ``` |
| 78 | +#[macro_export] |
| 79 | +macro_rules! array_bstr { |
| 80 | + ($bstr:expr) => { |
| 81 | + $crate::const_helper::byte_str::<{ $bstr.len() }>($bstr) |
| 82 | + }; |
| 83 | + ($bstr:expr, $cap:expr) => { |
| 84 | + $crate::const_helper::byte_str::<$cap>($bstr) |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn array_str() { |
| 94 | + const S_EMPTY: ArrayString<0> = array_str!(""); |
| 95 | + assert_eq!(&S_EMPTY, ""); |
| 96 | + |
| 97 | + const S_EMPTY_CAP5: ArrayString<5> = array_str!("", 5); |
| 98 | + assert_eq!(&S_EMPTY_CAP5, ""); |
| 99 | + |
| 100 | + const S1: ArrayString<5> = array_str!("hello"); |
| 101 | + assert_eq!(&S1, "hello"); |
| 102 | + |
| 103 | + const S2: ArrayString<10> = array_str!("hello", 10); |
| 104 | + assert_eq!(&S2, "hello"); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn array_bstr() { |
| 109 | + const B_EMPTY: ArrayString<0> = array_bstr!(b""); |
| 110 | + assert_eq!(&B_EMPTY, ""); |
| 111 | + |
| 112 | + const B_EMPTY_CAP5: ArrayString<5> = array_bstr!(b"", 5); |
| 113 | + assert_eq!(&B_EMPTY_CAP5, ""); |
| 114 | + |
| 115 | + const B1: ArrayString<5> = array_bstr!(b"hello"); |
| 116 | + assert_eq!(&B1, "hello"); |
| 117 | + |
| 118 | + const B2: ArrayString<10> = array_bstr!(b"hello", 10); |
| 119 | + assert_eq!(&B2, "hello"); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + #[should_panic] |
| 124 | + fn fail_empty() { |
| 125 | + let _bad_empty = array_str!("hello", 0); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + #[should_panic] |
| 130 | + fn fail_bempty() { |
| 131 | + let _bad_bempty = array_bstr!(b"hello", 0); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + #[should_panic] |
| 136 | + fn fail_cap() { |
| 137 | + let _bad_small = array_str!("hello", 4); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + #[should_panic] |
| 142 | + fn fail_bcap() { |
| 143 | + let _bad_bsmall = array_bstr!(b"hello", 4); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + #[should_panic] |
| 148 | + fn fail_utf8() { |
| 149 | + let _bad_utf8 = array_bstr!(b"\xFF\xFF\xFF", 4); |
| 150 | + } |
| 151 | +} |
0 commit comments