1 | /** @file
|
---|
2 | * IPRT - Simple character type classiciation and conversion.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 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 == ' ' || ch == '\t';
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Checks for a control character.
|
---|
72 | *
|
---|
73 | * @returns true / false.
|
---|
74 | * @param ch The character to test.
|
---|
75 | */
|
---|
76 | DECL_FORCE_INLINE(bool) RTLocCIsCntrl(int ch)
|
---|
77 | {
|
---|
78 | return (ch) >= 0 && (ch) < 32;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Checks for a decimal digit.
|
---|
83 | *
|
---|
84 | * @returns true / false.
|
---|
85 | * @param ch The character to test.
|
---|
86 | */
|
---|
87 | DECL_FORCE_INLINE(bool) RTLocCIsDigit(int ch)
|
---|
88 | {
|
---|
89 | return (ch) >= '0' && (ch) <= '9';
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Checks for a lower case character.
|
---|
94 | *
|
---|
95 | * @returns true / false.
|
---|
96 | * @param ch The character to test.
|
---|
97 | */
|
---|
98 | DECL_FORCE_INLINE(bool) RTLocCIsLower(int ch)
|
---|
99 | {
|
---|
100 | return (ch) >= 'a' && (ch) <= 'z';
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Checks for a octal digit.
|
---|
105 | *
|
---|
106 | * @returns true / false.
|
---|
107 | * @param ch The character to test.
|
---|
108 | */
|
---|
109 | DECL_FORCE_INLINE(bool) RTLocCIsODigit(int ch)
|
---|
110 | {
|
---|
111 | return (ch) >= '0' && (ch) <= '7';
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Checks for a printable character (whitespace included).
|
---|
116 | *
|
---|
117 | * @returns true / false.
|
---|
118 | * @param ch The character to test.
|
---|
119 | */
|
---|
120 | DECL_FORCE_INLINE(bool) RTLocCIsPrint(int ch)
|
---|
121 | {
|
---|
122 | /** @todo quite possibly incorrect */
|
---|
123 | return (ch) >= 32 && (ch) < 127;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Checks for punctuation (?).
|
---|
128 | *
|
---|
129 | * @returns true / false.
|
---|
130 | * @param ch The character to test.
|
---|
131 | */
|
---|
132 | DECL_FORCE_INLINE(bool) RTLocCIsPunct(int ch)
|
---|
133 | {
|
---|
134 | /** @todo possibly incorrect */
|
---|
135 | return (ch) == ',' || (ch) == '.' || (ch) == ':' || (ch) == ';' || (ch) == '!' || (ch) == '?';
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Checks for a white-space character.
|
---|
140 | *
|
---|
141 | * @returns true / false.
|
---|
142 | * @param ch The character to test.
|
---|
143 | */
|
---|
144 | DECL_FORCE_INLINE(bool) RTLocCIsSpace(int ch)
|
---|
145 | {
|
---|
146 | /* \t (9), \n (10), \v (11), \f (12), \r (13), ' ' (32). */
|
---|
147 | return (ch) == ' ' || ((ch) >= 9 && (ch) <= 13);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Checks for an upper case character.
|
---|
152 | *
|
---|
153 | * @returns true / false.
|
---|
154 | * @param ch The character to test.
|
---|
155 | */
|
---|
156 | DECL_FORCE_INLINE(bool) RTLocCIsUpper(int ch)
|
---|
157 | {
|
---|
158 | return (ch) >= 'A' && (ch) <= 'Z';
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Checks for a hexadecimal digit.
|
---|
163 | *
|
---|
164 | * @returns true / false.
|
---|
165 | * @param ch The character to test.
|
---|
166 | */
|
---|
167 | DECL_FORCE_INLINE(bool) RTLocCIsXDigit(int ch)
|
---|
168 | {
|
---|
169 | return RTLocCIsDigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || ((ch) >= 'A' && (ch) <= 'F');
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Checks for an alphabetic character.
|
---|
174 | *
|
---|
175 | * @returns true / false.
|
---|
176 | * @param ch The character to test.
|
---|
177 | */
|
---|
178 | DECL_FORCE_INLINE(bool) RTLocCIsAlpha(int ch)
|
---|
179 | {
|
---|
180 | return RTLocCIsLower(ch) || RTLocCIsUpper(ch);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Checks for an alphanumerical character.
|
---|
185 | *
|
---|
186 | * @returns true / false.
|
---|
187 | * @param ch The character to test.
|
---|
188 | */
|
---|
189 | DECL_FORCE_INLINE(bool) RTLocCIsAlNum(int ch)
|
---|
190 | {
|
---|
191 | return RTLocCIsDigit(ch) || RTLocCIsAlpha(ch);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Checks for a printable character whitespace excluded.
|
---|
196 | *
|
---|
197 | * @returns true / false.
|
---|
198 | * @param ch The character to test.
|
---|
199 | */
|
---|
200 | DECL_FORCE_INLINE(bool) RTLocCIsGraph(int ch)
|
---|
201 | {
|
---|
202 | return RTLocCIsPrint(ch) && !RTLocCIsBlank(ch);
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Converts the character to lower case if applictable.
|
---|
208 | *
|
---|
209 | * @returns lower cased character or ch.
|
---|
210 | * @param ch The character to test.
|
---|
211 | */
|
---|
212 | DECL_FORCE_INLINE(int) RTLocCToLower(int ch)
|
---|
213 | {
|
---|
214 | return RTLocCIsUpper(ch) ? (ch) + ('a' - 'A') : (ch);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Converts the character to upper case if applictable.
|
---|
219 | *
|
---|
220 | * @returns upper cased character or ch.
|
---|
221 | * @param ch The character to test.
|
---|
222 | */
|
---|
223 | DECL_FORCE_INLINE(int) RTLocCToUpper(int ch)
|
---|
224 | {
|
---|
225 | return RTLocCIsLower(ch) ? (ch) - ('a' - 'A') : (ch);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /** @} */
|
---|
230 |
|
---|
231 | #endif
|
---|
232 |
|
---|