|
| 1 | +//! Generators for char type. |
| 2 | +
|
| 3 | +use crate::BoxGen; |
| 4 | +use crate::FilterWithGen; |
| 5 | +use crate::MapWithGen; |
| 6 | + |
| 7 | +/// Any possible Rust `char` value, that is any valid unicode scalar value. |
| 8 | +/// For reference, see |
| 9 | +/// [Rust official documentation on `char` type](https://doc.rust-lang.org/std/primitive.char.html). |
| 10 | +pub fn unicode() -> BoxGen<char> { |
| 11 | + crate::gens::u32::ranged(0..=0x10ffff) |
| 12 | + .filter(|&num| !(0xD800..=0xDFFF).contains(&num)) |
| 13 | + .map(|num| char::from_u32(num).unwrap(), |ch| ch as u32) |
| 14 | +} |
| 15 | + |
| 16 | +/// Shorthand for [unicode]. |
| 17 | +pub fn any() -> BoxGen<char> { |
| 18 | + unicode() |
| 19 | +} |
| 20 | + |
| 21 | +/// Build char generator from u32 range (inclusive) |
| 22 | +fn chars_from_u32_range(min: u32, max_inclusive: u32) -> BoxGen<char> { |
| 23 | + crate::gens::u32::ranged(min..=max_inclusive) |
| 24 | + .map(|num| char::from_u32(num).unwrap(), |ch| ch as u32) |
| 25 | +} |
| 26 | + |
| 27 | +/// Any arabic numeral 0..9, unicode values 48..=57. |
| 28 | +pub fn number() -> BoxGen<char> { |
| 29 | + chars_from_u32_range(48, 57) |
| 30 | +} |
| 31 | + |
| 32 | +/// Any alpha upper char, unicode values 65..=90. |
| 33 | +pub fn alpha_upper() -> BoxGen<char> { |
| 34 | + chars_from_u32_range(65, 90) |
| 35 | +} |
| 36 | + |
| 37 | +/// Any alpha lower char, unicode values 97..=122. |
| 38 | +pub fn alpha_lower() -> BoxGen<char> { |
| 39 | + chars_from_u32_range(97, 122) |
| 40 | +} |
| 41 | + |
| 42 | +/// Any alpha char, both lower and upper case. |
| 43 | +pub fn alpha() -> BoxGen<char> { |
| 44 | + crate::gens::mix_evenly(&[alpha_upper(), alpha_lower()]) |
| 45 | +} |
| 46 | + |
| 47 | +/// Any alpha numeric char, see [alpha] and [number]. |
| 48 | +pub fn alpha_numeric() -> BoxGen<char> { |
| 49 | + crate::gens::mix_with_ratio(&[(9, alpha()), (1, number())]) |
| 50 | +} |
| 51 | + |
| 52 | +/// Any ASCII printable character, unicode values 32..=126. |
| 53 | +pub fn ascii_printable() -> BoxGen<char> { |
| 54 | + chars_from_u32_range(32, 126) |
| 55 | +} |
| 56 | + |
| 57 | +/// Any ASCII character, including both printable and non-printable characters, |
| 58 | +/// unicode values 0..=127. |
| 59 | +pub fn ascii() -> BoxGen<char> { |
| 60 | + chars_from_u32_range(0, 127) |
| 61 | +} |
0 commit comments