2 * IPRT - Simple character type classiciation and conversion.
6 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
36 #ifndef IPRT_INCLUDED_ctype_h
37 #define IPRT_INCLUDED_ctype_h
38 #ifndef RT_WITHOUT_PRAGMA_ONCE
42 #include <iprt/types.h>
44 /** @name C locale predicates and conversions.
46 * For most practical purposes, this can safely be used when parsing UTF-8
47 * strings. Just keep in mind that we only deal with the first 127 chars and
48 * that full correctness is only archived using the non-existing RTLocIs* API.
50 * @remarks Use the marcros, not the inlined functions.
52 * @remarks ASSUMES the source code includes the basic ASCII chars. This is a
53 * general IPRT assumption.
55 #define RT_C_IS_BLANK(ch) RTLocCIsBlank((ch))
56 #define RT_C_IS_ALNUM(ch) RTLocCIsAlNum((ch))
57 #define RT_C_IS_ALPHA(ch) RTLocCIsAlpha((ch))
58 #define RT_C_IS_CNTRL(ch) RTLocCIsCntrl((ch))
59 #define RT_C_IS_DIGIT(ch) RTLocCIsDigit((ch))
60 #define RT_C_IS_LOWER(ch) RTLocCIsLower((ch))
61 #define RT_C_IS_GRAPH(ch) RTLocCIsGraph((ch))
62 #define RT_C_IS_ODIGIT(ch) RTLocCIsODigit((ch))
63 #define RT_C_IS_PRINT(ch) RTLocCIsPrint((ch))
64 #define RT_C_IS_PUNCT(ch) RTLocCIsPunct((ch))
65 #define RT_C_IS_SPACE(ch) RTLocCIsSpace((ch))
66 #define RT_C_IS_UPPER(ch) RTLocCIsUpper((ch))
67 #define RT_C_IS_XDIGIT(ch) RTLocCIsXDigit((ch))
69 #define RT_C_TO_LOWER(ch) RTLocCToLower((ch))
70 #define RT_C_TO_UPPER(ch) RTLocCToUpper((ch))
73 * Checks for a blank character.
75 * @returns true / false.
76 * @param ch The character to test.
78 DECL_FORCE_INLINE(bool) RTLocCIsBlank(int ch
)
80 return ch
== 0x20 /* space */
81 || ch
== 0x09; /* horizontal tab */
85 * Checks for a control character.
87 * @returns true / false.
88 * @param ch The character to test.
90 * @note Will return true of ch is '\0'!
92 DECL_FORCE_INLINE(bool) RTLocCIsCntrl(int ch
)
94 return (unsigned)ch
< 32U /* 0..2f */
99 * Checks for a decimal digit.
101 * @returns true / false.
102 * @param ch The character to test.
104 DECL_FORCE_INLINE(bool) RTLocCIsDigit(int ch
)
106 return (unsigned)ch
- 0x30 < 10U; /* 30..39 */
110 * Checks for a lower case character.
112 * @returns true / false.
113 * @param ch The character to test.
115 DECL_FORCE_INLINE(bool) RTLocCIsLower(int ch
)
117 return (unsigned)ch
- 0x61U
< 26U; /* 61..7a */
121 * Checks for an octal digit.
123 * @returns true / false.
124 * @param ch The character to test.
126 DECL_FORCE_INLINE(bool) RTLocCIsODigit(int ch
)
128 return (unsigned)ch
- 0x30 < 8U; /* 30..37 */
132 * Checks for a printable character (whitespace included).
134 * @returns true / false.
135 * @param ch The character to test.
137 DECL_FORCE_INLINE(bool) RTLocCIsPrint(int ch
)
139 return (unsigned)ch
- 0x20U
< 95U; /* 20..7e */
143 * Checks for punctuation (?).
145 * @returns true / false.
146 * @param ch The character to test.
148 DECL_FORCE_INLINE(bool) RTLocCIsPunct(int ch
)
150 return (unsigned)ch
- 0x21U
< 15U /* 21..2f */
151 || (unsigned)ch
- 0x3aU
< 7U /* 3a..40 */
152 || (unsigned)ch
- 0x5bU
< 6U /* 5a..60 */
153 || (unsigned)ch
- 0x7bU
< 4U /* 7b..7e */;
157 * Checks for a white-space character.
159 * @returns true / false.
160 * @param ch The character to test.
162 DECL_FORCE_INLINE(bool) RTLocCIsSpace(int ch
)
164 return ch
== 0x20 /* 20 (space) */
165 || (unsigned)ch
- 0x09U
< 5U; /* 09..0d */
169 * Checks for an upper case character.
171 * @returns true / false.
172 * @param ch The character to test.
174 DECL_FORCE_INLINE(bool) RTLocCIsUpper(int ch
)
176 return (unsigned)ch
- 0x41 < 26U; /* 41..5a */
180 * Checks for a hexadecimal digit.
182 * @returns true / false.
183 * @param ch The character to test.
185 DECL_FORCE_INLINE(bool) RTLocCIsXDigit(int ch
)
187 return (unsigned)ch
- 0x30 < 10U /* 30..39 (0-9) */
188 || (unsigned)ch
- 0x41 < 6 /* 41..46 (A-F) */
189 || (unsigned)ch
- 0x61 < 6; /* 61..66 (a-f) */
193 * Checks for an alphabetic character.
195 * @returns true / false.
196 * @param ch The character to test.
198 DECL_FORCE_INLINE(bool) RTLocCIsAlpha(int ch
)
200 return RTLocCIsLower(ch
) || RTLocCIsUpper(ch
);
204 * Checks for an alphanumerical character.
206 * @returns true / false.
207 * @param ch The character to test.
209 DECL_FORCE_INLINE(bool) RTLocCIsAlNum(int ch
)
211 return RTLocCIsDigit(ch
) || RTLocCIsAlpha(ch
);
215 * Checks for a printable character whitespace excluded.
217 * @returns true / false.
218 * @param ch The character to test.
220 DECL_FORCE_INLINE(bool) RTLocCIsGraph(int ch
)
222 return RTLocCIsPrint(ch
) && !RTLocCIsBlank(ch
);
227 * Converts the character to lower case if applicable.
229 * @returns lower cased character or ch.
230 * @param ch The character to test.
232 DECL_FORCE_INLINE(int) RTLocCToLower(int ch
)
234 return RTLocCIsUpper(ch
) ? (ch
) + 0x20 : (ch
);
238 * Converts the character to upper case if applicable.
240 * @returns upper cased character or ch.
241 * @param ch The character to test.
243 DECL_FORCE_INLINE(int) RTLocCToUpper(int ch
)
245 return RTLocCIsLower(ch
) ? (ch
) - 0x20 : (ch
);
251 #endif /* !IPRT_INCLUDED_ctype_h */