VirtualBox

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

Last change on this file since 78377 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/** @file
2 * IPRT - Simple character type classiciation and conversion.
3 */
4
5/*
6 * Copyright (C) 2006-2019 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_ctype_h
27#define IPRT_INCLUDED_ctype_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33
34/** @name C locale predicates and conversions.
35 *
36 * For most practical purposes, this can safely be used when parsing UTF-8
37 * strings. Just keep in mind that we only deal with the first 127 chars and
38 * that full correctness is only archived using the non-existing RTLocIs* API.
39 *
40 * @remarks Use the marcros, not the inlined functions.
41 *
42 * @remarks ASSUMES the source code includes the basic ASCII chars. This is a
43 * general IPRT assumption.
44 * @{ */
45#define RT_C_IS_BLANK(ch) RTLocCIsBlank((ch))
46#define RT_C_IS_ALNUM(ch) RTLocCIsAlNum((ch))
47#define RT_C_IS_ALPHA(ch) RTLocCIsAlpha((ch))
48#define RT_C_IS_CNTRL(ch) RTLocCIsCntrl((ch))
49#define RT_C_IS_DIGIT(ch) RTLocCIsDigit((ch))
50#define RT_C_IS_LOWER(ch) RTLocCIsLower((ch))
51#define RT_C_IS_GRAPH(ch) RTLocCIsGraph((ch))
52#define RT_C_IS_ODIGIT(ch) RTLocCIsODigit((ch))
53#define RT_C_IS_PRINT(ch) RTLocCIsPrint((ch))
54#define RT_C_IS_PUNCT(ch) RTLocCIsPunct((ch))
55#define RT_C_IS_SPACE(ch) RTLocCIsSpace((ch))
56#define RT_C_IS_UPPER(ch) RTLocCIsUpper((ch))
57#define RT_C_IS_XDIGIT(ch) RTLocCIsXDigit((ch))
58
59#define RT_C_TO_LOWER(ch) RTLocCToLower((ch))
60#define RT_C_TO_UPPER(ch) RTLocCToUpper((ch))
61
62/**
63 * Checks for a blank character.
64 *
65 * @returns true / false.
66 * @param ch The character to test.
67 */
68DECL_FORCE_INLINE(bool) RTLocCIsBlank(int ch)
69{
70 return ch == 0x20 /* space */
71 || ch == 0x09; /* horizontal tab */
72}
73
74/**
75 * Checks for a control character.
76 *
77 * @returns true / false.
78 * @param ch The character to test.
79 *
80 * @note Will return true of ch is '\0'!
81 */
82DECL_FORCE_INLINE(bool) RTLocCIsCntrl(int ch)
83{
84 return (unsigned)ch < 32U /* 0..2f */
85 || ch == 0x7f;
86}
87
88/**
89 * Checks for a decimal digit.
90 *
91 * @returns true / false.
92 * @param ch The character to test.
93 */
94DECL_FORCE_INLINE(bool) RTLocCIsDigit(int ch)
95{
96 return (unsigned)ch - 0x30 < 10U; /* 30..39 */
97}
98
99/**
100 * Checks for a lower case character.
101 *
102 * @returns true / false.
103 * @param ch The character to test.
104 */
105DECL_FORCE_INLINE(bool) RTLocCIsLower(int ch)
106{
107 return (unsigned)ch - 0x61U < 26U; /* 61..7a */
108}
109
110/**
111 * Checks for an octal digit.
112 *
113 * @returns true / false.
114 * @param ch The character to test.
115 */
116DECL_FORCE_INLINE(bool) RTLocCIsODigit(int ch)
117{
118 return (unsigned)ch - 0x30 < 8U; /* 30..37 */
119}
120
121/**
122 * Checks for a printable character (whitespace included).
123 *
124 * @returns true / false.
125 * @param ch The character to test.
126 */
127DECL_FORCE_INLINE(bool) RTLocCIsPrint(int ch)
128{
129 return (unsigned)ch - 0x20U < 95U; /* 20..7e */
130}
131
132/**
133 * Checks for punctuation (?).
134 *
135 * @returns true / false.
136 * @param ch The character to test.
137 */
138DECL_FORCE_INLINE(bool) RTLocCIsPunct(int ch)
139{
140 return (unsigned)ch - 0x21U < 15U /* 21..2f */
141 || (unsigned)ch - 0x2aU < 6U /* 2a..2f */
142 || (unsigned)ch - 0x3aU < 7U /* 3a..40 */
143 || (unsigned)ch - 0x5bU < 6U /* 5a..60 */
144 || (unsigned)ch - 0x7bU < 4U /* 7b..7e */;
145}
146
147/**
148 * Checks for a white-space character.
149 *
150 * @returns true / false.
151 * @param ch The character to test.
152 */
153DECL_FORCE_INLINE(bool) RTLocCIsSpace(int ch)
154{
155 return ch == 0x20 /* 20 (space) */
156 || (unsigned)ch - 0x09U < 5U; /* 09..0d */
157}
158
159/**
160 * Checks for an upper case character.
161 *
162 * @returns true / false.
163 * @param ch The character to test.
164 */
165DECL_FORCE_INLINE(bool) RTLocCIsUpper(int ch)
166{
167 return (unsigned)ch - 0x41 < 26U; /* 41..5a */
168}
169
170/**
171 * Checks for a hexadecimal digit.
172 *
173 * @returns true / false.
174 * @param ch The character to test.
175 */
176DECL_FORCE_INLINE(bool) RTLocCIsXDigit(int ch)
177{
178 return (unsigned)ch - 0x30 < 10U /* 30..39 (0-9) */
179 || (unsigned)ch - 0x41 < 6 /* 41..46 (A-F) */
180 || (unsigned)ch - 0x61 < 6; /* 61..66 (a-f) */
181}
182
183/**
184 * Checks for an alphabetic character.
185 *
186 * @returns true / false.
187 * @param ch The character to test.
188 */
189DECL_FORCE_INLINE(bool) RTLocCIsAlpha(int ch)
190{
191 return RTLocCIsLower(ch) || RTLocCIsUpper(ch);
192}
193
194/**
195 * Checks for an alphanumerical character.
196 *
197 * @returns true / false.
198 * @param ch The character to test.
199 */
200DECL_FORCE_INLINE(bool) RTLocCIsAlNum(int ch)
201{
202 return RTLocCIsDigit(ch) || RTLocCIsAlpha(ch);
203}
204
205/**
206 * Checks for a printable character whitespace excluded.
207 *
208 * @returns true / false.
209 * @param ch The character to test.
210 */
211DECL_FORCE_INLINE(bool) RTLocCIsGraph(int ch)
212{
213 return RTLocCIsPrint(ch) && !RTLocCIsBlank(ch);
214}
215
216
217/**
218 * Converts the character to lower case if applictable.
219 *
220 * @returns lower cased character or ch.
221 * @param ch The character to test.
222 */
223DECL_FORCE_INLINE(int) RTLocCToLower(int ch)
224{
225 return RTLocCIsUpper(ch) ? (ch) + 0x20 : (ch);
226}
227
228/**
229 * Converts the character to upper case if applictable.
230 *
231 * @returns upper cased character or ch.
232 * @param ch The character to test.
233 */
234DECL_FORCE_INLINE(int) RTLocCToUpper(int ch)
235{
236 return RTLocCIsLower(ch) ? (ch) - 0x20 : (ch);
237}
238
239
240/** @} */
241
242#endif /* !IPRT_INCLUDED_ctype_h */
243
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