1 | /** @file
|
---|
2 | * IPRT - Simple character type classiciation and conversion.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2015 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_ctype_h
|
---|
27 | #define ___iprt_ctype_h
|
---|
28 |
|
---|
29 | #include <iprt/types.h>
|
---|
30 |
|
---|
31 | /** @name C locale predicates and conversions.
|
---|
32 | *
|
---|
33 | * For most practical purposes, this can safely be used when parsing UTF-8
|
---|
34 | * strings. Just keep in mind that we only deal with the first 127 chars and
|
---|
35 | * that full correctness is only archived using the non-existing RTLocIs* API.
|
---|
36 | *
|
---|
37 | * @remarks Use the marcros, not the inlined functions.
|
---|
38 | *
|
---|
39 | * @remarks ASSUMES the source code includes the basic ASCII chars. This is a
|
---|
40 | * general IPRT assumption.
|
---|
41 | * @{ */
|
---|
42 | #define RT_C_IS_BLANK(ch) RTLocCIsBlank((ch))
|
---|
43 | #define RT_C_IS_ALNUM(ch) RTLocCIsAlNum((ch))
|
---|
44 | #define RT_C_IS_ALPHA(ch) RTLocCIsAlpha((ch))
|
---|
45 | #define RT_C_IS_CNTRL(ch) RTLocCIsCntrl((ch))
|
---|
46 | #define RT_C_IS_DIGIT(ch) RTLocCIsDigit((ch))
|
---|
47 | #define RT_C_IS_LOWER(ch) RTLocCIsLower((ch))
|
---|
48 | #define RT_C_IS_GRAPH(ch) RTLocCIsGraph((ch))
|
---|
49 | #define RT_C_IS_ODIGIT(ch) RTLocCIsODigit((ch))
|
---|
50 | #define RT_C_IS_PRINT(ch) RTLocCIsPrint((ch))
|
---|
51 | #define RT_C_IS_PUNCT(ch) RTLocCIsPunct((ch))
|
---|
52 | #define RT_C_IS_SPACE(ch) RTLocCIsSpace((ch))
|
---|
53 | #define RT_C_IS_UPPER(ch) RTLocCIsUpper((ch))
|
---|
54 | #define RT_C_IS_XDIGIT(ch) RTLocCIsXDigit((ch))
|
---|
55 |
|
---|
56 | #define RT_C_TO_LOWER(ch) RTLocCToLower((ch))
|
---|
57 | #define RT_C_TO_UPPER(ch) RTLocCToUpper((ch))
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Checks for a blank character.
|
---|
61 | *
|
---|
62 | * @returns true / false.
|
---|
63 | * @param ch The character to test.
|
---|
64 | */
|
---|
65 | DECL_FORCE_INLINE(bool) RTLocCIsBlank(int ch)
|
---|
66 | {
|
---|
67 | return ch == 0x20 /* space */
|
---|
68 | || ch == 0x09; /* horizontal tab */
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Checks for a control character.
|
---|
73 | *
|
---|
74 | * @returns true / false.
|
---|
75 | * @param ch The character to test.
|
---|
76 | *
|
---|
77 | * @note Will return true of ch is '\0'!
|
---|
78 | */
|
---|
79 | DECL_FORCE_INLINE(bool) RTLocCIsCntrl(int ch)
|
---|
80 | {
|
---|
81 | return (unsigned)ch < 32U /* 0..2f */
|
---|
82 | || ch == 0x7f;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Checks for a decimal digit.
|
---|
87 | *
|
---|
88 | * @returns true / false.
|
---|
89 | * @param ch The character to test.
|
---|
90 | */
|
---|
91 | DECL_FORCE_INLINE(bool) RTLocCIsDigit(int ch)
|
---|
92 | {
|
---|
93 | return (unsigned)ch - 0x30 < 10U; /* 30..39 */
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Checks for a lower case character.
|
---|
98 | *
|
---|
99 | * @returns true / false.
|
---|
100 | * @param ch The character to test.
|
---|
101 | */
|
---|
102 | DECL_FORCE_INLINE(bool) RTLocCIsLower(int ch)
|
---|
103 | {
|
---|
104 | return (unsigned)ch - 0x61U < 26U; /* 61..7a */
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Checks for an octal digit.
|
---|
109 | *
|
---|
110 | * @returns true / false.
|
---|
111 | * @param ch The character to test.
|
---|
112 | */
|
---|
113 | DECL_FORCE_INLINE(bool) RTLocCIsODigit(int ch)
|
---|
114 | {
|
---|
115 | return (unsigned)ch - 0x30 < 8U; /* 30..37 */
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Checks for a printable character (whitespace included).
|
---|
120 | *
|
---|
121 | * @returns true / false.
|
---|
122 | * @param ch The character to test.
|
---|
123 | */
|
---|
124 | DECL_FORCE_INLINE(bool) RTLocCIsPrint(int ch)
|
---|
125 | {
|
---|
126 | return (unsigned)ch - 0x20U < 95U; /* 20..7e */
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Checks for punctuation (?).
|
---|
131 | *
|
---|
132 | * @returns true / false.
|
---|
133 | * @param ch The character to test.
|
---|
134 | */
|
---|
135 | DECL_FORCE_INLINE(bool) RTLocCIsPunct(int ch)
|
---|
136 | {
|
---|
137 | return (unsigned)ch - 0x21U < 15U /* 21..2f */
|
---|
138 | || (unsigned)ch - 0x2aU < 6U /* 2a..2f */
|
---|
139 | || (unsigned)ch - 0x3aU < 7U /* 3a..40 */
|
---|
140 | || (unsigned)ch - 0x5bU < 6U /* 5a..60 */
|
---|
141 | || (unsigned)ch - 0x7bU < 4U /* 7b..7e */;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Checks for a white-space character.
|
---|
146 | *
|
---|
147 | * @returns true / false.
|
---|
148 | * @param ch The character to test.
|
---|
149 | */
|
---|
150 | DECL_FORCE_INLINE(bool) RTLocCIsSpace(int ch)
|
---|
151 | {
|
---|
152 | return ch == 0x20 /* 20 (space) */
|
---|
153 | || (unsigned)ch - 0x09U < 5U; /* 09..0d */
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Checks for an upper case character.
|
---|
158 | *
|
---|
159 | * @returns true / false.
|
---|
160 | * @param ch The character to test.
|
---|
161 | */
|
---|
162 | DECL_FORCE_INLINE(bool) RTLocCIsUpper(int ch)
|
---|
163 | {
|
---|
164 | return (unsigned)ch - 0x41 < 26U; /* 41..5a */
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Checks for a hexadecimal digit.
|
---|
169 | *
|
---|
170 | * @returns true / false.
|
---|
171 | * @param ch The character to test.
|
---|
172 | */
|
---|
173 | DECL_FORCE_INLINE(bool) RTLocCIsXDigit(int ch)
|
---|
174 | {
|
---|
175 | return (unsigned)ch - 0x30 < 10U /* 30..39 (0-9) */
|
---|
176 | || (unsigned)ch - 0x41 < 6 /* 41..46 (A-F) */
|
---|
177 | || (unsigned)ch - 0x61 < 6; /* 61..66 (a-f) */
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Checks for an alphabetic character.
|
---|
182 | *
|
---|
183 | * @returns true / false.
|
---|
184 | * @param ch The character to test.
|
---|
185 | */
|
---|
186 | DECL_FORCE_INLINE(bool) RTLocCIsAlpha(int ch)
|
---|
187 | {
|
---|
188 | return RTLocCIsLower(ch) || RTLocCIsUpper(ch);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Checks for an alphanumerical character.
|
---|
193 | *
|
---|
194 | * @returns true / false.
|
---|
195 | * @param ch The character to test.
|
---|
196 | */
|
---|
197 | DECL_FORCE_INLINE(bool) RTLocCIsAlNum(int ch)
|
---|
198 | {
|
---|
199 | return RTLocCIsDigit(ch) || RTLocCIsAlpha(ch);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Checks for a printable character whitespace excluded.
|
---|
204 | *
|
---|
205 | * @returns true / false.
|
---|
206 | * @param ch The character to test.
|
---|
207 | */
|
---|
208 | DECL_FORCE_INLINE(bool) RTLocCIsGraph(int ch)
|
---|
209 | {
|
---|
210 | return RTLocCIsPrint(ch) && !RTLocCIsBlank(ch);
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Converts the character to lower case if applictable.
|
---|
216 | *
|
---|
217 | * @returns lower cased character or ch.
|
---|
218 | * @param ch The character to test.
|
---|
219 | */
|
---|
220 | DECL_FORCE_INLINE(int) RTLocCToLower(int ch)
|
---|
221 | {
|
---|
222 | return RTLocCIsUpper(ch) ? (ch) + 0x20 : (ch);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Converts the character to upper case if applictable.
|
---|
227 | *
|
---|
228 | * @returns upper cased character or ch.
|
---|
229 | * @param ch The character to test.
|
---|
230 | */
|
---|
231 | DECL_FORCE_INLINE(int) RTLocCToUpper(int ch)
|
---|
232 | {
|
---|
233 | return RTLocCIsLower(ch) ? (ch) - 0x20 : (ch);
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | /** @} */
|
---|
238 |
|
---|
239 | #endif
|
---|
240 |
|
---|