Skip to main content

qubit_text_codec/charset/
utf32.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use crate::{
11    ByteOrder,
12    Unicode,
13    UnicodeBom,
14};
15
16/// Namespace for UTF-32 constants and code-unit classification helpers.
17pub enum Utf32 {}
18
19impl Utf32 {
20    /// Maximum number of UTF-32 code units needed for one Unicode scalar value.
21    pub const MAX_UNITS_PER_CHAR: usize = 1;
22
23    /// Maximum number of serialized UTF-32 bytes needed for one Unicode scalar value.
24    pub const MAX_BYTES_PER_CHAR: usize = 4;
25
26    /// Tests whether a UTF-32 unit is a valid Unicode scalar value.
27    ///
28    /// # Parameters
29    ///
30    /// - `unit`: The UTF-32 unit to test.
31    ///
32    /// # Returns
33    ///
34    /// Returns `true` if `unit` is a valid Unicode scalar value.
35    #[inline]
36    pub const fn is_valid_unit(unit: u32) -> bool {
37        Unicode::is_scalar_value(unit)
38    }
39
40    /// Returns the UTF-32 unit count needed for a character.
41    ///
42    /// # Parameters
43    ///
44    /// - `_ch`: The character to size.
45    ///
46    /// # Returns
47    ///
48    /// Always returns `1`.
49    #[inline]
50    pub const fn unit_len(_ch: char) -> usize {
51        1
52    }
53
54    /// Detects a UTF-32 BOM and returns its byte order.
55    ///
56    /// # Parameters
57    ///
58    /// - `bytes`: The byte buffer to inspect.
59    ///
60    /// # Returns
61    ///
62    /// Returns `Some(ByteOrder)` for UTF-32 BOM prefixes, or `None` otherwise.
63    #[inline]
64    pub fn detect_bom(bytes: &[u8]) -> Option<ByteOrder> {
65        match UnicodeBom::detect(bytes) {
66            Some(UnicodeBom::Utf32BigEndian) => Some(ByteOrder::BigEndian),
67            Some(UnicodeBom::Utf32LittleEndian) => Some(ByteOrder::LittleEndian),
68            _ => None,
69        }
70    }
71}