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
// devela::text::char::impls
//
//!
//
// TOC
// - specific implementations
// - common implementations
use super::{
char_byte_len, char_is_7bit, char_is_noncharacter, char_to_utf8_bytes, Char16, Char24, Char32,
Char7, Char8, CharConversionError, NonMaxU8, NonSurrogateU16, Result, UnicodeScalar,
};
use crate::meta::paste;
/* specific implementations */
mod char;
mod char16;
mod char24;
mod char32;
mod char7;
mod char8;
/* common implementations */
macro_rules! impls {
($name:ident: $( $bits:literal ),+ ) => {
$( impls![@$name: $bits]; )+
};
(@$name:ident: $bits:literal) => { paste! {
/* impl traits */
impl UnicodeScalar for [<$name $bits>] {
const MAX: Self = Self::MAX;
/* encode */
#[inline]
fn byte_len(self) -> usize { self.byte_len() }
#[inline]
fn len_utf8(self) -> usize { self.len_utf8() }
#[inline]
fn len_utf16(self) -> usize { self.len_utf16() }
#[inline]
fn encode_utf8(self, dst: &mut [u8]) -> &mut str {
self.to_char().encode_utf8(dst)
}
#[inline]
fn to_utf8_bytes(self) -> [u8; 4] {
let dyn_array = self.to_utf8_bytes();
let mut array = [0u8; 4];
for i in 0..dyn_array.len() {
array[i] = dyn_array[i];
}
array
}
#[inline]
fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16] {
self.to_char().encode_utf16(dst)
}
#[inline]
fn to_digit(self, radix: u32) -> Option<u32> { self.to_digit(radix) }
#[inline]
fn to_ascii_uppercase(self) -> Self { self.to_ascii_uppercase() }
#[inline]
fn to_ascii_lowercase(self) -> Self { self.to_ascii_lowercase() }
/* queries */
#[inline]
fn is_noncharacter(self) -> bool { self.is_noncharacter() }
#[inline]
fn is_digit(self, radix: u32) -> bool { self.is_digit(radix) }
//
#[inline]
fn is_control(self) -> bool { self.to_char().is_control() }
#[inline]
fn is_nul(self) -> bool { self.is_nul() }
#[inline]
fn is_alphabetic(self) -> bool { self.to_char().is_alphabetic() }
#[inline]
fn is_numeric(self) -> bool { self.to_char().is_numeric() }
#[inline]
fn is_alphanumeric(self) -> bool { self.to_char().is_alphanumeric() }
#[inline]
fn is_lowercase(self) -> bool { self.to_char().is_lowercase() }
#[inline]
fn is_uppercase(self) -> bool { self.to_char().is_uppercase() }
#[inline]
fn is_whitespace(self) -> bool { self.to_char().is_whitespace() }
//
#[inline]
fn is_ascii(self) -> bool { self.is_ascii() }
}
/* impl const fns */
impl [<$name $bits>] {
/* encode */
/// Returns the number of bytes needed to represent the scalar value.
#[inline]
#[must_use]
pub const fn byte_len(self) -> usize { char_byte_len(self.to_u32()) }
/// Returns the number of bytes needed to encode in UTF-8.
#[inline]
#[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.
#[inline]
#[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.
#[inline]
#[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`).
#[inline]
#[must_use]
pub const fn is_nul(self) -> bool { self.to_u32() == 0 }
/// Checks if the unicode scalar is a digit in the given radix.
///
/// See also [`to_digit`][Self#method.to_digit].
#[inline]
#[must_use]
pub const fn is_digit(self, radix: u32) -> bool {
if let Some(_) = self.to_digit(radix) { true } else { false }
}
}
}};
}
impls![Char: 7, 8, 16, 24, 32];