const_panic/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! Utility functions

use crate::debug_str_fmt::ForEscaping;

#[cfg(feature = "rust_1_64")]
#[cfg(test)]
mod utils_1_64_tests;

#[cfg(feature = "non_basic")]
mod non_basic_utils;

#[cfg(feature = "non_basic")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "non_basic")))]
pub use self::non_basic_utils::*;

/// Computes the minimum of `l` and `r`
pub const fn min_usize(l: usize, r: usize) -> usize {
    if l < r {
        l
    } else {
        r
    }
}

/// Computes the maximum of `l` and `r`
pub const fn max_usize(l: usize, r: usize) -> usize {
    if l > r {
        l
    } else {
        r
    }
}

#[derive(Copy, Clone)]
pub(crate) struct TailShortString<const LEN: usize> {
    start: u8,
    buffer: [u8; LEN],
}

pub(crate) type PreFmtString = TailShortString<{ string_cap::PREFMT }>;

pub(crate) mod string_cap {
    /// The capacity of a [`ShortString`](crate::fmt::ShortString).
    #[cfg(feature = "non_basic")]
    pub const TINY: usize = 16;

    // the TailShortString that's stored in PanicVal
    pub(crate) const PREFMT: usize = 21;

    // length of string to alternate binary format a 64 bit integer
    pub(crate) const MEDIUM: usize = 66;

    // length of string to alternate binary format a 128 bit integer
    pub(crate) const LARGE: usize = 130;
}

impl<const LEN: usize> TailShortString<LEN> {
    ///
    /// # Safety
    ///
    /// `buffer` must be valid utf8 starting from the `start` index.
    #[inline(always)]
    pub(crate) const unsafe fn new(start: u8, buffer: [u8; LEN]) -> Self {
        Self { start, buffer }
    }

    pub(crate) const fn len(&self) -> usize {
        LEN - self.start as usize
    }

    pub(crate) const fn ranged(&self) -> RangedBytes<&[u8]> {
        RangedBytes {
            start: self.start as usize,
            end: LEN,
            bytes: &self.buffer,
        }
    }
}

////////////////////////////////////////////////////////

#[repr(packed)]
#[derive(Copy)]
pub(crate) struct Packed<T>(pub(crate) T);

impl<T: Copy> Clone for Packed<T> {
    fn clone(&self) -> Self {
        *self
    }
}

////////////////////////////////////////////////////////

#[derive(Copy, Clone)]
pub(crate) struct RangedBytes<B> {
    pub(crate) start: usize,
    pub(crate) end: usize,
    pub(crate) bytes: B,
}

impl<B> RangedBytes<B> {
    pub(crate) const fn len(&self) -> usize {
        self.end - self.start
    }
}
impl RangedBytes<&'static [u8]> {
    pub const EMPTY: Self = RangedBytes {
        start: 0,
        end: 0,
        bytes: &[],
    };
}

////////////////////////////////////////////////////////

#[derive(Copy, Clone)]
pub(crate) enum Sign {
    Positive,
    Negative = 1,
}

#[derive(Copy, Clone)]
pub(crate) enum WasTruncated {
    Yes(usize),
    No,
}

impl WasTruncated {
    pub(crate) const fn get_length(self, len: usize) -> usize {
        match self {
            WasTruncated::Yes(x) => x,
            WasTruncated::No => len,
        }
    }
}

const fn is_char_boundary(b: u8) -> bool {
    (b as i8) >= -0x40
}

// truncates a utf8-encoded string to the character before the `truncate_to` index
//
pub(crate) const fn truncated_str_len(
    ranged: RangedBytes<&[u8]>,
    truncate_to: usize,
) -> WasTruncated {
    if ranged.len() <= truncate_to {
        WasTruncated::No
    } else {
        let mut i = ranged.start + truncate_to;
        while i != ranged.start {
            // if it's a non-continuation byte, break
            if is_char_boundary(ranged.bytes[i]) {
                break;
            }
            i -= 1;
        }

        WasTruncated::Yes(i - ranged.start)
    }
}

