| 1 | #[cfg(feature = "format")] |
| 2 | use core::num; |
| 3 | |
| 4 | #[cfg(feature = "format")] |
| 5 | use lexical_util::format; |
| 6 | |
| 7 | #[cfg(feature = "format")] |
| 8 | const fn from_digit_separator(digit_separator: u8) -> u128 { |
| 9 | format::NumberFormatBuilder::new() |
| 10 | .digit_separator(num::NonZeroU8::new(digit_separator)) |
| 11 | .digit_separator_flags(true) |
| 12 | .build_unchecked() |
| 13 | } |
| 14 | |
| 15 | #[cfg(feature = "format")] |
| 16 | fn is_valid_digit_separator(digit_separator: u8) -> bool { |
| 17 | let format = from_digit_separator(digit_separator); |
| 18 | format::is_valid_digit_separator(format) |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | #[cfg(feature = "format")] |
| 23 | fn test_is_valid_digit_separator() { |
| 24 | assert_eq!(is_valid_digit_separator(b'_'), true); |
| 25 | assert_eq!(is_valid_digit_separator(b'\''), true); |
| 26 | assert_eq!(is_valid_digit_separator(b'.'), true); |
| 27 | assert_eq!(is_valid_digit_separator(b'e'), true); |
| 28 | assert_eq!(is_valid_digit_separator(b'0'), false); |
| 29 | assert_eq!(is_valid_digit_separator(128), false); |
| 30 | |
| 31 | // Try with a custom radix. |
| 32 | #[cfg(feature = "radix")] |
| 33 | { |
| 34 | let format = format::NumberFormat::<{ from_digit_separator(b'e') }>::rebuild() |
| 35 | .radix(16) |
| 36 | .build_unchecked(); |
| 37 | assert_eq!(format::is_valid_digit_separator(format), false); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[cfg(all(feature = "power-of-two", feature = "format"))] |
| 42 | fn is_valid_punctuation(digit_separator: u8, base_prefix: u8, base_suffix: u8) -> bool { |
| 43 | let fmt = format::NumberFormatBuilder::new() |
| 44 | .digit_separator(num::NonZeroU8::new(digit_separator)) |
| 45 | .digit_separator_flags(true) |
| 46 | .base_prefix(num::NonZeroU8::new(base_prefix)) |
| 47 | .base_suffix(num::NonZeroU8::new(base_suffix)) |
| 48 | .build_unchecked(); |
| 49 | format::is_valid_punctuation(fmt) |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | #[cfg(all(feature = "power-of-two", feature = "format"))] |
| 54 | fn test_is_valid_punctuation() { |
| 55 | assert_eq!(is_valid_punctuation(b'_', b'h', 0), true); |
| 56 | assert_eq!(is_valid_punctuation(b'e', b'h', 0), true); |
| 57 | assert_eq!(is_valid_punctuation(b'^', b'h', 0), true); |
| 58 | assert_eq!(is_valid_punctuation(b'\'', b'h', 0), true); |
| 59 | assert_eq!(is_valid_punctuation(b'\'', b'h', b'h'), false); |
| 60 | } |