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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// devela::text::char::unicode_scalar
//
//! Define the [`UnicodeScalar`] trait.
//
// TOC
// - trait UnicodeScalar
// - impl for char
// - impl for char[7|8|16]
// - impl for charu[_niche]
use crate::{Char, char7, char8, char16, charu, charu_niche};
#[rustfmt::skip]
#[doc = crate::_tags!(text)]
/// Common trait for Unicode scalar types.
///
#[doc = crate::_doc_location!("text/char")]
///
/// It's implemented for: [`char7`], [`char8`], [`char16`],
/// and [`char`][crate::char].
pub trait UnicodeScalar {
/// The lowest Unicode scalar that can be represented.
const MIN: Self;
/// The highest Unicode scalar that can be represented.
const MAX: Self;
/* encode */
/// Returns itself as a `char`.
#[must_use]
fn to_char(self) -> char;
/// Returns itself as a `u32` scalar.
fn to_scalar(self) -> u32;
// // MAYBE
// fn to_charu(self) -> char_utf;
#[must_use]
/// Returns the number of bytes needed to represent the scalar value.
fn len_bytes(self) -> usize;
#[must_use]
/// Returns the number of bytes needed to encode in UTF-8.
fn len_utf8(self) -> usize;
#[must_use]
/// Returns the number of bytes needed to encode in UTF-16.
fn len_utf16(self) -> usize;
#[must_use]
/// Encodes this scalar as UTF-8 into the provided byte buffer,
/// and then returns the subslice of the buffer that contains the encoded scalar.
///
/// # Panics
/// Panics if the buffer is not large enough.
/// A buffer of length four is large enough to encode any char.
fn encode_utf8(self, dst: &mut [u8]) -> &mut str;
#[must_use]
/// Converts this `scalar` to an UTF-8 encoded sequence of bytes.
///
/// Note that this function always returns a 4-byte array, but the actual
/// UTF-8 sequence may be shorter. The unused bytes are set to 0.
fn to_utf8_bytes(self) -> [u8; 4];
#[must_use]
/// Encodes this scalar as UTF-16 into the provided byte buffer,
/// and then returns the subslice of the buffer that contains the encoded scalar.
///
/// # Panics
/// Panics if the buffer is not large enough.
/// A buffer of length 2 is large enough to encode any char.
fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16];
#[must_use]
/// 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.
fn to_digit(self, radix: u32) -> Option<u32>;
#[must_use]
/// Makes a copy of the value in its ASCII upper case equivalent.
///
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters
/// are unchanged.
fn to_ascii_uppercase(self) -> Self where Self: Sized;
#[must_use]
/// Makes a copy of the value in its ASCII lower case equivalent.
///
/// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters
/// are unchanged.
fn to_ascii_lowercase(self) -> Self where Self: Sized;
#[must_use]
/// Returns the ASCII transliteration of the value.
#[cfg(feature = "translit")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "translit")))]
fn as_ascii_translit(self) -> &'static str where Self: Sized {
crate::scalar_as_ascii_translit(self.to_scalar())
}
/* escape */
/* queries */
// basic ASCII checks
#[must_use]
/// Checks if the value is within the ASCII range.
fn is_ascii(self) -> bool;
#[must_use]
/// Returns `true` if this Unicode scalar is the nul character (`0x00`).
fn is_nul(self) -> bool;
// character type categories (general classification)
#[must_use]
/// Returns `true` if this Unicode scalar has the `Alphabetic` property.
fn is_alphabetic(self) -> bool;
#[must_use]
/// Returns `true` if this Unicode scalar has one of the general categories for numbers.
///
/// If you want to parse ASCII decimal digits (0-9) or ASCII base-N,
/// use [`is_ascii_digit`][Self#method.is_ascii_digit] or
/// [`is_digit`][Self#method.is_digit] instead.
fn is_numeric(self) -> bool;
#[must_use]
/// Returns `true` if this Unicode scalar satisfies either
/// [`is_alphabetic()`][Self#method.is_alphabetic] or
/// [`is_numeric()`][Self#method.is_numeric].
fn is_alphanumeric(self) -> bool;
#[must_use]
/// Checks if the Unicode scalar is a digit in the given radix.
///
/// See also [`to_digit`][Self#method.to_digit].
fn is_digit(self, radix: u32) -> bool;
// casing (related to alphabetic)
#[must_use]
/// Returns `true` if this Unicode scalar has the `Lowercase` property.
fn is_lowercase(self) -> bool;
#[must_use]
/// Returns `true` if this Unicode scalar has the `Uppercase` property.
fn is_uppercase(self) -> bool;
// whitespace and controls (structural characters)
#[must_use]
/// Returns `true` if this Unicode scalar has the `White_Space` property.
fn is_whitespace(self) -> bool;
#[must_use]
/// Returns `true` if this Unicode scalar has the general category for control codes.
///
/// This crate's implementations leverage [`char::is_control()`],
/// instead of [`Char::is_control()`][crate::Char::is_control].
fn is_control(self) -> bool;
/// Optional faster control mark check for common text.
fn is_control_common(self) -> bool where Self: Sized { self.is_control() }
// special Unicode categories (edge cases, least common)
#[must_use]
/// Returns `true` if this Unicode scalar is a [noncharacter][0].
///
/// [0]: https://www.unicode.org/glossary/#noncharacter
fn is_noncharacter(self) -> bool;
#[must_use]
/// Returns `true` if this Unicode scalar is an [abstract character][0].
///
/// [0]: https://www.unicode.org/glossary/#abstract_character
fn is_character(self) -> bool where Self: Sized { !self.is_noncharacter() }
#[must_use]
/// Returns `true` if this Unicode scalar is a [combining character][0].
///
/// [0]: https://www.unicode.org/glossary/#combining_character
fn is_combining(self) -> bool;
#[must_use]
/// Optional faster combining mark check for common text.
///
/// By default, this calls `is_combining()`. Implementations may override
/// this with a faster version that only checks common ranges, but may
/// return false negatives for rare [combining characters][0].
///
/// For complete Unicode compliance, use `is_combining()` instead.
///
/// [0]: https://www.unicode.org/glossary/#combining_character
fn is_combining_common(self) -> bool where Self: Sized { self.is_combining() }
#[must_use]
/// Returns `true` if this Unicode scalar is a [fullwidth][0] character.
///
/// [0]: https://www.unicode.org/glossary/#fullwidth
fn is_fullwidth(self) -> bool;
#[must_use]
/// Optional faster fullwidth check for common text.
///
/// By default, this calls `is_fullwidth()`. Implementations may override
/// this with a faster version that only checks common ranges, but may
/// return false negatives for rare [fullwidth][0] characters.
///
/// For complete Unicode compliance, use `is_fullwidth()` instead.
///
/// [0]: https://www.unicode.org/glossary/#fullwidth
fn is_fullwidth_common(self) -> bool where Self: Sized { self.is_fullwidth() }
}
/* impls */
#[rustfmt::skip]
impl UnicodeScalar for char {
const MIN: Self = char::MIN;
const MAX: Self = char::MAX;
/* encode */
fn to_char(self) -> char { self }
fn to_scalar(self) -> u32 { self as u32 }
fn len_bytes(self) -> usize { Char(self as u32).len_bytes() }
fn len_utf8(self) -> usize { self.len_utf8() }
fn len_utf16(self) -> usize { self.len_utf16() }
fn encode_utf8(self, dst: &mut [u8]) -> &mut str { self.encode_utf8(dst) }
fn to_utf8_bytes(self) -> [u8; 4] { Char(self).to_utf8_bytes() }
fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16] { self.encode_utf16(dst) }
fn to_digit(self, radix: u32) -> Option<u32> { self.to_digit(radix) }
fn to_ascii_uppercase(self) -> char { char::to_ascii_uppercase(&self) }
fn to_ascii_lowercase(self) -> char { char::to_ascii_lowercase(&self) }
/* queries */
fn is_ascii(self) -> bool { (self as u32) <= 0x7F }
fn is_nul(self) -> bool { self as u32 == 0 }
fn is_alphabetic(self) -> bool { self.is_alphabetic() }
fn is_numeric(self) -> bool { self.is_numeric() }
fn is_alphanumeric(self) -> bool { self.is_alphanumeric() }
fn is_digit(self, radix: u32) -> bool { self.is_digit(radix) }
fn is_lowercase(self) -> bool { self.is_lowercase() }
fn is_uppercase(self) -> bool { self.is_uppercase() }
fn is_whitespace(self) -> bool { self.is_whitespace() }
fn is_control(self) -> bool { self.is_control() }
fn is_control_common(self) -> bool { Char(self as u32).is_control_common() }
fn is_noncharacter(self) -> bool { Char(self as u32).is_noncharacter() }
fn is_combining(self) -> bool { Char(self as u32).is_combining() }
fn is_combining_common(self) -> bool { Char(self as u32).is_combining_common() }
fn is_fullwidth(self) -> bool { Char(self as u32).is_fullwidth() }
fn is_fullwidth_common(self) -> bool { Char(self as u32).is_fullwidth_common() }
}
/// Implements `UnicodeScalar` for custom char types.
macro_rules! impl_char {
() => {
impl_char![7, 8, 16];
};
($( $bits:literal),+ ) => { $crate::paste! {
$( impl_char!(@[<char $bits>]); )+
}};
(@$name:ident) => {
/* impl traits */
impl UnicodeScalar for $name { // TODO:IMPROVE avoid converting to char
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;
/* encode */
fn to_char(self) -> char { self.to_char() }
fn to_scalar(self) -> u32 { self.to_scalar() }
fn len_bytes(self) -> usize { self.len_bytes() }
fn len_utf8(self) -> usize { self.len_utf8() }
fn len_utf16(self) -> usize { self.len_utf16() }
fn encode_utf8(self, dst: &mut [u8]) -> &mut str {
self.to_char().encode_utf8(dst)
}
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
}
fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16] {
self.to_char().encode_utf16(dst)
}
fn to_digit(self, radix: u32) -> Option<u32> { self.to_digit(radix) }
fn to_ascii_uppercase(self) -> Self { self.to_ascii_uppercase() }
fn to_ascii_lowercase(self) -> Self { self.to_ascii_lowercase() }
/* queries */
fn is_ascii(self) -> bool { self.is_ascii() }
fn is_nul(self) -> bool { self.is_nul() }
fn is_alphabetic(self) -> bool { self.to_char().is_alphabetic() }
fn is_numeric(self) -> bool { self.to_char().is_numeric() }
fn is_alphanumeric(self) -> bool { self.to_char().is_alphanumeric() }
fn is_digit(self, radix: u32) -> bool { self.is_digit(radix) }
fn is_lowercase(self) -> bool { self.to_char().is_lowercase() }
fn is_uppercase(self) -> bool { self.to_char().is_uppercase() }
fn is_whitespace(self) -> bool { self.to_char().is_whitespace() }
fn is_control(self) -> bool { self.to_char().is_control() }
fn is_control_common(self) -> bool { Char(self.to_scalar()).is_control_common() }
fn is_noncharacter(self) -> bool { self.is_noncharacter() }
fn is_combining(self) -> bool { Char(self.to_scalar()).is_combining() }
fn is_combining_common(self) -> bool { Char(self.to_scalar()).is_combining_common() }
fn is_fullwidth(self) -> bool { Char(self.to_scalar()).is_fullwidth() }
fn is_fullwidth_common(self) -> bool { Char(self.to_scalar()).is_fullwidth_common() }
}
};
}
impl_char!();
macro_rules! impl_charu {
() => { impl_charu![charu, charu_niche]; };
($( $name:ident),+ ) => { $( impl_charu!(@$name); )+ };
(@$name:ident) => {
impl UnicodeScalar for $name { // TODO:IMPROVE avoid converting to char
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;
/* encode */
fn to_char(self) -> char { self.to_char() }
fn to_scalar(self) -> u32 { self.to_scalar() }
fn len_bytes(self) -> usize { self.len_bytes() }
fn len_utf8(self) -> usize { self.len_utf8() }
fn len_utf16(self) -> usize { self.to_char().len_utf16() }
fn encode_utf8(self, dst: &mut [u8]) -> &mut str { self.to_char().encode_utf8(dst) }
fn to_utf8_bytes(self) -> [u8; 4] { self.to_utf8_bytes() }
fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16] { self.to_char().encode_utf16(dst) }
fn to_digit(self, radix: u32) -> Option<u32> { self.to_char().to_digit(radix) }
fn to_ascii_uppercase(self) -> Self {
Self::from_char(char::to_ascii_uppercase(&self.to_char()))
}
fn to_ascii_lowercase(self) -> Self {
Self::from_char(char::to_ascii_lowercase(&self.to_char()))
}
/* queries */
fn is_ascii(self) -> bool { self.is_ascii() }
fn is_nul(self) -> bool { self.is_nul() }
fn is_alphabetic(self) -> bool { self.to_char().is_alphabetic() }
fn is_numeric(self) -> bool { self.to_char().is_numeric() }
fn is_alphanumeric(self) -> bool { self.to_char().is_alphanumeric() }
fn is_digit(self, radix: u32) -> bool { self.to_char().is_digit(radix) }
fn is_lowercase(self) -> bool { self.to_char().is_lowercase() }
fn is_uppercase(self) -> bool { self.to_char().is_uppercase() }
fn is_whitespace(self) -> bool { self.to_char().is_whitespace() }
fn is_control(self) -> bool { self.to_char().is_control() }
fn is_noncharacter(self) -> bool { Char(self.to_scalar()).is_noncharacter() }
fn is_combining(self) -> bool { Char(self.to_scalar()).is_combining() }
fn is_combining_common(self) -> bool { Char(self.to_scalar()).is_combining_common() }
fn is_fullwidth(self) -> bool { Char(self.to_scalar()).is_fullwidth() }
fn is_fullwidth_common(self) -> bool { Char(self.to_scalar()).is_fullwidth_common() }
}
};
}
impl_charu!();