rustyaid/string.rs
1use crate::number::{some_number_between, some_number_less_than};
2
3const ALPHA_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
4 abcdefghijklmnopqrstuvwxyz ";
5const NUMERIC_CHARSET: &[u8] = b"0123456789";
6
7const ALPHANUMERIC_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
8 abcdefghijklmnopqrstuvwxyz\
9 0123456789 ";
10
11const ASCII_CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
12 abcdefghijklmnopqrstuvwxyz\
13 0123456789!@#$%^&*(){}[]\"\
14 <>',.?+|_/=\\-:;`~ ";
15
16const DEFAULT_MIN_LENGTH: usize = 1;
17const DEFAULT_MAX_LENGTH: usize = 1024;
18
19/// Creates a new string of random characters
20///
21/// Creates a new string of a random length between 1 and 1024 characters.
22/// Each character can be any ASCII value with a valid character value (Excluding values such as
23/// NUL, BELL, DEL) and does not include the extended ASCII character set.
24/// Complete set is defined as a space and the following
25/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
26/// >
27/// > abcdefghijklmnopqrstuvwxyz
28/// >
29/// > 0123456789!@#$%^&*(){}[]\"
30/// >
31/// > <>',.?+|_/=\\-:;`~
32///
33/// # Examples
34///
35/// Basic usage:
36/// ```
37/// use rustyaid::string::*;
38/// let s = some_string();
39/// ```
40pub fn some_string() -> String {
41 some_string_of_length_between(DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH)
42}
43
44/// Creates a new string of random characters for a set length
45///
46/// Creates a new string of `to_bound: usize` characters.
47/// Each character can be any ASCII value with a valid character value (Excluding values such as
48/// NUL, BELL, DEL) and does not include the extended ASCII character set.
49/// Complete set is defined as a space and the following
50/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
51/// >
52/// > abcdefghijklmnopqrstuvwxyz
53/// >
54/// > 0123456789!@#$%^&*(){}[]\"
55/// >
56/// > <>',.?+|_/=\\-:;`~
57///
58/// # Examples
59///
60/// Basic usage:
61/// ```
62/// use rustyaid::string::*;
63/// let s = some_string_of_length(50);
64/// ```
65pub fn some_string_of_length(to_bound: usize) -> String {
66 some_string_of_length_between(to_bound, to_bound)
67}
68
69/// Creates a new string of random characters for a length between two values
70///
71/// Creates a new string of random characters between `from_bound: usize` and `to_bound: usize`
72/// Each character can be any ASCII value with a valid character value (Excluding values such as
73/// NUL, BELL, DEL) and does not include the extended ASCII character set.
74/// Complete set is defined as a space and the following
75/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
76/// >
77/// > abcdefghijklmnopqrstuvwxyz
78/// >
79/// > 0123456789!@#$%^&*(){}[]\"
80/// >
81/// > <>',.?+|_/=\\-:;`~
82///
83/// # Examples
84///
85/// Basic usage:
86/// ```
87/// use rustyaid::string::*;
88/// let s = some_string_of_length_between(50, 100);
89/// ```
90pub fn some_string_of_length_between(from_bound: usize, to_bound: usize) -> String {
91 let bound = some_number_between(from_bound, to_bound);
92 string_for_charset(bound, ASCII_CHARSET)
93}
94
95/// Creates a new string of random alphanumeric characters
96///
97/// Creates a new string of a random length between 1 and 1024 alphanumeric characters.
98/// Each character can be any ASCII value with a valid character value (Excluding values such as
99/// NUL, BELL, DEL) and does not include the extended ASCII character set.
100/// Complete set is defined as a space and the following
101/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
102/// >
103/// > abcdefghijklmnopqrstuvwxyz
104/// >
105/// > 0123456789
106///
107/// # Examples
108///
109/// Basic usage:
110/// ```
111/// use rustyaid::string::*;
112/// let s = some_alphanumeric_string();
113/// ```
114pub fn some_alphanumeric_string() -> String {
115 some_alphanumeric_string_of_length_between(DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH)
116}
117
118/// Creates a new string of random alphanumeric characters for a set length
119///
120/// Creates a new string of `to_bound: usize` alphanumeric characters.
121/// Each character can be any ASCII value with a valid character value (Excluding values such as
122/// NUL, BELL, DEL) and does not include the extended ASCII character set.
123/// Complete set is defined as a space and the following
124/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
125/// >
126/// > abcdefghijklmnopqrstuvwxyz
127/// >
128/// > 0123456789
129///
130/// # Examples
131///
132/// Basic usage:
133/// ```
134/// use rustyaid::string::*;
135/// let s = some_alphanumeric_string_of_length(50);
136/// ```
137pub fn some_alphanumeric_string_of_length(to_bound: usize) -> String {
138 some_alphanumeric_string_of_length_between(to_bound, to_bound)
139}
140
141/// Creates a new string of random alphanumeric characters for a length between two values
142///
143/// Creates a new string of random alphanumeric characters between `from_bound: usize` and `to_bound: usize`
144/// Each character can be any ASCII value with a valid character value (Excluding values such as
145/// NUL, BELL, DEL) and does not include the extended ASCII character set.
146/// Complete set is defined as a space and the following
147/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
148/// >
149/// > abcdefghijklmnopqrstuvwxyz
150/// >
151/// > 0123456789
152///
153/// # Examples
154///
155/// Basic usage:
156/// ```
157/// use rustyaid::string::*;
158/// let s = some_alpha_string_of_length_between(50, 100);
159/// ```
160pub fn some_alphanumeric_string_of_length_between(from_bound: usize, to_bound: usize) -> String {
161 let bound = some_number_between(from_bound, to_bound);
162 string_for_charset(bound, ALPHANUMERIC_CHARSET)
163}
164
165/// Creates a new string of random numeric characters
166///
167/// Creates a new string of a random length between 1 and 1024 numeric characters.
168/// Each character can be any ASCII value with a valid character value (Excluding values such as
169/// NUL, BELL, DEL) and does not include the extended ASCII character set.
170/// Complete set is defined as the following
171/// > 0123456789
172///
173/// # Examples
174///
175/// Basic usage:
176/// ```
177/// use rustyaid::string::*;
178/// let s = some_numeric_string();
179/// ```
180pub fn some_numeric_string() -> String {
181 some_numeric_string_of_length_between(DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH)
182}
183
184/// Creates a new string of random numeric characters for a set length
185///
186/// Creates a new string of `to_bound: usize` numeric characters.
187/// Each character can be any ASCII value with a valid character value (Excluding values such as
188/// NUL, BELL, DEL) and does not include the extended ASCII character set.
189/// Complete set is defined as the following
190/// > 0123456789
191///
192/// # Examples
193///
194/// Basic usage:
195/// ```
196/// use rustyaid::string::*;
197/// let s = some_numeric_string_of_length(50);
198/// ```
199pub fn some_numeric_string_of_length(to_bound: usize) -> String {
200 some_numeric_string_of_length_between(to_bound, to_bound)
201}
202
203/// Creates a new string of random numeric characters for a length between two values
204///
205/// Creates a new string of random numeric characters between `from_bound: usize` and `to_bound: usize`
206/// Each character can be any ASCII value with a valid character value (Excluding values such as
207/// NUL, BELL, DEL) and does not include the extended ASCII character set.
208/// Complete set is defined as the following
209/// > 0123456789
210///
211/// # Examples
212///
213/// Basic usage:
214/// ```
215/// use rustyaid::string::*;
216/// let s = some_numeric_string_of_length_between(50, 100);
217/// ```
218pub fn some_numeric_string_of_length_between(from_bound: usize, to_bound: usize) -> String {
219 let bound = some_number_between(from_bound, to_bound);
220 string_for_charset(bound, NUMERIC_CHARSET)
221}
222
223/// Creates a new string of random alpha characters
224///
225/// Creates a new string of a random length between 1 and 1024 alpha characters.
226/// Each character can be any ASCII value with a valid character value (Excluding values such as
227/// NUL, BELL, DEL) and does not include the extended ASCII character set.
228/// Complete set is defined as the following
229/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
230/// >
231/// > abcdefghijklmnopqrstuvwxyz
232///
233/// # Examples
234///
235/// Basic usage:
236/// ```
237/// use rustyaid::string::*;
238/// let s = some_numeric_string();
239/// ```
240pub fn some_alpha_string() -> String {
241 some_alpha_string_of_length_between(DEFAULT_MIN_LENGTH, DEFAULT_MAX_LENGTH)
242}
243
244/// Creates a new string of random alpha characters for a set length
245///
246/// Creates a new string of `to_bound: usize` alpha characters.
247/// Each character can be any ASCII value with a valid character value (Excluding values such as
248/// NUL, BELL, DEL) and does not include the extended ASCII character set.
249/// Complete set is defined as the following
250/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
251/// >
252/// > abcdefghijklmnopqrstuvwxyz
253///
254/// # Examples
255///
256/// Basic usage:
257/// ```
258/// use rustyaid::string::*;
259/// let s = some_alpha_string_of_length(50);
260/// ```
261pub fn some_alpha_string_of_length(to_bound: usize) -> String {
262 some_alpha_string_of_length_between(to_bound, to_bound)
263}
264
265/// Creates a new string of random alpha characters for a length between two values
266///
267/// Creates a new string of random alpha characters between `from_bound: usize` and `to_bound: usize`
268/// Each character can be any ASCII value with a valid character value (Excluding values such as
269/// NUL, BELL, DEL) and does not include the extended ASCII character set.
270/// Complete set is defined as the following
271/// > ABCDEFGHIJKLMNOPQRSTUVWXYZ
272/// >
273/// > abcdefghijklmnopqrstuvwxyz
274///
275/// # Examples
276///
277/// Basic usage:
278/// ```
279/// use rustyaid::string::*;
280/// let s = some_alpha_string_of_length_between(50, 100);
281/// ```
282pub fn some_alpha_string_of_length_between(from_bound: usize, to_bound: usize) -> String {
283 let bound = some_number_between(from_bound, to_bound);
284 string_for_charset(bound, ALPHA_CHARSET)
285}
286
287fn string_for_charset(bound: usize, charset: &[u8]) -> String {
288 (0..bound)
289 .map(|_| charset[some_number_less_than(charset.len())] as char)
290 .collect()
291}
292
293#[cfg(test)]
294mod tests {
295 use super::*;
296
297 #[test]
298 fn can_create_some_string() {
299 let actual = some_string();
300 assert!(actual.len() > 0);
301 }
302
303 #[test]
304 fn can_create_some_string_of_length() {
305 let length = some_number_between(1, 64);
306 let actual = some_string_of_length(length);
307 assert_eq!(actual.len(), length);
308 }
309
310 #[test]
311 fn can_create_some_string_of_length_between() {
312 let min_length = some_number_between(1, 32);
313 let max_length = some_number_between(33, 64);
314 let actual = some_string_of_length_between(min_length, max_length);
315 assert!(actual.len() >= min_length);
316 assert!(actual.len() <= max_length);
317 }
318
319 #[test]
320 fn can_create_some_alpha_string() {
321 let actual = some_alpha_string();
322 assert!(actual.len() > 0);
323 }
324
325 #[test]
326 fn can_create_some_alpha_string_of_length() {
327 let length = some_number_between(1, 64);
328 let actual = some_alpha_string_of_length(length);
329 assert_eq!(actual.len(), length);
330 }
331
332 #[test]
333 fn can_create_some_alpha_string_of_length_between() {
334 let min_length = some_number_between(1, 32);
335 let max_length = some_number_between(33, 64);
336 let actual = some_alpha_string_of_length_between(min_length, max_length);
337 assert!(actual.len() >= min_length);
338 assert!(actual.len() <= max_length);
339 }
340
341 #[test]
342 fn can_create_some_alphanumeric_string() {
343 let actual = some_alphanumeric_string();
344 assert!(actual.len() > 0);
345 }
346
347 #[test]
348 fn can_create_some_alphanumeric_string_of_length() {
349 let length = some_number_between(1, 64);
350 let actual = some_alphanumeric_string_of_length(length);
351 assert_eq!(actual.len(), length);
352 }
353
354 #[test]
355 fn can_create_some_alphanumeric_string_of_length_between() {
356 let min_length = some_number_between(1, 32);
357 let max_length = some_number_between(33, 64);
358 let actual = some_alphanumeric_string_of_length_between(min_length, max_length);
359 assert!(actual.len() >= min_length);
360 assert!(actual.len() <= max_length);
361 }
362
363 #[test]
364 fn can_create_some_numeric_string() {
365 let actual = some_numeric_string();
366 assert!(actual.len() > 0);
367 }
368
369 #[test]
370 fn can_create_some_numeric_string_of_length() {
371 let length = some_number_between(1, 64);
372 let actual = some_numeric_string_of_length(length);
373 assert_eq!(actual.len(), length);
374 }
375
376 #[test]
377 fn can_create_some_numeric_string_of_length_between() {
378 let min_length = some_number_between(1, 32);
379 let max_length = some_number_between(33, 64);
380 let actual = some_numeric_string_of_length_between(min_length, max_length);
381 assert!(actual.len() >= min_length);
382 assert!(actual.len() <= max_length);
383 }
384}