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