VirtualBox

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

Last change on this file since 96550 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 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 - 0x2aU < 6U /* 2a..2f */
152 || (unsigned)ch - 0x3aU < 7U /* 3a..40 */
153 || (unsigned)ch - 0x5bU < 6U /* 5a..60 */
154 || (unsigned)ch - 0x7bU < 4U /* 7b..7e */;
155}
156
157/**
158 * Checks for a white-space character.
159 *
160 * @returns true / false.
161 * @param ch The character to test.
162 */
163DECL_FORCE_INLINE(bool) RTLocCIsSpace(int ch)
164{
165 return ch == 0x20 /* 20 (space) */
166 || (unsigned)ch - 0x09U < 5U; /* 09..0d */
167}
168
169/**
170 * Checks for an upper case character.
171 *
172 * @returns true / false.
173 * @param ch The character to test.
174 */
175DECL_FORCE_INLINE(bool) RTLocCIsUpper(int ch)
176{
177 return (unsigned)ch - 0x41 < 26U; /* 41..5a */
178}
179
180/**
181 * Checks for a hexadecimal digit.
182 *
183 * @returns true / false.
184 * @param ch The character to test.
185 */
186DECL_FORCE_INLINE(bool) RTLocCIsXDigit(int ch)
187{
188 return (unsigned)ch - 0x30 < 10U /* 30..39 (0-9) */
189 || (unsigned)ch - 0x41 < 6 /* 41..46 (A-F) */
190 || (unsigned)ch - 0x61 < 6; /* 61..66 (a-f) */
191}
192
193/**
194 * Checks for an alphabetic character.
195 *
196 * @returns true / false.
197 * @param ch The character to test.
198 */
199DECL_FORCE_INLINE(bool) RTLocCIsAlpha(int ch)
200{
201 return RTLocCIsLower(ch) || RTLocCIsUpper(ch);
202}
203
204/**
205 * Checks for an alphanumerical character.
206 *
207 * @returns true / false.
208 * @param ch The character to test.
209 */
210DECL_FORCE_INLINE(bool) RTLocCIsAlNum(int ch)
211{
212 return RTLocCIsDigit(ch) || RTLocCIsAlpha(ch);
213}
214
215/**
216 * Checks for a printable character whitespace excluded.
217 *
218 * @returns true / false.
219 * @param ch The character to test.
220 */
221DECL_FORCE_INLINE(bool) RTLocCIsGraph(int ch)
222{
223 return RTLocCIsPrint(ch) && !RTLocCIsBlank(ch);
224}
225
226
227/**
228 * Converts the character to lower case if applicable.
229 *
230 * @returns lower cased character or ch.
231 * @param ch The character to test.
232 */
233DECL_FORCE_INLINE(int) RTLocCToLower(int ch)
234{
235 return RTLocCIsUpper(ch) ? (ch) + 0x20 : (ch);
236}
237
238/**
239 * Converts the character to upper case if applicable.
240 *
241 * @returns upper cased character or ch.
242 * @param ch The character to test.
243 */
244DECL_FORCE_INLINE(int) RTLocCToUpper(int ch)
245{
246 return RTLocCIsLower(ch) ? (ch) - 0x20 : (ch);
247}
248
249
250/** @} */
251
252#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