VirtualBox

source: vbox/trunk/include/iprt/ctype.h@ 97778

Last change on this file since 97778 was 96851, checked in by vboxsync, 2 years ago

iprt/ctype: Removed duplicate test in RTLocCIsPunct (harmless).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/** @file
2 * IPRT - Simple character type classiciation and conversion.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
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
14 * License.
15 *
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.
20 *
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>.
23 *
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.
29 *
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.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_ctype_h
37#define IPRT_INCLUDED_ctype_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/types.h>
43
44/** @name C locale predicates and conversions.
45 *
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.
49 *
50 * @remarks Use the marcros, not the inlined functions.
51 *
52 * @remarks ASSUMES the source code includes the basic ASCII chars. This is a
53 * general IPRT assumption.
54 * @{ */
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))
68
69#define RT_C_TO_LOWER(ch) RTLocCToLower((ch))
70#define RT_C_TO_UPPER(ch) RTLocCToUpper((ch))
71
72/**
73 * Checks for a blank character.
74 *
75 * @returns true / false.
76 * @param ch The character to test.
77 */
78DECL_FORCE_INLINE(bool) RTLocCIsBlank(int ch)
79{
80 return ch == 0x20 /* space */
81 || ch == 0x09; /* horizontal tab */
82}
83
84/**
85 * Checks for a control character.
86 *
87 * @returns true / false.
88 * @param ch The character to test.
89 *
90 * @note Will return true of ch is '\0'!
91 */
92DECL_FORCE_INLINE(bool) RTLocCIsCntrl(int ch)
93{
94 return (unsigned)ch < 32U /* 0..2f */
95 || ch == 0x7f;
96}
97
98/**
99 * Checks for a decimal digit.
100 *
101 * @returns true / false.
102 * @param ch The character to test.
103 */
104DECL_FORCE_INLINE(bool) RTLocCIsDigit(int ch)
105{
106 return (unsigned)ch - 0x30 < 10U; /* 30..39 */
107}
108
109/**
110 * Checks for a lower case character.
111 *
112 * @returns true / false.
113 * @param ch The character to test.
114 */
115DECL_FORCE_INLINE(bool) RTLocCIsLower(int ch)
116{
117 return (unsigned)ch - 0x61U < 26U; /* 61..7a */
118}
119
120/**
121 * Checks for an octal digit.
122 *
123 * @returns true / false.
124 * @param ch The character to test.
125 */
126DECL_FORCE_INLINE(bool) RTLocCIsODigit(int ch)
127{
128 return (unsigned)ch - 0x30 < 8U; /* 30..37 */
129}
130
131/**
132 * Checks for a printable character (whitespace included).
133 *
134 * @returns true / false.
135 * @param ch The character to test.
136 */
137DECL_FORCE_INLINE(bool) RTLocCIsPrint(int ch)
138{
139 return (unsigned)ch - 0x20U < 95U; /* 20..7e */
140}
141
142/**
143 * Checks for punctuation (?).
144 *
145 * @returns true / false.
146 * @param ch The character to test.
147 */
148DECL_FORCE_INLINE(bool) RTLocCIsPunct(int ch)
149{
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 */;
154}
155
156/**
157 * Checks for a white-space character.
158 *
159 * @returns true / false.
160 * @param ch The character to test.
161 */
162DECL_FORCE_INLINE(bool) RTLocCIsSpace(int ch)
163{
164 return ch == 0x20 /* 20 (space) */
165 || (unsigned)ch - 0x09U < 5U; /* 09..0d */
166}
167
168/**
169 * Checks for an upper case character.
170 *
171 * @returns true / false.
172 * @param ch The character to test.
173 */
174DECL_FORCE_INLINE(bool) RTLocCIsUpper(int ch)
175{
176 return (unsigned)ch - 0x41 < 26U; /* 41..5a */
177}
178
179/**
180 * Checks for a hexadecimal digit.
181 *
182 * @returns true / false.
183 * @param ch The character to test.
184 */
185DECL_FORCE_INLINE(bool) RTLocCIsXDigit(int ch)
186{
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) */
190}
191
192/**
193 * Checks for an alphabetic character.
194 *
195 * @returns true / false.
196 * @param ch The character to test.
197 */
198DECL_FORCE_INLINE(bool) RTLocCIsAlpha(int ch)
199{
200 return RTLocCIsLower(ch) || RTLocCIsUpper(ch);
201}
202
203/**
204 * Checks for an alphanumerical character.
205 *
206 * @returns true / false.
207 * @param ch The character to test.
208 */
209DECL_FORCE_INLINE(bool) RTLocCIsAlNum(int ch)
210{
211 return RTLocCIsDigit(ch) || RTLocCIsAlpha(ch);
212}
213
214/**
215 * Checks for a printable character whitespace excluded.
216 *
217 * @returns true / false.
218 * @param ch The character to test.
219 */
220DECL_FORCE_INLINE(bool) RTLocCIsGraph(int ch)
221{
222 return RTLocCIsPrint(ch) && !RTLocCIsBlank(ch);
223}
224
225
226/**
227 * Converts the character to lower case if applicable.
228 *
229 * @returns lower cased character or ch.
230 * @param ch The character to test.
231 */
232DECL_FORCE_INLINE(int) RTLocCToLower(int ch)
233{
234 return RTLocCIsUpper(ch) ? (ch) + 0x20 : (ch);
235}
236
237/**
238 * Converts the character to upper case if applicable.
239 *
240 * @returns upper cased character or ch.
241 * @param ch The character to test.
242 */
243DECL_FORCE_INLINE(int) RTLocCToUpper(int ch)
244{
245 return RTLocCIsLower(ch) ? (ch) - 0x20 : (ch);
246}
247
248
249/** @} */
250
251#endif /* !IPRT_INCLUDED_ctype_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette