macro_toolset/string_v2/
rand.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Random string.

use rand::{distributions::Slice, Rng};

use super::{NumStr, StringExtT, StringT};
use crate::random::fast_random;

#[macro_export]
/// See [`RandStr`] and [`RandHexStr`] for more information.
///
/// # Example
///
/// ```
/// use macro_toolset::{random_str, string_v2::PushAnyT};
/// let mut string = String::new();
/// string.push_any(random_str!(16, b"abcABC123"));
/// string.push_any(random_str!(HEX)); // 16 (default, max 16) * 1 (default) + 0 (default, max 16)
/// string.push_any(random_str!(HEX: 16)); // 16 * 1 (default) + 0 (default)
/// string.push_any(random_str!(HEX: 16, 3)); // 16 * 3 + 0 (default)
/// string.push_any(random_str!(HEX: 16, 3, 8)); // 16 * 3 + 8
/// ```
///
/// // If you like, just `to_string_ext` is fine.
/// ```
/// use macro_toolset::{random_str, string_v2::StringExtT};
/// let string = random_str!(16, b"abcABC123").to_string_ext();
/// ```
macro_rules! random_str {
    ($range:expr, $charset:expr) => {{
        $crate::string_v2::rand::RandStr::<$range>::with_charset($charset)
    }};
    (HEX) => {{
        $crate::string_v2::rand::RandHexStr::new_default()
    }};
    (HEX: $l:expr) => {{
        $crate::string_v2::rand::RandHexStr::<$l>::new()
    }};
    (HEX: $l:expr, $rp:expr) => {{
        $crate::string_v2::rand::RandHexStr::<$l, $rp>::new()
    }};
    (HEX: $l:expr, $rp:expr, $lp:expr) => {{
        $crate::string_v2::rand::RandHexStr::<$l, $rp, $lp>::new()
    }};
}

#[derive(Debug, Clone, Copy, Default)]
/// Randon hex-like string, with fix length.
///
/// For better performance, the underlying random number is generated by
/// xorshift algorithm then converted to hex string with [`NumStr`].
///
/// By default, the length is 16.
///
/// # Generic Parameters
///
/// - `L`: The length of the string. Max 16 (u64).
/// - `RP`: Repeat `L` for `RP` times.
/// - `LP`: Lefted length. Max 16.
///
/// For example, if you need a string with length 56, you may specify `L` as 16,
/// `RP` as 56 / 16 = 3, and `LP` as 56 % 16 = 8.
///
/// Since `#![feature(generic_const_exprs)]` is not stable, we have to make use
/// of these complex const generics.
///
/// Notice: will check if params are valid when you push this to a string, or
/// panic in debug mode, work normally but slower in release mode.
pub struct RandHexStr<const L: usize = 16, const RP: usize = 1, const LP: usize = 0>;

impl<const L: usize, const RP: usize, const LP: usize> StringT for RandHexStr<L, RP, LP> {
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        match L {
            1..=16 => {
                for _ in 0..RP {
                    NumStr::hex_default(fast_random())
                        .set_resize_len::<L>()
                        .encode_to_buf(string);
                }

                if LP > 0 {
                    debug_assert!(LP <= 16, "LP should be 0..=16");

                    NumStr::hex_default(fast_random())
                        .set_resize_len::<LP>()
                        .encode_to_buf(string);
                }
            }
            0 => {}
            _ => {
                #[cfg(any(debug_assertions, test))]
                unreachable!("L should be 0..=16");

                #[cfg(not(any(debug_assertions, test)))]
                // For RELEASE mode, avoid panic but still generate random string like general
                // RandStr does.
                string.extend(
                    rand::thread_rng()
                        .sample_iter(&Slice::new(b"0123456789abcdef").unwrap())
                        .take(L * RP + LP),
                );
            }
        }
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        self.encode_to_buf(string);
        string.extend(separator.as_bytes());
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        match L {
            1..=16 => {
                for _ in 0..RP {
                    NumStr::hex_default(fast_random())
                        .set_resize_len::<L>()
                        .encode_to_bytes_buf(string);
                }

                if LP > 0 {
                    debug_assert!(LP <= 16, "LP should be 0..=16");

                    NumStr::hex_default(fast_random())
                        .set_resize_len::<LP>()
                        .encode_to_bytes_buf(string);
                }
            }
            0 => {}
            _ => {
                #[cfg(any(debug_assertions, test))]
                unreachable!("L should be 0..=16");

                #[cfg(not(any(debug_assertions, test)))]
                // For RELEASE mode, avoid panic but still generate random string like general
                // RandStr does.
                string.extend(
                    rand::thread_rng()
                        .sample_iter(&Slice::new(b"0123456789abcdef").unwrap())
                        .take(L * RP + LP),
                );
            }
        }
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        self.encode_to_bytes_buf(string);
        string.extend(separator.as_bytes());
    }
}

