devela 0.27.0

A development layer of coherence.
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// devela::text::char::scalar::utf8

use super::*;
use crate::{Char, CharAscii, MismatchedCapacity, NonExtremeU32, NonNiche, Str, is, slice, unwrap};

/// Implements methods for both `charu` and `charu_niche`.
macro_rules! impl_charu {
    //
    () => {
        impl_charu![@non charu, NonNiche::<u32>, !charu_niche];
        impl_charu![@niche charu_niche, NonExtremeU32, !charu];
    };
    // specific implementations for the non-niche version
    (@non $name:ident, $inner:ty, !$other:ty) => { $crate::paste! {
        impl_charu![@common $name, $inner, !$other];
        impl $name {
            #[doc = "Tries to convert this `" $name "` to `char7`."]
            ///
            /// # Errors
            /// Returns [`MismatchedCapacity`] if `self` can't fit in 7 bits.
            #[inline(always)]
            pub const fn try_to_char7(self) -> Result<char7, MismatchedCapacity> {
                char7::try_from_charu(self)
            }
            #[doc = "Tries to convert this `" $name "` to `char8`."]
            ///
            /// # Errors
            /// Returns [`MismatchedCapacity`] if `self` can't fit in 8 bits.
            #[inline(always)]
            pub const fn try_to_char8(self) -> Result<char8, MismatchedCapacity> {
                char8::try_from_charu(self)
            }
            #[doc = "Tries to convert this `" $name "` to `char17`."]
            ///
            /// # Errors
            /// Returns [`MismatchedCapacity`] if `self` can't fit in 16 bits.
            #[inline(always)]
            pub const fn try_to_char16(self) -> Result<char16, MismatchedCapacity> {
                char16::try_from_charu(self)
            }
        }
    }};
    // specific implementations for the niche version
    (@niche $name:ident, $inner:ty, !$other:ty) => { $crate::paste! {
        impl_charu![@common $name, $inner, !$other];
        // impl $name {}
    }};
    // common implementations for all versions
    (@common $name:ident, $inner:ty, !$other:ty) => { $crate::paste! {
        impl $name {
            /* private helper fns */

            const fn new_unchecked(value: u32) -> $name {
                #[cfg(any(feature = "safe_text", not(feature = "unsafe_niche")))]
                return $name(unwrap![some $inner::new(value)]);

                #[cfg(all(not(feature = "safe_text"), feature = "unsafe_niche"))]
                unsafe {
                    $name($inner::new_unchecked(value))
                }
            }

            /* constants */

            #[doc = "The lowest Unicode scalar a `" $name "` can represent, `'\u{00}'`."]
            pub const MIN: $name = $name::new_unchecked(0x00);

            #[doc = "The highest Unicode scalar a `" $name "` can represent, `'\u{FF}'`."]
            pub const MAX: $name = $name::from_char(char::MAX);

            /// `U+FFFD REPLACEMENT CHARACTER (�)` is used in Unicode to represent a decoding error.
            pub const REPLACEMENT_CHARACTER: $name =
                $name::from_char(char::REPLACEMENT_CHARACTER);

            /* from_* conversions */

            #[doc = "Creates a `" $name "` from a `char`."]
            pub const fn from_char(c: char) -> $name {
                Self::new_unchecked(u32::from_be_bytes(Char(c as u32).to_utf8_bytes_unchecked()))
            }

            #[doc = "Creates a `" $name "` from an `CharAscii`."]
            pub const fn from_char_ascii(c: CharAscii) -> $name {
                Self::new_unchecked(c as u8 as u32)
            }

            #[doc = "Creates a `" $name "` from a `char7`."]
            pub const fn from_char7(c: char7) -> $name {
                Self::new_unchecked(c.0.get() as u32)
            }

            #[doc = "Creates a `" $name "` from an `char8`."]
            pub const fn from_char8(c: char8) -> $name {
                Self::new_unchecked(
                    u32::from_be_bytes(Char(c.to_scalar()).to_utf8_bytes_unchecked()))
            }

            #[doc = "Creates a `" $name "` from an `char16`."]
            pub const fn from_char16(c: char16) -> $name {
                Self::new_unchecked(
                    u32::from_be_bytes(Char(c.to_scalar()).to_utf8_bytes_unchecked()))
            }

            #[doc = "Creates a `" $name "` from the first scalar value present in a string slice."]
            ///
            /// Returns `None` if the string is empty.
            ///
            /// # Example
            /// ```
            /// # use devela::charu;
            /// let c = charu::from_str("A").unwrap();
            /// assert_eq!(c.to_utf8_bytes(), [b'A', 0, 0, 0]);
            ///
            /// let c = charu::from_str("¢ rest").unwrap();
            /// assert_eq!(c.to_utf8_bytes(), [0xC2, 0xA2, 0, 0]);
            ///
            /// assert!(charu::from_str("").is_none());
            /// ```
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            pub const fn from_str(string: &str) -> Option<$name> {
                is![string.is_empty(), None, Some($name::from_str_unchecked(string))]
            }

            #[doc = "Creates a `" $name "` from the first scalar value present in a string slice."]
            ///
            /// # Panics
            /// Panics if the string is empty.
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            pub const fn from_str_unchecked(string: &str) -> $name {
                debug_assert!(!string.is_empty(), "string must not be empty");
                let bytes = string.as_bytes();
                let len = Char(bytes[0]).len_utf8_unchecked();
                Self::decode_utf8(bytes, len)
            }

            #[doc = "Creates a `" $name "` from the first scalar value present in a string slice."]
            ///
            /// Returns the scalar and the bytes consumed as a `u32`.
            /// Returns `None` if the string is empty.
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[must_use]
            pub const fn from_str_with_len(string: &str) -> Option<($name, u32)> {
                is![string.is_empty(), None, Some($name::from_str_with_len_unchecked(string))]
            }

            #[doc = "Creates a `" $name "` from the first scalar value present in a string slice."]
            ///
            /// Returns the scalar and the bytes consumed as a `u32`.
            ///
            /// # Panics
            /// Panics if the string is empty.
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            pub const fn from_str_with_len_unchecked(string: &str) -> ($name, u32) {
                debug_assert!(!string.is_empty(), "string must not be empty");
                let bytes = string.as_bytes();
                let len = Char(bytes[0]).len_utf8_unchecked();
                (Self::decode_utf8(bytes, len), len as u32)
            }

            #[doc = "Creates a `" $name "` from the first UTF-8 encoded scalar value in a byte
            /// slice."]
            ///
            /// Returns `None` if the slice is empty or doesn't contain a valid UTF-8 sequence
            /// at the beginning.
            ///
            /// # Example
            /// ```
            /// # use devela::charu;
            /// let ascii = charu::from_utf8_bytes(b"A").unwrap();
            /// assert_eq!(ascii.to_utf8_bytes(), [b'A', 0, 0, 0]);
            ///
            /// let multi_byte = charu::from_utf8_bytes(b"\xC2\xA2 rest").unwrap(); // ¢
            /// assert_eq!(multi_byte.to_utf8_bytes(), [0xC2, 0xA2, 0, 0]);
            ///
            /// assert!(charu::from_utf8_bytes(b"").is_none());
            /// assert!(charu::from_utf8_bytes(b"\xC2").is_none()); // incomplete sequence
            /// ```
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[must_use] #[rustfmt::skip]
            pub const fn from_utf8_bytes(bytes: &[u8]) -> Option<$name> {
                is![bytes.is_empty(), return None];
                let len = unwrap![some? Char(bytes[0]).len_utf8()]; // invalid leading byte?
                is![!Char(bytes).has_valid_continuation(0, len), return None]; // malformed utf-8?
                is![Char(bytes).has_overlong_encoding(0, len), return None]; // overlong encoding?
                is![len == 3 && bytes[0] == 0xED && bytes[1] >= 0xA0, return None]; // surrogate?
                Some(Self::decode_utf8(bytes, len))
            }

            #[doc = "Creates a `" $name "` from the first UTF-8 encoded scalar value in a byte slice,
            /// without validation."]
            ///
            /// # Safety
            /// This function is unsafe because it does not check that:
            /// - The slice is non-empty.
            /// - The bytes form a valid UTF-8 sequence.
            /// - The decoded value is a valid Unicode scalar value.
            /// - The slice has enough bytes for the UTF-8 sequence.
            ///
            /// The caller must ensure the slice contains at least one complete, valid UTF-8
            /// encoded scalar value at the beginning.
            /// Violating these conditions may lead to undefined behavior.
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
            #[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
            pub const unsafe fn from_utf8_bytes_unchecked(bytes: &[u8]) -> $name {
                let len = Char(bytes[0]).len_utf8_unchecked();
                Self::decode_utf8(bytes, len)
            }

            #[doc = "Creates a `" $name "` from the first UTF-8 encoded scalar value in a byte
            /// slice."]
            ///
            /// Returns the scalar and the consumed bytes.
            /// Returns `None` if the slice is empty
            /// or doesn't contain a valid UTF-8 sequence at the beginning.
            ///
            /// # Example
            /// ```
            /// # use devela::charu;
            /// let (c, len) = charu::from_utf8_bytes_with_len(b"A").unwrap();
            /// assert!(c.to_utf8_bytes() == [b'A', 0, 0, 0] && len == 1);
            /// let (c, len) = charu::from_utf8_bytes_with_len(b"\xC2\xA2 rest").unwrap(); // ¢
            /// assert!(c.to_utf8_bytes() == [0xC2, 0xA2, 0, 0] && len == 2);
            ///
            /// assert!(charu::from_utf8_bytes_with_len(b"").is_none());
            /// assert!(charu::from_utf8_bytes_with_len(b"\xC2").is_none()); // incomplete sequence
            /// ```
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[must_use] #[rustfmt::skip]
            pub const fn from_utf8_bytes_with_len(bytes: &[u8]) -> Option<($name, u32)> {
                is![bytes.is_empty(), return None];
                let len = unwrap![some? Char(bytes[0]).len_utf8()]; // invalid leading byte?
                is![!Char(bytes).has_valid_continuation(0, len), return None]; // malformed utf-8?
                is![Char(bytes).has_overlong_encoding(0, len), return None]; // overlong encoding?
                is![len == 3 && bytes[0] == 0xED && bytes[1] >= 0xA0, return None]; // surrogate?
                Some((Self::decode_utf8(bytes, len), len as u32))
            }

            #[doc = "Creates a `" $name "` from the first UTF-8 encoded scalar value in a byte slice,
            /// without validation."]
            ///
            /// # Safety
            /// This function is unsafe because it does not check that:
            /// - The slice is non-empty.
            /// - The bytes form a valid UTF-8 sequence.
            /// - The decoded value is a valid Unicode scalar value.
            /// - The slice has enough bytes for the UTF-8 sequence.
            ///
            /// The caller must ensure the slice contains at least one complete, valid UTF-8
            /// encoded scalar value at the beginning.
            /// Violating these conditions may lead to undefined behavior.
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
            #[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
            pub const unsafe fn from_utf8_bytes_with_len_unchecked(bytes: &[u8]) -> ($name, u32) {
                let len = Char(bytes[0]).len_utf8_unchecked();
                (Self::decode_utf8(bytes, len), len as u32)
            }

            #[doc = "Creates a `" $name "` from an array of UTF-8 bytes."]
            ///
            /// This method validates that the bytes form a valid UTF-8 sequence.
            /// represent a valid Unicode scalar value.
            ///
            /// # Example
            /// ```
            /// # use devela::charu;
            /// assert!(charu::from_utf8_byte_array([b'A', 0, 0, 0]).is_some());    // Valid
            /// assert!(charu::from_utf8_byte_array([0xC2, 0xA2, 0, 0]).is_some()); // Valid (¢)
            /// assert!(charu::from_utf8_byte_array([0xC0, 0x80, 0, 0]).is_none()); // overlong enc.
            /// assert!(charu::from_utf8_byte_array([0xC2, 0x41, 0, 0]).is_none()); // malformed cont.
            /// ```
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[rustfmt::skip]
            pub const fn from_utf8_byte_array(bytes: [u8; 4]) -> Option<$name> {
                let len = unwrap![some? Char(bytes[0]).len_utf8()]; // invalid leading byte?
                is![!Char(&bytes).has_valid_continuation(0, len), return None]; // malformed utf-8?
                is![Char(&bytes).has_overlong_encoding(0, len), return None]; // overlong encoding?
                is![len == 3 && bytes[0] == 0xED && bytes[1] >= 0xA0, return None]; // surrogate?
                Some(Self::decode_utf8(&bytes, len))
            }

            #[doc = "Creates a `" $name "` from an array of UTF-8 bytes, without validation."]
            ///
            /// # Safety
            /// This function is unsafe because it does not check that the bytes form a valid:
            /// UTF-8 sequence or represent a valid Unicode scalar value. The caller must ensure:
            ///
            /// 1. The bytes form a valid UTF-8 encoded character.
            /// 2. The decoded value is a valid Unicode scalar (e.g. not a surrogate).
            /// 3. The array contains no overlong encodings.
            /// 4. For sequences shorter than 4 bytes, trailing bytes should be zero.
            ///
            /// Violating these conditions may lead to undefined behavior.
            ///
            /// # Features
            /// Uses the `unsafe_hint` feature to optimize out unreachable branches.
            #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
            #[cfg_attr(nightly_doc, doc(cfg(feature = "unsafe_str")))]
            pub const unsafe fn from_utf8_byte_array_unchecked(bytes: [u8; 4]) -> $name {
                let len = Char(bytes[0]).len_utf8_unchecked();
                Self::decode_utf8(&bytes, len)
            }

            // Private helper to create a charu* from the first scalar value in a UTF-8 byte slice.
            // Most efficient implementation with jump table, bit shifting and oring.
            // This doesn't perform any validation and should be used carefully.
            #[inline(always)] #[rustfmt::skip]
            pub(crate) const fn decode_utf8(bytes: &[u8], len: usize) -> $name {
                let scalar = match len {
                    1 =>  (bytes[0] as u32) << 24,
                    2 => ((bytes[0] as u32) << 24)
                       | ((bytes[1] as u32) << 16),
                    3 => ((bytes[0] as u32) << 24)
                       | ((bytes[1] as u32) << 16)
                       | ((bytes[2] as u32) <<  8),
                    4 => ((bytes[0] as u32) << 24)
                       | ((bytes[1] as u32) << 16)
                       | ((bytes[2] as u32) <<  8)
                       |  (bytes[3] as u32),
                    _ => {
                        #[cfg(any(feature = "safe_text", not(feature = "unsafe_hint")))]
                        unreachable!();
                        #[cfg(all(not(feature = "safe_text"), feature = "unsafe_hint"))]
                        unsafe {
                            ::core::hint::unreachable_unchecked()
                        }
                    }
                };
                Self::new_unchecked(scalar)
            }

            /* to_* conversions */

            #[doc = "Converts this `" $name "` to a `" $other "` representation."]
            ///
            #[inline(always)]
            pub const fn [<to_ $other>](self) -> $other {
                $other::new_unchecked(self.0.get())
            }

            #[doc = "Converts this `" $name "` to a `char` Unicode scalar."]
            ///
            /// # Features
            /// Uses the `unsafe_str` feature to avoid duplicated validation.
            #[must_use]
            #[inline(always)]
            pub const fn to_char(self) -> char {
                #[cfg(any(feature = "safe_text", not(feature = "unsafe_str")))]
                return unwrap![some char::from_u32(self.to_scalar())];

                #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
                // SAFETY: charu* always has valid UTF-8 in 0..len.
                return unsafe { char::from_u32_unchecked(self.to_scalar()) };
            }

            #[doc = "Converts this `" $name "` to a `u32` Unicode scalar value."]
            #[must_use]
            #[inline(always)]
            pub const fn to_scalar(self) -> u32 {
                Char(&self.to_utf8_bytes()).to_scalar_unchecked(0).0
            }

            /// Writes the UTF-8 representation to a buffer and returns it as a string slice.
            ///
            /// # Example
            /// ```
            #[doc = "# use devela::" $name ";"]
            #[doc = "let c = " $name "::from_char('A');"]
            /// let mut buf = [0u8; 4];
            /// assert_eq!(c.as_str_into(&mut buf), "A");
            ///
            #[doc = "let c = " $name "::from_char('¢');"]
            /// let mut buf = [0u8; 4];
            /// assert_eq!(c.as_str_into(&mut buf), "¢");
            /// ```
            /// # Features
            /// Uses the `unsafe_str` feature to avoid duplicated validation.
            #[must_use]
            pub const fn as_str_into<'a>(&self, buf: &'a mut [u8; 4]) -> &'a str {
                *buf = self.to_utf8_bytes();
                let len = self.len_utf8();

                #[cfg(any(feature = "safe_text", not(feature = "unsafe_str")))]
                return unwrap![ok Str::from_utf8(slice![mut buf, ..len])];

                #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
                // SAFETY: charu always contains valid UTF-8
                unsafe {
                    Str::from_utf8_unchecked(slice![mut buf, ..len])
                }
            }

            /// Copy UTF-8 bytes into caller buffer and return a slice of the valid bytes.
            #[must_use]
            #[inline(always)]
            pub const fn as_bytes_into<'a>(&self, buf: &'a mut [u8; 4]) -> &'a [u8] {
                *buf = self.to_utf8_bytes();
                slice![buf, ..self.len_utf8()]
            }

            /// Returns the UTF-8 byte representation as a 4-byte array.
            ///
            /// The bytes are arranged with the **first UTF-8 byte at index 0**,
            /// exactly matching how UTF-8 is stored in `&str` and `[u8]`.
            /// Unused bytes in the sequence are set to 0.
            ///
            /// # Example
            /// ```
            /// # use devela::charu;
            /// let c = charu::from_char('A');
            /// assert_eq!(c.to_utf8_bytes(), [0x41, 0, 0, 0]);
            ///
            /// let c = charu::from_char('¢');
            /// assert_eq!(c.to_utf8_bytes(), [0xC2, 0xA2, 0, 0]);
            ///
            /// let c = charu::from_char('€');
            /// assert_eq!(c.to_utf8_bytes(), [0xE2, 0x82, 0xAC, 0]);
            ///
            /// let c = charu::from_char('𝅘𝅥𝅮');
            /// assert_eq!(c.to_utf8_bytes(), [0xF0, 0x9D, 0x85, 0xA0]);
            /// ```
            #[must_use]
            #[inline(always)]
            pub const fn to_utf8_bytes(self) -> [u8; 4] {
                self.0.get().to_be_bytes()
            }

            /// Returns the first byte of the UTF-8 representation.
            ///
            /// # Example
            /// ```
            /// # use devela::charu;
            /// let c = charu::from_char('A');
            /// assert_eq!(c.first_utf8_byte(), 0x41);
            ///
            /// let c = charu::from_char('¢');
            /// assert_eq!(c.first_utf8_byte(), 0xC2);
            ///
            /// let c = charu::from_char('€');
            /// assert_eq!(c.first_utf8_byte(), 0xE2);
            /// ```
            #[must_use]
            #[inline(always)]
            pub const fn first_utf8_byte(self) -> u8 {
                (self.0.get() >> 24) as u8
            }

            /* queries */

            /// Returns `true` if this Unicode scalar is a [noncharacter][0].
            ///
            /// [0]: https://www.unicode.org/glossary/#noncharacter
            #[must_use]
            #[inline(always)]
            pub const fn is_noncharacter(self) -> bool {
                Char(self.0.get()).is_noncharacter()
            }

            /// Returns `true` if this Unicode scalar is an [abstract character][0].
            ///
            /// [0]: https://www.unicode.org/glossary/#abstract_character
            #[must_use]
            #[inline(always)]
            pub const fn is_character(self) -> bool {
                !self.is_noncharacter()
            }

            /// Checks if the value is within the ASCII range.
            #[must_use]
            #[inline(always)]
            pub const fn is_ascii(self) -> bool {
                self.0.get() <= 0x7F
            }

            /// Returns `true` if this is the nul character (`0x00`).
            #[must_use]
            #[inline(always)]
            pub const fn is_nul(self) -> bool {
                self.0.get() == 0x00
            }

            /// Returns the length of the UTF-8 representation.
            #[must_use]
            #[inline(always)]
            pub const fn len_bytes(self) -> usize {
                Char(self.to_scalar()).len_bytes() // we need to convert to a scalar
            }

            /// Returns the length of the UTF-8 representation.
            #[must_use]
            #[inline(always)]
            pub const fn len_utf8(self) -> usize {
                Char::UTF8_CHAR_LEN[self.first_utf8_byte() as usize] as usize
            }
        }
    }};
}
impl_charu!();