pub(crate) const fn truncated_debug_str_len(
    ranged: RangedBytes<&[u8]>,
    truncate_to: usize,
) -> WasTruncated {
    let blen = ranged.end;

    // `* 4` because the longest escape is written like `\xNN` which is 4 bytes
    // `+ 2` for the quote characters
    if blen * 4 + 2 <= truncate_to {
        WasTruncated::No
    } else if truncate_to == 0 {
        WasTruncated::Yes(0)
    } else {
        let mut i = ranged.start;
        // = 1 for opening quote char
        let mut fmtlen = 1;
        loop {
            let next_i = next_char_boundary(ranged, min_usize(i + 1, ranged.end));

            let mut j = i;
            while j < next_i {
                fmtlen += ForEscaping::byte_len(ranged.bytes[j]);
                j += 1;
            }

            if fmtlen > truncate_to {
                break;
            } else if next_i == ranged.end {
                i = next_i;
                break;
            } else {
                i = next_i;
            }
        }

        if i == blen && fmtlen < truncate_to {
            WasTruncated::No
        } else {
            WasTruncated::Yes(i - ranged.start)
        }
    }
}

const fn next_char_boundary(ranged: RangedBytes<&[u8]>, mut i: usize) -> usize {
    while i < ranged.end && !is_char_boundary(ranged.bytes[i]) {
        i += 1;
    }
    i
}

#[cfg_attr(feature = "test", derive(Debug, PartialEq))]
pub(crate) struct StartAndBytes<const LEN: usize> {
    pub start: u8,
    pub bytes: [u8; LEN],
}

#[track_caller]
pub(crate) const fn tail_byte_array<const TO: usize>(
    len: usize,
    input: &[u8],
) -> StartAndBytes<TO> {
    assert!(len <= TO);

    let mut bytes = [0u8; TO];

    let start = TO - len;
    let mut i = start;
    let mut j = 0;
    while j < len {
        bytes[i] = input[j];
        i += 1;
        j += 1;
    }

    assert!(start < 256);
    StartAndBytes {
        start: start as u8,
        bytes,
    }
}

////////////////////////////////////////////////////////////////////////////////

/// Const equivalent of `&buffer[..upto]` with saturating indexing.
///
/// "saturating indexing" means that if `upto > buffer.len()`,
/// then this returns all of `buffer` instead of panicking.
///
/// # Example
///
/// ```rust
/// use const_panic::utils::bytes_up_to;
///
/// const BYTES: &[u8] = &[3, 5, 8, 13, 21, 34, 55, 89];
///
/// const SLICE: &[u8] = bytes_up_to(BYTES, 4);
/// assert_eq!(SLICE, &[3, 5, 8, 13][..]);
///
/// const WHOLE: &[u8] = bytes_up_to(BYTES, usize::MAX);
/// assert_eq!(WHOLE, &[3, 5, 8, 13, 21, 34, 55, 89][..]);
///
/// ```
pub const fn bytes_up_to(buffer: &[u8], upto: usize) -> &[u8] {
    if upto > buffer.len() {
        return buffer;
    }

    #[cfg(not(feature = "rust_1_64"))]
    {
        let mut to_truncate = buffer.len() - upto;
        let mut out: &[u8] = buffer;

        while to_truncate != 0 {
            if let [rem @ .., _] = out {
                out = rem;
            }
            to_truncate -= 1;
        }

        if out.len() != upto {
            panic!("BUG!")
        }

        out
    }

    #[cfg(feature = "rust_1_64")]
    {
        // SAFETY: the above conditional ensures that `upto` doesn't
        // create a partially-dangling slice
        unsafe { core::slice::from_raw_parts(buffer.as_ptr(), upto) }
    }
}