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
// devela::text::char::scalar
//
//!
//
use crate::{Char, MaybeNiche, char7, char8, char16};
/// Implements const fns for custom char types.
macro_rules! impl_char {
() => {
impl_char![7, 8, 16];
};
($( $bits:literal),+ ) => { $crate::paste! {
$( impl_char!(@[<char $bits>]); )+
}};
(@$name:ident) => {
impl $name {
/* encode */
/// Returns the number of bytes needed to represent the scalar value.
#[must_use]
pub const fn len_bytes(self) -> usize { Char(self.to_scalar()).len_bytes() }
/// Returns the number of bytes needed to encode in UTF-8.
#[must_use]
pub const fn len_utf8(self) -> usize { self.to_char().len_utf8() }
/// Returns the number of bytes needed to encode in UTF-16.
#[must_use]
pub const fn len_utf16(self) -> usize { self.to_char().len_utf16() }
/// Converts the scalar to a digit in the given radix.
///
/// 'Digit' is defined to be only the following characters:
/// `0-9`, `a-z`, `A-Z`.
///
/// # Errors
/// Returns None if the char does not refer to a digit in the given radix.
///
/// # Panics
/// Panics if given a radix larger than 36.
#[must_use]
pub const fn to_digit(self, radix: u32) -> Option<u32> {
self.to_char().to_digit(radix)
}
/* queries */
/// Returns `true` if this is the nul character (`0x00`).
#[must_use]
pub const fn is_nul(self) -> bool { self.to_scalar() == 0 }
/// Checks if the Unicode scalar is a digit in the given radix.
///
/// See also [`to_digit`][Self#method.to_digit].
#[must_use]
pub const fn is_digit(self, radix: u32) -> bool {
if let Some(_) = self.to_digit(radix) { true } else { false }
}
/// Compile-time equality comparison.
#[must_use]
pub const fn eq(self, other: Self) -> bool {
MaybeNiche(self.0).eq(MaybeNiche(other.0))
}
}
};
}
impl_char!();