impl<const L: usize, const RP: usize, const LP: usize> StringExtT for RandHexStr<L, RP, LP> {}

impl RandHexStr {
    #[inline]
    /// Create a new [`RandHexStr`] and generate simple random hex-like string
    /// with length 16 (default).
    ///
    /// # Example
    ///
    /// ```rust
    /// # use macro_toolset::string::{RandHexStr, StringExtT};
    /// let random_str = RandHexStr::new_default().to_string_ext();
    /// assert_eq!(random_str.len(), 16);
    /// ```
    pub const fn new_default() -> Self {
        Self
    }
}

impl<const L: usize, const RP: usize, const LP: usize> RandHexStr<L, RP, LP> {
    #[inline]
    /// Create a new [`RandStr`] and generate random hex-like string with
    /// length setting by `L`, `RP`, `LP`.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use macro_toolset::string::{RandHexStr, StringExtT};
    /// let random_str = RandHexStr::<16, 3, 8>::new().to_string_ext();
    /// assert_eq!(random_str.len(), 56);
    /// ```
    pub const fn new() -> Self {
        RandHexStr
    }

    #[inline]
    /// Set `L`.
    ///
    /// You may prefer [`RandHexStr::<L, RP, LP>::new`](Self::new).
    pub const fn with_l<const NL: usize>(self) -> RandHexStr<NL, RP, LP> {
        RandHexStr
    }

    #[inline]
    /// Set `RP`.
    ///
    /// You may prefer [`RandHexStr::<L, RP, LP>::new`](Self::new).
    pub const fn with_rp<const NRP: usize>(self) -> RandHexStr<L, NRP, LP> {
        RandHexStr
    }

    #[inline]
    /// Set `LP`.
    ///
    /// You may prefer [`RandHexStr::<L, RP, LP>::new`](Self::new).
    pub const fn with_lp<const NLP: usize>(self) -> RandHexStr<L, RP, NLP> {
        RandHexStr
    }
}

#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
/// Randon string, with fix length and given charset.
///
/// # Generic Parameters
///
/// - `L`: The length of the string. Default is 32.
///
/// Notice: must make sure each u8 within the slice is valid
/// single byte UTF-8 char.
///
/// If the charset is `0123456789abcdef`, [`RandHexStr`] is recommended and 4~6x
/// faster than this (when feature `feat-random-fast` enabled).
pub struct RandStr<'r, const L: usize = 32>(&'r [u8]);

impl<const L: usize> StringT for RandStr<'_, L> {
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        if self.0.is_empty() {
            return;
        }

        string.extend(
            rand::thread_rng()
                .sample_iter(Slice::new(self.0).unwrap())
                .take(L),
        );
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        if self.0.is_empty() {
            return;
        }

        string.extend(
            rand::thread_rng()
                .sample_iter(Slice::new(self.0).unwrap())
                .take(L),
        );

        string.extend(separator.as_bytes());
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        if self.0.is_empty() {
            return;
        }

        string.extend(
            rand::thread_rng()
                .sample_iter(Slice::new(self.0).unwrap())
                .take(L),
        );
    }

    #[inline]
    #[cfg(feature = "feat-string-ext-bytes")]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        if self.0.is_empty() {
            return;
        }

        string.extend(
            rand::thread_rng()
                .sample_iter(Slice::new(self.0).unwrap())
                .take(L),
        );

        string.extend(separator.as_bytes());
    }
}

impl<const L: usize> StringExtT for RandStr<'_, L> {}

impl<'r> RandStr<'r> {
    #[inline]
    /// Create a new [`RandStr`] and generate random string with length
    /// setting by `L`.
    pub const fn with_charset_default(charset: &'r [u8]) -> Self {
        Self(charset)
    }
}

impl<'r, const L: usize> RandStr<'r, L> {
    #[inline]
    /// Create a new [`RandStr`] and generate random string with length
    /// setting by `L`.
    pub const fn with_charset(charset: &'r [u8]) -> Self {
        Self(charset)
    }

    #[inline]
    /// Set `L`.
    pub const fn with_l<const NL: usize>(self) -> RandStr<'r, NL> {
        RandStr(self.0)
    }
}