Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
Vendored
+1267
File diff suppressed because it is too large
Load Diff
Vendored
+181
@@ -0,0 +1,181 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
#[repr(u8)]
|
||||
#[allow(dead_code)]
|
||||
#[derive(PartialEq, Eq, Ord, PartialOrd, Copy, Clone, Hash)]
|
||||
pub enum AsciiByte {
|
||||
B0 = 0,
|
||||
B1 = 1,
|
||||
B2 = 2,
|
||||
B3 = 3,
|
||||
B4 = 4,
|
||||
B5 = 5,
|
||||
B6 = 6,
|
||||
B7 = 7,
|
||||
B8 = 8,
|
||||
B9 = 9,
|
||||
B10 = 10,
|
||||
B11 = 11,
|
||||
B12 = 12,
|
||||
B13 = 13,
|
||||
B14 = 14,
|
||||
B15 = 15,
|
||||
B16 = 16,
|
||||
B17 = 17,
|
||||
B18 = 18,
|
||||
B19 = 19,
|
||||
B20 = 20,
|
||||
B21 = 21,
|
||||
B22 = 22,
|
||||
B23 = 23,
|
||||
B24 = 24,
|
||||
B25 = 25,
|
||||
B26 = 26,
|
||||
B27 = 27,
|
||||
B28 = 28,
|
||||
B29 = 29,
|
||||
B30 = 30,
|
||||
B31 = 31,
|
||||
B32 = 32,
|
||||
B33 = 33,
|
||||
B34 = 34,
|
||||
B35 = 35,
|
||||
B36 = 36,
|
||||
B37 = 37,
|
||||
B38 = 38,
|
||||
B39 = 39,
|
||||
B40 = 40,
|
||||
B41 = 41,
|
||||
B42 = 42,
|
||||
B43 = 43,
|
||||
B44 = 44,
|
||||
B45 = 45,
|
||||
B46 = 46,
|
||||
B47 = 47,
|
||||
B48 = 48,
|
||||
B49 = 49,
|
||||
B50 = 50,
|
||||
B51 = 51,
|
||||
B52 = 52,
|
||||
B53 = 53,
|
||||
B54 = 54,
|
||||
B55 = 55,
|
||||
B56 = 56,
|
||||
B57 = 57,
|
||||
B58 = 58,
|
||||
B59 = 59,
|
||||
B60 = 60,
|
||||
B61 = 61,
|
||||
B62 = 62,
|
||||
B63 = 63,
|
||||
B64 = 64,
|
||||
B65 = 65,
|
||||
B66 = 66,
|
||||
B67 = 67,
|
||||
B68 = 68,
|
||||
B69 = 69,
|
||||
B70 = 70,
|
||||
B71 = 71,
|
||||
B72 = 72,
|
||||
B73 = 73,
|
||||
B74 = 74,
|
||||
B75 = 75,
|
||||
B76 = 76,
|
||||
B77 = 77,
|
||||
B78 = 78,
|
||||
B79 = 79,
|
||||
B80 = 80,
|
||||
B81 = 81,
|
||||
B82 = 82,
|
||||
B83 = 83,
|
||||
B84 = 84,
|
||||
B85 = 85,
|
||||
B86 = 86,
|
||||
B87 = 87,
|
||||
B88 = 88,
|
||||
B89 = 89,
|
||||
B90 = 90,
|
||||
B91 = 91,
|
||||
B92 = 92,
|
||||
B93 = 93,
|
||||
B94 = 94,
|
||||
B95 = 95,
|
||||
B96 = 96,
|
||||
B97 = 97,
|
||||
B98 = 98,
|
||||
B99 = 99,
|
||||
B100 = 100,
|
||||
B101 = 101,
|
||||
B102 = 102,
|
||||
B103 = 103,
|
||||
B104 = 104,
|
||||
B105 = 105,
|
||||
B106 = 106,
|
||||
B107 = 107,
|
||||
B108 = 108,
|
||||
B109 = 109,
|
||||
B110 = 110,
|
||||
B111 = 111,
|
||||
B112 = 112,
|
||||
B113 = 113,
|
||||
B114 = 114,
|
||||
B115 = 115,
|
||||
B116 = 116,
|
||||
B117 = 117,
|
||||
B118 = 118,
|
||||
B119 = 119,
|
||||
B120 = 120,
|
||||
B121 = 121,
|
||||
B122 = 122,
|
||||
B123 = 123,
|
||||
B124 = 124,
|
||||
B125 = 125,
|
||||
B126 = 126,
|
||||
B127 = 127,
|
||||
}
|
||||
|
||||
impl AsciiByte {
|
||||
/// Convert `[u8; N]` to `[AsciiByte; N]`
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// All bytes MUST be in the range 0 to 127 inclusive.
|
||||
#[inline]
|
||||
pub const unsafe fn to_ascii_byte_array<const N: usize>(bytes: &[u8; N]) -> [AsciiByte; N] {
|
||||
*(bytes as *const [u8; N] as *const [AsciiByte; N])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn from_decimal_digit(digit: u8) -> AsciiByte {
|
||||
// Note: This code optimizes nicely with no branches.
|
||||
match digit {
|
||||
0 => AsciiByte::B48,
|
||||
1 => AsciiByte::B49,
|
||||
2 => AsciiByte::B50,
|
||||
3 => AsciiByte::B51,
|
||||
4 => AsciiByte::B52,
|
||||
5 => AsciiByte::B53,
|
||||
6 => AsciiByte::B54,
|
||||
7 => AsciiByte::B55,
|
||||
8 => AsciiByte::B56,
|
||||
9 => AsciiByte::B57,
|
||||
_ => {
|
||||
debug_assert!(false, "not a single digit: {digit}");
|
||||
AsciiByte::B32 // Space
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn to_ascii_uppercase(self) -> Self {
|
||||
// SAFETY: AsciiByte is repr(u8) and has same size as u8
|
||||
unsafe { core::mem::transmute::<u8, AsciiByte>((self as u8).to_ascii_uppercase()) }
|
||||
}
|
||||
|
||||
pub(crate) const fn to_ascii_lowercase(self) -> Self {
|
||||
// SAFETY: AsciiByte is repr(u8) and has same size as u8
|
||||
unsafe { core::mem::transmute::<u8, AsciiByte>((self as u8).to_ascii_lowercase()) }
|
||||
}
|
||||
}
|
||||
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
use crate::TinyAsciiStr;
|
||||
use crate::UnvalidatedTinyAsciiStr;
|
||||
use databake::*;
|
||||
|
||||
impl<const N: usize> Bake for TinyAsciiStr<N> {
|
||||
fn bake(&self, env: &CrateEnv) -> TokenStream {
|
||||
env.insert("tinystr");
|
||||
let string = self.as_str();
|
||||
quote! {
|
||||
tinystr::tinystr!(#N, #string)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> BakeSize for TinyAsciiStr<N> {
|
||||
fn borrows_size(&self) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> Bake for UnvalidatedTinyAsciiStr<N> {
|
||||
fn bake(&self, env: &CrateEnv) -> TokenStream {
|
||||
match self.try_into_tinystr() {
|
||||
Ok(tiny) => {
|
||||
let tiny = tiny.bake(env);
|
||||
quote! {
|
||||
#tiny.to_unvalidated()
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
let bytes = self.0.bake(env);
|
||||
env.insert("tinystr");
|
||||
quote! {
|
||||
tinystr::UnvalidatedTinyAsciiStr::from_utf8_unchecked(#bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> BakeSize for UnvalidatedTinyAsciiStr<N> {
|
||||
fn borrows_size(&self) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
test_bake!(
|
||||
TinyAsciiStr<10>,
|
||||
const,
|
||||
crate::tinystr!(10usize, "foo"),
|
||||
tinystr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unvalidated() {
|
||||
test_bake!(
|
||||
UnvalidatedTinyAsciiStr<10>,
|
||||
const,
|
||||
crate::tinystr!(10usize, "foo").to_unvalidated(),
|
||||
tinystr
|
||||
);
|
||||
test_bake!(
|
||||
UnvalidatedTinyAsciiStr<3>,
|
||||
const,
|
||||
crate::UnvalidatedTinyAsciiStr::from_utf8_unchecked(*b"AB\xCD"),
|
||||
tinystr
|
||||
);
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
use displaydoc::Display;
|
||||
|
||||
impl core::error::Error for ParseError {}
|
||||
|
||||
#[derive(Display, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum ParseError {
|
||||
#[displaydoc("found string of larger length {len} when constructing string of length {max}")]
|
||||
TooLong { max: usize, len: usize },
|
||||
#[displaydoc("tinystr types do not support strings with null bytes")]
|
||||
ContainsNull,
|
||||
#[displaydoc("attempted to construct TinyAsciiStr from a non-ASCII string")]
|
||||
NonAscii,
|
||||
}
|
||||
Vendored
+303
@@ -0,0 +1,303 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
use crate::asciibyte::AsciiByte;
|
||||
|
||||
/// Internal helper struct that performs operations on aligned integers.
|
||||
/// Supports strings up to 4 bytes long.
|
||||
#[repr(transparent)]
|
||||
pub struct Aligned4(u32);
|
||||
|
||||
impl Aligned4 {
|
||||
/// # Panics
|
||||
/// Panics if N is greater than 4
|
||||
#[inline]
|
||||
pub const fn from_ascii_bytes<const N: usize>(src: &[AsciiByte; N]) -> Self {
|
||||
let mut bytes = [0; 4];
|
||||
let mut i = 0;
|
||||
// The function documentation defines when panics may occur
|
||||
#[expect(clippy::indexing_slicing)]
|
||||
while i < N {
|
||||
bytes[i] = src[i] as u8;
|
||||
i += 1;
|
||||
}
|
||||
Self(u32::from_ne_bytes(bytes))
|
||||
}
|
||||
|
||||
pub const fn len(&self) -> usize {
|
||||
let word = self.0;
|
||||
#[cfg(target_endian = "little")]
|
||||
let len = (4 - word.leading_zeros() / 8) as usize;
|
||||
#[cfg(target_endian = "big")]
|
||||
let len = (4 - word.trailing_zeros() / 8) as usize;
|
||||
len
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic(&self) -> bool {
|
||||
let word = self.0;
|
||||
// Each of the following bitmasks set *the high bit* (0x8) to 0 for valid and 1 for invalid.
|
||||
// `mask` sets all NUL bytes to 0.
|
||||
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
|
||||
// `lower` converts the string to lowercase. It may also change the value of non-alpha
|
||||
// characters, but this does not matter for the alphabetic test that follows.
|
||||
let lower = word | 0x2020_2020;
|
||||
// `alpha` sets all alphabetic bytes to 0. We only need check for lowercase characters.
|
||||
let alpha = !(lower + 0x1f1f_1f1f) | (lower + 0x0505_0505);
|
||||
// The overall string is valid if every character passes at least one test.
|
||||
// We performed two tests here: non-NUL (`mask`) and alphabetic (`alpha`).
|
||||
(alpha & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphanumeric(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_alphabetic
|
||||
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
|
||||
let numeric = !(word + 0x5050_5050) | (word + 0x4646_4646);
|
||||
let lower = word | 0x2020_2020;
|
||||
let alpha = !(lower + 0x1f1f_1f1f) | (lower + 0x0505_0505);
|
||||
(alpha & numeric & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_numeric(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_alphabetic
|
||||
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
|
||||
let numeric = !(word + 0x5050_5050) | (word + 0x4646_4646);
|
||||
(numeric & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_lowercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// For efficiency, this function tests for an invalid string rather than a valid string.
|
||||
// A string is ASCII lowercase iff it contains no uppercase ASCII characters.
|
||||
// `invalid_case` sets all uppercase ASCII characters to 0 and all others to 1.
|
||||
let invalid_case = !(word + 0x3f3f_3f3f) | (word + 0x2525_2525);
|
||||
// The string is valid if it contains no invalid characters (if all high bits are 1).
|
||||
(invalid_case & 0x8080_8080) == 0x8080_8080
|
||||
}
|
||||
|
||||
pub const fn is_ascii_titlecase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_lowercase
|
||||
let invalid_case = if cfg!(target_endian = "little") {
|
||||
!(word + 0x3f3f_3f1f) | (word + 0x2525_2505)
|
||||
} else {
|
||||
!(word + 0x1f3f_3f3f) | (word + 0x0525_2525)
|
||||
};
|
||||
(invalid_case & 0x8080_8080) == 0x8080_8080
|
||||
}
|
||||
|
||||
pub const fn is_ascii_uppercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_lowercase
|
||||
let invalid_case = !(word + 0x1f1f_1f1f) | (word + 0x0505_0505);
|
||||
(invalid_case & 0x8080_8080) == 0x8080_8080
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic_lowercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// `mask` sets all NUL bytes to 0.
|
||||
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
|
||||
// `lower_alpha` sets all lowercase ASCII characters to 0 and all others to 1.
|
||||
let lower_alpha = !(word + 0x1f1f_1f1f) | (word + 0x0505_0505);
|
||||
// The overall string is valid if every character passes at least one test.
|
||||
// We performed two tests here: non-NUL (`mask`) and lowercase ASCII character (`alpha`).
|
||||
(lower_alpha & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic_titlecase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_alphabetic_lowercase
|
||||
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
|
||||
let title_case = if cfg!(target_endian = "little") {
|
||||
!(word + 0x1f1f_1f3f) | (word + 0x0505_0525)
|
||||
} else {
|
||||
!(word + 0x3f1f_1f1f) | (word + 0x2505_0505)
|
||||
};
|
||||
(title_case & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic_uppercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_alphabetic_lowercase
|
||||
let mask = (word + 0x7f7f_7f7f) & 0x8080_8080;
|
||||
let upper_alpha = !(word + 0x3f3f_3f3f) | (word + 0x2525_2525);
|
||||
(upper_alpha & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn to_ascii_lowercase(&self) -> [AsciiByte; 4] {
|
||||
let word = self.0;
|
||||
let mask = ((word + 0x3f3f_3f3f) & !(word + 0x2525_2525) & 0x8080_8080) >> 2;
|
||||
let result = word | mask;
|
||||
// SAFETY: The existing word is ASCII, and it is bitwise-ORed with `mask`,
|
||||
// a subset of 0x20_20_20_... (0x80_... >> 2), which keeps the word
|
||||
// in the ASCII range (all bytes <= 127).
|
||||
unsafe { AsciiByte::to_ascii_byte_array(&result.to_ne_bytes()) }
|
||||
}
|
||||
|
||||
pub const fn to_ascii_titlecase(&self) -> [AsciiByte; 4] {
|
||||
let word = self.0.to_le();
|
||||
let mask = ((word + 0x3f3f_3f1f) & !(word + 0x2525_2505) & 0x8080_8080) >> 2;
|
||||
let result = (word | mask) & !(0x20 & mask);
|
||||
// SAFETY: The existing word is ASCII, and it is bitwise-ORed with `mask`, a subset of 0x20_20_20_...
|
||||
// (0x80_... >> 2), which keeps the word in the ASCII range (all bytes <= 127). It is then ANDed
|
||||
// which cannot introduce new bits.
|
||||
unsafe { AsciiByte::to_ascii_byte_array(&u32::from_le(result).to_ne_bytes()) }
|
||||
}
|
||||
|
||||
pub const fn to_ascii_uppercase(&self) -> [AsciiByte; 4] {
|
||||
let word = self.0;
|
||||
let mask = !(((word + 0x1f1f_1f1f) & !(word + 0x0505_0505) & 0x8080_8080) >> 2);
|
||||
let result = word & mask;
|
||||
// SAFETY: The existing word is ASCII, and it is bitwise-ANDed with `mask`,
|
||||
// which cannot introduce new bits, so it stays ASCII.
|
||||
unsafe { AsciiByte::to_ascii_byte_array(&result.to_ne_bytes()) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal helper struct that performs operations on aligned integers.
|
||||
/// Supports strings up to 8 bytes long.
|
||||
#[repr(transparent)]
|
||||
pub struct Aligned8(u64);
|
||||
|
||||
impl Aligned8 {
|
||||
/// # Panics
|
||||
/// Panics if N is greater than 8
|
||||
#[inline]
|
||||
pub const fn from_ascii_bytes<const N: usize>(src: &[AsciiByte; N]) -> Self {
|
||||
let mut bytes = [0; 8];
|
||||
let mut i = 0;
|
||||
// The function documentation defines when panics may occur
|
||||
#[expect(clippy::indexing_slicing)]
|
||||
while i < N {
|
||||
bytes[i] = src[i] as u8;
|
||||
i += 1;
|
||||
}
|
||||
Self(u64::from_ne_bytes(bytes))
|
||||
}
|
||||
|
||||
pub const fn len(&self) -> usize {
|
||||
let word = self.0;
|
||||
#[cfg(target_endian = "little")]
|
||||
let len = (8 - word.leading_zeros() / 8) as usize;
|
||||
#[cfg(target_endian = "big")]
|
||||
let len = (8 - word.trailing_zeros() / 8) as usize;
|
||||
len
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic(&self) -> bool {
|
||||
let word = self.0;
|
||||
let mask = (word + 0x7f7f_7f7f_7f7f_7f7f) & 0x8080_8080_8080_8080;
|
||||
let lower = word | 0x2020_2020_2020_2020;
|
||||
let alpha = !(lower + 0x1f1f_1f1f_1f1f_1f1f) | (lower + 0x0505_0505_0505_0505);
|
||||
(alpha & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphanumeric(&self) -> bool {
|
||||
let word = self.0;
|
||||
let mask = (word + 0x7f7f_7f7f_7f7f_7f7f) & 0x8080_8080_8080_8080;
|
||||
let numeric = !(word + 0x5050_5050_5050_5050) | (word + 0x4646_4646_4646_4646);
|
||||
let lower = word | 0x2020_2020_2020_2020;
|
||||
let alpha = !(lower + 0x1f1f_1f1f_1f1f_1f1f) | (lower + 0x0505_0505_0505_0505);
|
||||
(alpha & numeric & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_numeric(&self) -> bool {
|
||||
let word = self.0;
|
||||
let mask = (word + 0x7f7f_7f7f_7f7f_7f7f) & 0x8080_8080_8080_8080;
|
||||
let numeric = !(word + 0x5050_5050_5050_5050) | (word + 0x4646_4646_4646_4646);
|
||||
(numeric & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_lowercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
let invalid_case = !(word + 0x3f3f_3f3f_3f3f_3f3f) | (word + 0x2525_2525_2525_2525);
|
||||
(invalid_case & 0x8080_8080_8080_8080) == 0x8080_8080_8080_8080
|
||||
}
|
||||
|
||||
pub const fn is_ascii_titlecase(&self) -> bool {
|
||||
let word = self.0;
|
||||
let invalid_case = if cfg!(target_endian = "little") {
|
||||
!(word + 0x3f3f_3f3f_3f3f_3f1f) | (word + 0x2525_2525_2525_2505)
|
||||
} else {
|
||||
!(word + 0x1f3f_3f3f_3f3f_3f3f) | (word + 0x0525_2525_2525_2525)
|
||||
};
|
||||
(invalid_case & 0x8080_8080_8080_8080) == 0x8080_8080_8080_8080
|
||||
}
|
||||
|
||||
pub const fn is_ascii_uppercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
let invalid_case = !(word + 0x1f1f_1f1f_1f1f_1f1f) | (word + 0x0505_0505_0505_0505);
|
||||
(invalid_case & 0x8080_8080_8080_8080) == 0x8080_8080_8080_8080
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic_lowercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// `mask` sets all NUL bytes to 0.
|
||||
let mask = (word + 0x7f7f_7f7f_7f7f_7f7f) & 0x8080_8080_8080_8080;
|
||||
// `lower_alpha` sets all lowercase ASCII characters to 0 and all others to 1.
|
||||
let lower_alpha = !(word + 0x1f1f_1f1f_1f1f_1f1f) | (word + 0x0505_0505_0505_0505);
|
||||
// The overall string is valid if every character passes at least one test.
|
||||
// We performed two tests here: non-NUL (`mask`) and lowercase ASCII character (`alpha`).
|
||||
(lower_alpha & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic_titlecase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_alphabetic_lowercase
|
||||
let mask = (word + 0x7f7f_7f7f_7f7f_7f7f) & 0x8080_8080_8080_8080;
|
||||
let title_case = if cfg!(target_endian = "little") {
|
||||
!(word + 0x1f1f_1f1f_1f1f_1f3f) | (word + 0x0505_0505_0505_0525)
|
||||
} else {
|
||||
!(word + 0x3f1f_1f1f_1f1f_1f1f) | (word + 0x2505_0505_0505_0505)
|
||||
};
|
||||
(title_case & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn is_ascii_alphabetic_uppercase(&self) -> bool {
|
||||
let word = self.0;
|
||||
// See explanatory comments in is_ascii_alphabetic_lowercase
|
||||
let mask = (word + 0x7f7f_7f7f_7f7f_7f7f) & 0x8080_8080_8080_8080;
|
||||
let upper_alpha = !(word + 0x3f3f_3f3f_3f3f_3f3f) | (word + 0x2525_2525_2525_2525);
|
||||
(upper_alpha & mask) == 0
|
||||
}
|
||||
|
||||
pub const fn to_ascii_lowercase(&self) -> [AsciiByte; 8] {
|
||||
let word = self.0;
|
||||
let mask = ((word + 0x3f3f_3f3f_3f3f_3f3f)
|
||||
& !(word + 0x2525_2525_2525_2525)
|
||||
& 0x8080_8080_8080_8080)
|
||||
>> 2;
|
||||
let result = word | mask;
|
||||
// SAFETY: The existing word is ASCII, and it is bitwise-ORed with `mask`,
|
||||
// a subset of 0x20_20_20_... (0x80_... >> 2), which keeps the word
|
||||
// in the ASCII range (all bytes <= 127).
|
||||
unsafe { AsciiByte::to_ascii_byte_array(&result.to_ne_bytes()) }
|
||||
}
|
||||
|
||||
pub const fn to_ascii_titlecase(&self) -> [AsciiByte; 8] {
|
||||
let word = self.0.to_le();
|
||||
let mask = ((word + 0x3f3f_3f3f_3f3f_3f1f)
|
||||
& !(word + 0x2525_2525_2525_2505)
|
||||
& 0x8080_8080_8080_8080)
|
||||
>> 2;
|
||||
let result = (word | mask) & !(0x20 & mask);
|
||||
// SAFETY: The existing word is ASCII, and it is bitwise-ORed with `mask`, a subset of 0x20_20_20_...
|
||||
// (0x80_... >> 2), which keeps the word in the ASCII range (all bytes <= 127). It is then ANDed
|
||||
// which cannot introduce new bits.
|
||||
unsafe { AsciiByte::to_ascii_byte_array(&u64::from_le(result).to_ne_bytes()) }
|
||||
}
|
||||
|
||||
pub const fn to_ascii_uppercase(&self) -> [AsciiByte; 8] {
|
||||
let word = self.0;
|
||||
let mask = !(((word + 0x1f1f_1f1f_1f1f_1f1f)
|
||||
& !(word + 0x0505_0505_0505_0505)
|
||||
& 0x8080_8080_8080_8080)
|
||||
>> 2);
|
||||
let result = word & mask;
|
||||
// SAFETY: The existing word is ASCII, and it is bitwise-ANDed with `mask`,
|
||||
// which cannot introduce new bits, so it stays ASCII.
|
||||
unsafe { AsciiByte::to_ascii_byte_array(&result.to_ne_bytes()) }
|
||||
}
|
||||
}
|
||||
Vendored
+105
@@ -0,0 +1,105 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
|
||||
#![cfg_attr(not(any(test, doc)), no_std)]
|
||||
#![cfg_attr(
|
||||
not(test),
|
||||
deny(
|
||||
clippy::indexing_slicing,
|
||||
clippy::unwrap_used,
|
||||
clippy::expect_used,
|
||||
clippy::panic,
|
||||
)
|
||||
)]
|
||||
// #![warn(missing_docs)]
|
||||
|
||||
//! `tinystr` is a utility crate of the [`ICU4X`] project.
|
||||
//!
|
||||
//! It includes [`TinyAsciiStr`], a core API for representing small ASCII-only bounded length strings.
|
||||
//!
|
||||
//! It is optimized for operations on strings of size 8 or smaller. When use cases involve comparison
|
||||
//! and conversion of strings for lowercase/uppercase/titlecase, or checking
|
||||
//! numeric/alphabetic/alphanumeric, `TinyAsciiStr` is the edge performance library.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```rust
|
||||
//! use tinystr::TinyAsciiStr;
|
||||
//!
|
||||
//! let s1: TinyAsciiStr<4> = "tEsT".parse().expect("Failed to parse.");
|
||||
//!
|
||||
//! assert_eq!(s1, "tEsT");
|
||||
//! assert_eq!(s1.to_ascii_uppercase(), "TEST");
|
||||
//! assert_eq!(s1.to_ascii_lowercase(), "test");
|
||||
//! assert_eq!(s1.to_ascii_titlecase(), "Test");
|
||||
//! assert!(s1.is_ascii_alphanumeric());
|
||||
//! assert!(!s1.is_ascii_numeric());
|
||||
//!
|
||||
//! let s2 = TinyAsciiStr::<8>::try_from_raw(*b"New York")
|
||||
//! .expect("Failed to parse.");
|
||||
//!
|
||||
//! assert_eq!(s2, "New York");
|
||||
//! assert_eq!(s2.to_ascii_uppercase(), "NEW YORK");
|
||||
//! assert_eq!(s2.to_ascii_lowercase(), "new york");
|
||||
//! assert_eq!(s2.to_ascii_titlecase(), "New york");
|
||||
//! assert!(!s2.is_ascii_alphanumeric());
|
||||
//! ```
|
||||
//!
|
||||
//! # Details
|
||||
//!
|
||||
//! When strings are of size 8 or smaller, the struct transforms the strings as `u32`/`u64` and uses
|
||||
//! bitmasking to provide basic string manipulation operations:
|
||||
//! * `is_ascii_numeric`
|
||||
//! * `is_ascii_alphabetic`
|
||||
//! * `is_ascii_alphanumeric`
|
||||
//! * `to_ascii_lowercase`
|
||||
//! * `to_ascii_uppercase`
|
||||
//! * `to_ascii_titlecase`
|
||||
//! * `PartialEq`
|
||||
//!
|
||||
//! `TinyAsciiStr` will fall back to `u8` character manipulation for strings of length greater than 8.
|
||||
|
||||
//!
|
||||
//! [`ICU4X`]: ../icu/index.html
|
||||
|
||||
mod macros;
|
||||
|
||||
mod ascii;
|
||||
mod asciibyte;
|
||||
mod error;
|
||||
mod int_ops;
|
||||
mod unvalidated;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
mod serde;
|
||||
|
||||
#[cfg(feature = "databake")]
|
||||
mod databake;
|
||||
|
||||
#[cfg(feature = "zerovec")]
|
||||
mod ule;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
|
||||
pub use ascii::TinyAsciiStr;
|
||||
pub use error::ParseError;
|
||||
pub use unvalidated::UnvalidatedTinyAsciiStr;
|
||||
|
||||
/// These are temporary compatability reexports that will be removed
|
||||
/// in a future version.
|
||||
pub type TinyStr4 = TinyAsciiStr<4>;
|
||||
/// These are temporary compatability reexports that will be removed
|
||||
/// in a future version.
|
||||
pub type TinyStr8 = TinyAsciiStr<8>;
|
||||
/// These are temporary compatability reexports that will be removed
|
||||
/// in a future version.
|
||||
pub type TinyStr16 = TinyAsciiStr<16>;
|
||||
|
||||
#[test]
|
||||
fn test_size() {
|
||||
assert_eq!(size_of::<TinyStr4>(), size_of::<Option<TinyStr4>>());
|
||||
assert_eq!(size_of::<TinyStr8>(), size_of::<Option<TinyStr8>>());
|
||||
}
|
||||
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tinystr {
|
||||
($n:literal, $s:literal) => {{
|
||||
// Force it into a const context; otherwise it may get evaluated at runtime instead.
|
||||
const TINYSTR_MACRO_CONST: $crate::TinyAsciiStr<$n> = {
|
||||
match $crate::TinyAsciiStr::try_from_utf8($s.as_bytes()) {
|
||||
Ok(s) => s,
|
||||
// Cannot format the error since formatting isn't const yet
|
||||
Err(_) => panic!(concat!("Failed to construct tinystr from ", $s)),
|
||||
}
|
||||
};
|
||||
TINYSTR_MACRO_CONST
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_macro_construction() {
|
||||
let s1 = tinystr!(8, "foobar");
|
||||
assert_eq!(&*s1, "foobar");
|
||||
|
||||
let s1 = tinystr!(12, "foobarbaz");
|
||||
assert_eq!(&*s1, "foobarbaz");
|
||||
}
|
||||
}
|
||||
Vendored
+103
@@ -0,0 +1,103 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
use crate::TinyAsciiStr;
|
||||
use core::fmt;
|
||||
use core::marker::PhantomData;
|
||||
use core::ops::Deref;
|
||||
use serde_core::de::{Error, SeqAccess, Visitor};
|
||||
use serde_core::ser::SerializeTuple;
|
||||
use serde_core::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
impl<const N: usize> Serialize for TinyAsciiStr<N> {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
if serializer.is_human_readable() {
|
||||
self.deref().serialize(serializer)
|
||||
} else {
|
||||
let mut seq = serializer.serialize_tuple(N)?;
|
||||
for byte in self.all_bytes() {
|
||||
seq.serialize_element(byte)?;
|
||||
}
|
||||
seq.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TinyAsciiStrVisitor<const N: usize> {
|
||||
marker: PhantomData<TinyAsciiStr<N>>,
|
||||
}
|
||||
|
||||
impl<const N: usize> TinyAsciiStrVisitor<N> {
|
||||
fn new() -> Self {
|
||||
TinyAsciiStrVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, const N: usize> Visitor<'de> for TinyAsciiStrVisitor<N> {
|
||||
type Value = TinyAsciiStr<N>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a TinyAsciiStr<{N}>")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let mut bytes = [0u8; N];
|
||||
let mut zeroes = false;
|
||||
for out in &mut bytes.iter_mut().take(N) {
|
||||
let byte = seq
|
||||
.next_element()?
|
||||
.ok_or_else(|| Error::invalid_length(N, &self))?;
|
||||
if byte == 0 {
|
||||
zeroes = true;
|
||||
} else if zeroes {
|
||||
return Err(Error::custom("TinyAsciiStr cannot contain null bytes"));
|
||||
}
|
||||
|
||||
if byte >= 0x80 {
|
||||
return Err(Error::custom("TinyAsciiStr cannot contain non-ascii bytes"));
|
||||
}
|
||||
*out = byte;
|
||||
}
|
||||
|
||||
Ok(unsafe { TinyAsciiStr::from_utf8_unchecked(bytes) })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, const N: usize> Deserialize<'de> for TinyAsciiStr<N> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
if deserializer.is_human_readable() {
|
||||
struct HumanVisitor<const N: usize>;
|
||||
impl<'de, const M: usize> Visitor<'de> for HumanVisitor<M> {
|
||||
type Value = TinyAsciiStr<M>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(formatter, "a TinyAsciiStr<{M}>")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: Error,
|
||||
{
|
||||
TinyAsciiStr::try_from_str(v).map_err(|_| Error::custom("invalid str"))
|
||||
}
|
||||
}
|
||||
deserializer.deserialize_str(HumanVisitor::<N>)
|
||||
} else {
|
||||
deserializer.deserialize_tuple(N, TinyAsciiStrVisitor::<N>::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+125
@@ -0,0 +1,125 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
use crate::{TinyAsciiStr, UnvalidatedTinyAsciiStr};
|
||||
#[cfg(feature = "alloc")]
|
||||
use zerovec::maps::ZeroMapKV;
|
||||
use zerovec::ule::*;
|
||||
#[cfg(feature = "alloc")]
|
||||
use zerovec::{ZeroSlice, ZeroVec};
|
||||
|
||||
// Safety (based on the safety checklist on the ULE trait):
|
||||
// 1. TinyAsciiStr does not include any uninitialized or padding bytes.
|
||||
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
|
||||
// 2. TinyAsciiStr is aligned to 1 byte.
|
||||
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
|
||||
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
|
||||
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
|
||||
// 5. The other ULE methods use the default impl.
|
||||
// 6. TinyAsciiStr byte equality is semantic equality
|
||||
unsafe impl<const N: usize> ULE for TinyAsciiStr<N> {
|
||||
#[inline]
|
||||
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
|
||||
if bytes.len() % N != 0 {
|
||||
return Err(UleError::length::<Self>(bytes.len()));
|
||||
}
|
||||
// Validate the bytes
|
||||
for chunk in bytes.chunks_exact(N) {
|
||||
let _ = TinyAsciiStr::<N>::try_from_utf8_inner(chunk, true)
|
||||
.map_err(|_| UleError::parse::<Self>())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> NicheBytes<N> for TinyAsciiStr<N> {
|
||||
// AsciiByte is 0..128
|
||||
const NICHE_BIT_PATTERN: [u8; N] = [255; N];
|
||||
}
|
||||
|
||||
impl<const N: usize> AsULE for TinyAsciiStr<N> {
|
||||
type ULE = Self;
|
||||
|
||||
#[inline]
|
||||
fn to_unaligned(self) -> Self::ULE {
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_unaligned(unaligned: Self::ULE) -> Self {
|
||||
unaligned
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a, const N: usize> ZeroMapKV<'a> for TinyAsciiStr<N> {
|
||||
type Container = ZeroVec<'a, TinyAsciiStr<N>>;
|
||||
type Slice = ZeroSlice<TinyAsciiStr<N>>;
|
||||
type GetType = TinyAsciiStr<N>;
|
||||
type OwnedType = TinyAsciiStr<N>;
|
||||
}
|
||||
|
||||
// Safety (based on the safety checklist on the ULE trait):
|
||||
// 1. UnvalidatedTinyAsciiStr does not include any uninitialized or padding bytes.
|
||||
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
|
||||
// 2. UnvalidatedTinyAsciiStr is aligned to 1 byte.
|
||||
// (achieved by `#[repr(transparent)]` on a type that satisfies this invariant)
|
||||
// 3. The impl of validate_bytes() returns an error if any byte is not valid.
|
||||
// 4. The impl of validate_bytes() returns an error if there are extra bytes.
|
||||
// 5. The other ULE methods use the default impl.
|
||||
// 6. UnvalidatedTinyAsciiStr byte equality is semantic equality
|
||||
unsafe impl<const N: usize> ULE for UnvalidatedTinyAsciiStr<N> {
|
||||
#[inline]
|
||||
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> {
|
||||
if bytes.len() % N != 0 {
|
||||
return Err(UleError::length::<Self>(bytes.len()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> AsULE for UnvalidatedTinyAsciiStr<N> {
|
||||
type ULE = Self;
|
||||
|
||||
#[inline]
|
||||
fn to_unaligned(self) -> Self::ULE {
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_unaligned(unaligned: Self::ULE) -> Self {
|
||||
unaligned
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a, const N: usize> ZeroMapKV<'a> for UnvalidatedTinyAsciiStr<N> {
|
||||
type Container = ZeroVec<'a, UnvalidatedTinyAsciiStr<N>>;
|
||||
type Slice = ZeroSlice<UnvalidatedTinyAsciiStr<N>>;
|
||||
type GetType = UnvalidatedTinyAsciiStr<N>;
|
||||
type OwnedType = UnvalidatedTinyAsciiStr<N>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::*;
|
||||
use zerovec::*;
|
||||
|
||||
#[test]
|
||||
fn test_zerovec() {
|
||||
let mut vec = ZeroVec::<TinyAsciiStr<7>>::new();
|
||||
|
||||
vec.with_mut(|v| v.push("foobar".parse().unwrap()));
|
||||
vec.with_mut(|v| v.push("baz".parse().unwrap()));
|
||||
vec.with_mut(|v| v.push("quux".parse().unwrap()));
|
||||
|
||||
let bytes = vec.as_bytes();
|
||||
|
||||
let vec: ZeroVec<TinyAsciiStr<7>> = ZeroVec::parse_bytes(bytes).unwrap();
|
||||
|
||||
assert_eq!(&*vec.get(0).unwrap(), "foobar");
|
||||
assert_eq!(&*vec.get(1).unwrap(), "baz");
|
||||
assert_eq!(&*vec.get(2).unwrap(), "quux");
|
||||
}
|
||||
}
|
||||
Vendored
+149
@@ -0,0 +1,149 @@
|
||||
// This file is part of ICU4X. For terms of use, please see the file
|
||||
// called LICENSE at the top level of the ICU4X source tree
|
||||
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
|
||||
|
||||
use crate::ParseError;
|
||||
use crate::TinyAsciiStr;
|
||||
use core::fmt;
|
||||
|
||||
/// A fixed-length bytes array that is expected to be an ASCII string but does not enforce that invariant.
|
||||
///
|
||||
/// Use this type instead of `TinyAsciiStr` if you don't need to enforce ASCII during deserialization. For
|
||||
/// example, strings that are keys of a map don't need to ever be reified as `TinyAsciiStr`s.
|
||||
///
|
||||
/// The main advantage of this type over `[u8; N]` is that it serializes as a string in
|
||||
/// human-readable formats like JSON.
|
||||
#[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy)]
|
||||
pub struct UnvalidatedTinyAsciiStr<const N: usize>(pub(crate) [u8; N]);
|
||||
|
||||
impl<const N: usize> fmt::Debug for UnvalidatedTinyAsciiStr<N> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// Debug as a string if possible
|
||||
match self.try_into_tinystr() {
|
||||
Ok(s) => fmt::Debug::fmt(&s, f),
|
||||
Err(_) => fmt::Debug::fmt(&self.0, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> UnvalidatedTinyAsciiStr<N> {
|
||||
#[inline]
|
||||
/// Converts into a [`TinyAsciiStr`]. Fails if the bytes are not valid ASCII.
|
||||
pub fn try_into_tinystr(self) -> Result<TinyAsciiStr<N>, ParseError> {
|
||||
TinyAsciiStr::try_from_raw(self.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Creates one of these from a byte slice. Fails if the bytes are too long, but
|
||||
/// does not check whether the bytes are a valid ASCII string.
|
||||
pub fn try_from_utf8(bytes: &[u8]) -> Result<Self, ParseError> {
|
||||
if bytes.len() > N {
|
||||
return Err(ParseError::TooLong {
|
||||
max: N,
|
||||
len: bytes.len(),
|
||||
});
|
||||
}
|
||||
let mut target = [0u8; N];
|
||||
target[0..bytes.len()].copy_from_slice(bytes);
|
||||
Ok(Self(target))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Creates one of these from a raw byte array.
|
||||
pub const fn from_utf8_unchecked(bytes: [u8; N]) -> Self {
|
||||
Self(bytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns the empty string.
|
||||
pub const fn default() -> Self {
|
||||
TinyAsciiStr::EMPTY.to_unvalidated()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> Default for UnvalidatedTinyAsciiStr<N> {
|
||||
fn default() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> TinyAsciiStr<N> {
|
||||
#[inline]
|
||||
// Converts into a [`UnvalidatedTinyAsciiStr`]
|
||||
pub const fn to_unvalidated(self) -> UnvalidatedTinyAsciiStr<N> {
|
||||
UnvalidatedTinyAsciiStr(*self.all_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<TinyAsciiStr<N>> for UnvalidatedTinyAsciiStr<N> {
|
||||
fn from(other: TinyAsciiStr<N>) -> Self {
|
||||
other.to_unvalidated()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<const N: usize> serde_core::Serialize for UnvalidatedTinyAsciiStr<N> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde_core::Serializer,
|
||||
{
|
||||
use serde_core::ser::Error;
|
||||
self.try_into_tinystr()
|
||||
.map_err(|_| S::Error::custom("invalid ascii in UnvalidatedTinyAsciiStr"))?
|
||||
.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! deserialize {
|
||||
($size:literal) => {
|
||||
#[cfg(feature = "serde")]
|
||||
impl<'de, 'a> serde_core::Deserialize<'de> for UnvalidatedTinyAsciiStr<$size>
|
||||
where
|
||||
'de: 'a,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde_core::Deserializer<'de>,
|
||||
{
|
||||
if deserializer.is_human_readable() {
|
||||
Ok(TinyAsciiStr::deserialize(deserializer)?.to_unvalidated())
|
||||
} else {
|
||||
Ok(Self(<[u8; $size]>::deserialize(deserializer)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
deserialize!(1);
|
||||
deserialize!(2);
|
||||
deserialize!(3);
|
||||
deserialize!(4);
|
||||
deserialize!(5);
|
||||
deserialize!(6);
|
||||
deserialize!(7);
|
||||
deserialize!(8);
|
||||
deserialize!(9);
|
||||
deserialize!(10);
|
||||
deserialize!(11);
|
||||
deserialize!(12);
|
||||
deserialize!(13);
|
||||
deserialize!(14);
|
||||
deserialize!(15);
|
||||
deserialize!(16);
|
||||
deserialize!(17);
|
||||
deserialize!(18);
|
||||
deserialize!(19);
|
||||
deserialize!(20);
|
||||
deserialize!(21);
|
||||
deserialize!(22);
|
||||
deserialize!(23);
|
||||
deserialize!(24);
|
||||
deserialize!(25);
|
||||
deserialize!(26);
|
||||
deserialize!(27);
|
||||
deserialize!(28);
|
||||
deserialize!(29);
|
||||
deserialize!(30);
|
||||
deserialize!(31);
|
||||
deserialize!(32);
|
||||
Reference in New Issue
Block a user