nstd_sys/core/
cty.rs

1//! Provides functions for examining and operating on character types.
2use crate::{NSTDBool, NSTDChar, NSTDChar32};
3use nstdapi::nstdapi;
4
5/// Determines whether or not a 32-bit character value is a valid Unicode scalar value.
6///
7/// # Parameters:
8///
9/// - `NSTDChar32 chr` - The 32-bit character value to check.
10///
11/// # Returns
12///
13/// `NSTDBool is_unicode` - True if `chr` is a valid Unicode character.
14///
15/// # Example
16///
17/// ```
18/// use nstd_sys::{core::cty::nstd_core_cty_is_unicode, NSTDChar32};
19///
20/// # unsafe {
21/// assert!(nstd_core_cty_is_unicode('🦀' as NSTDChar32));
22/// assert!(!nstd_core_cty_is_unicode(NSTDChar32::MAX));
23/// # }
24/// ```
25#[inline]
26#[nstdapi]
27pub const fn nstd_core_cty_is_unicode(chr: NSTDChar32) -> NSTDBool {
28    char::from_u32(chr).is_some()
29}
30
31/// Generates deterministic functions such as `is_alphabetic` or `is_numeric`.
32macro_rules! gen_deterministic {
33    (
34        $(#[$meta:meta])*
35        $name: ident,
36        $method: ident
37    ) => {
38        $(#[$meta])*
39        #[inline]
40        #[nstdapi]
41        #[allow(trivial_numeric_casts)]
42        pub const fn $name(chr: NSTDChar) -> NSTDBool {
43            (chr as u8).$method()
44        }
45    };
46}
47gen_deterministic!(
48    /// Determines whether or not `chr` is a valid ASCII value.
49    ///
50    /// # Parameters:
51    ///
52    /// - `NSTDChar chr` - The character to check.
53    ///
54    /// # Returns
55    ///
56    /// `NSTDBool is_ascii` - `NSTD_TRUE` if `chr` is a valid ASCII value.
57    ///
58    /// # Example
59    ///
60    /// ```
61    /// use nstd_sys::{core::cty::nstd_core_cty_is_ascii, NSTDChar, NSTD_FALSE};
62    ///
63    /// # unsafe {
64    /// assert!(nstd_core_cty_is_ascii(b'-' as NSTDChar) != NSTD_FALSE);
65    /// assert!(nstd_core_cty_is_ascii(u8::MAX as NSTDChar) == NSTD_FALSE);
66    /// # }
67    /// ```
68    nstd_core_cty_is_ascii,
69    is_ascii
70);
71gen_deterministic!(
72    /// Determines whether or not `chr` is alphabetic.
73    ///
74    /// # Parameters:
75    ///
76    /// - `NSTDChar chr` - The character to check.
77    ///
78    /// # Returns
79    ///
80    /// `NSTDBool is_alphabetic` - `NSTD_TRUE` if `chr` is alphabetic.
81    ///
82    /// # Example
83    ///
84    /// ```
85    /// use nstd_sys::{core::cty::nstd_core_cty_is_alphabetic, NSTDChar, NSTD_FALSE};
86    ///
87    /// # unsafe {
88    /// assert!(nstd_core_cty_is_alphabetic(b'G' as NSTDChar) != NSTD_FALSE);
89    /// assert!(nstd_core_cty_is_alphabetic(b'0' as NSTDChar) == NSTD_FALSE);
90    /// # }
91    /// ```
92    nstd_core_cty_is_alphabetic,
93    is_ascii_alphabetic
94);
95gen_deterministic!(
96    /// Determines whether or not `chr` is numeric.
97    ///
98    /// # Parameters:
99    ///
100    /// - `NSTDChar chr` - The character to check.
101    ///
102    /// # Returns
103    ///
104    /// `NSTDBool is_numeric` - `NSTD_TRUE` if `chr` is numeric.
105    ///
106    /// # Example
107    ///
108    /// ```
109    /// use nstd_sys::{core::cty::nstd_core_cty_is_numeric, NSTDChar, NSTD_FALSE};
110    ///
111    /// # unsafe {
112    /// assert!(nstd_core_cty_is_numeric(b'9' as NSTDChar) != NSTD_FALSE);
113    /// assert!(nstd_core_cty_is_numeric(b'a' as NSTDChar) == NSTD_FALSE);
114    /// # }
115    /// ```
116    nstd_core_cty_is_numeric,
117    is_ascii_digit
118);
119gen_deterministic!(
120    /// Determines whether or not `chr` is alphabetic or numeric.
121    ///
122    /// # Parameters:
123    ///
124    /// - `NSTDChar chr` - The character to check.
125    ///
126    /// # Returns
127    ///
128    /// `NSTDBool is_alphanumeric` - `NSTD_TRUE` if `chr` is alphabetic or numeric.
129    ///
130    /// # Example
131    ///
132    /// ```
133    /// use nstd_sys::{core::cty::nstd_core_cty_is_alphanumeric, NSTDChar, NSTD_FALSE};
134    ///
135    /// # unsafe {
136    /// assert!(nstd_core_cty_is_alphanumeric(b'Z' as NSTDChar) != NSTD_FALSE);
137    /// assert!(nstd_core_cty_is_alphanumeric(b'5' as NSTDChar) != NSTD_FALSE);
138    /// assert!(nstd_core_cty_is_alphanumeric(b';' as NSTDChar) == NSTD_FALSE);
139    /// # }
140    /// ```
141    nstd_core_cty_is_alphanumeric,
142    is_ascii_alphanumeric
143);
144gen_deterministic!(
145    /// Determines whether or not `chr` is a hexadecimal digit.
146    ///
147    /// # Parameters:
148    ///
149    /// - `NSTDChar chr` - The character to check.
150    ///
151    /// # Returns
152    ///
153    /// `NSTDBool is_hexdigit` - `NSTD_TRUE` if `chr` is a hexadecimal digit.
154    ///
155    /// # Example
156    ///
157    /// ```
158    /// use nstd_sys::{core::cty::nstd_core_cty_is_hexdigit, NSTDChar, NSTD_FALSE};
159    ///
160    /// # unsafe {
161    /// assert!(nstd_core_cty_is_hexdigit(b'0' as NSTDChar) != NSTD_FALSE);
162    /// assert!(nstd_core_cty_is_hexdigit(b'F' as NSTDChar) != NSTD_FALSE);
163    /// assert!(nstd_core_cty_is_hexdigit(b';' as NSTDChar) == NSTD_FALSE);
164    /// # }
165    /// ```
166    nstd_core_cty_is_hexdigit,
167    is_ascii_hexdigit
168);
169gen_deterministic!(
170    /// Determines whether or not `chr` is lowercase.
171    ///
172    /// # Parameters:
173    ///
174    /// - `NSTDChar chr` - The character to check.
175    ///
176    /// # Returns
177    ///
178    /// `NSTDBool is_lowercase` - `NSTD_TRUE` if `chr` is lowercase.
179    ///
180    /// # Example
181    ///
182    /// ```
183    /// use nstd_sys::{core::cty::nstd_core_cty_is_lowercase, NSTDChar, NSTD_FALSE};
184    ///
185    /// # unsafe {
186    /// assert!(nstd_core_cty_is_lowercase(b'v' as NSTDChar) != NSTD_FALSE);
187    /// assert!(nstd_core_cty_is_lowercase(b'M' as NSTDChar) == NSTD_FALSE);
188    /// # }
189    /// ```
190    nstd_core_cty_is_lowercase,
191    is_ascii_lowercase
192);
193gen_deterministic!(
194    /// Determines whether or not `chr` is uppercase.
195    ///
196    /// # Parameters:
197    ///
198    /// - `NSTDChar chr` - The character to check.
199    ///
200    /// # Returns
201    ///
202    /// `NSTDBool is_uppercase` - `NSTD_TRUE` if `chr` is uppercase.
203    ///
204    /// # Example
205    ///
206    /// ```
207    /// use nstd_sys::{core::cty::nstd_core_cty_is_uppercase, NSTDChar, NSTD_FALSE};
208    ///
209    /// # unsafe {
210    /// assert!(nstd_core_cty_is_uppercase(b'P' as NSTDChar) != NSTD_FALSE);
211    /// assert!(nstd_core_cty_is_uppercase(b's' as NSTDChar) == NSTD_FALSE);
212    /// # }
213    /// ```
214    nstd_core_cty_is_uppercase,
215    is_ascii_uppercase
216);
217gen_deterministic!(
218    /// Determines whether or not `chr` is white space.
219    ///
220    /// # Parameters:
221    ///
222    /// - `NSTDChar chr` - The character to check.
223    ///
224    /// # Returns
225    ///
226    /// `NSTDBool is_whitespace` - `NSTD_TRUE` if `chr` is white space.
227    ///
228    /// # Example
229    ///
230    /// ```
231    /// use nstd_sys::{core::cty::nstd_core_cty_is_whitespace, NSTDChar, NSTD_FALSE};
232    ///
233    /// # unsafe {
234    /// assert!(nstd_core_cty_is_whitespace(b'\n' as NSTDChar) != NSTD_FALSE);
235    /// assert!(nstd_core_cty_is_whitespace(b'.' as NSTDChar) == NSTD_FALSE);
236    /// # }
237    /// ```
238    nstd_core_cty_is_whitespace,
239    is_ascii_whitespace
240);
241gen_deterministic!(
242    /// Determines whether or not `chr` is a control character.
243    ///
244    /// # Parameters:
245    ///
246    /// - `NSTDChar chr` - The character to check.
247    ///
248    /// # Returns
249    ///
250    /// `NSTDBool is_control` - `NSTD_TRUE` if `chr` is a control character.
251    ///
252    /// # Example
253    ///
254    /// ```
255    /// use nstd_sys::{core::cty::nstd_core_cty_is_control, NSTDChar, NSTD_FALSE};
256    ///
257    /// # unsafe {
258    /// assert!(nstd_core_cty_is_control(b'\0' as NSTDChar) != NSTD_FALSE);
259    /// assert!(nstd_core_cty_is_control(b'\\' as NSTDChar) == NSTD_FALSE);
260    /// # }
261    /// ```
262    nstd_core_cty_is_control,
263    is_ascii_control
264);
265gen_deterministic!(
266    /// Determines whether or not `chr` is punctuation.
267    ///
268    /// # Parameters:
269    ///
270    /// - `NSTDChar chr` - The character to check.
271    ///
272    /// # Returns
273    ///
274    /// `NSTDBool is_punctuation` - `NSTD_TRUE` if `chr` is punctuation.
275    ///
276    /// # Example
277    ///
278    /// ```
279    /// use nstd_sys::{core::cty::nstd_core_cty_is_punctuation, NSTDChar, NSTD_FALSE};
280    ///
281    /// # unsafe {
282    /// assert!(nstd_core_cty_is_punctuation(b'.' as NSTDChar) != NSTD_FALSE);
283    /// assert!(nstd_core_cty_is_punctuation(b'y' as NSTDChar) == NSTD_FALSE);
284    /// # }
285    /// ```
286    nstd_core_cty_is_punctuation,
287    is_ascii_punctuation
288);
289gen_deterministic!(
290    /// Determines whether or not `chr` is a graphical character.
291    ///
292    /// # Parameters:
293    ///
294    /// - `NSTDChar chr` - The character to check.
295    ///
296    /// # Returns
297    ///
298    /// `NSTDBool is_graphic` - `NSTD_TRUE` if `chr` is a graphical character.
299    ///
300    /// # Example
301    ///
302    /// ```
303    /// use nstd_sys::{core::cty::nstd_core_cty_is_graphic, NSTDChar, NSTD_FALSE};
304    ///
305    /// # unsafe {
306    /// assert!(nstd_core_cty_is_graphic(b'.' as NSTDChar) != NSTD_FALSE);
307    /// assert!(nstd_core_cty_is_graphic(b'\t' as NSTDChar) == NSTD_FALSE);
308    /// # }
309    /// ```
310    nstd_core_cty_is_graphic,
311    is_ascii_graphic
312);