1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : *
11 : * 1. Redistributions of source code must retain the above copyright
12 : * notice, this list of conditions and the following disclaimer.
13 : * 2. Redistributions in binary form must reproduce the above copyright
14 : * notice, this list of conditions and the following disclaimer in the
15 : * documentation and/or other materials provided with the distribution.
16 : * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 : * its contributors may be used to endorse or promote products derived
18 : * from this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 : * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 : * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 : * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 : * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 : * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 : * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 : * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 : *
31 : * ***** END LICENSE BLOCK ***** */
32 :
33 : #ifndef WTF_ASCIICType_h
34 : #define WTF_ASCIICType_h
35 :
36 : #include "assembler/wtf/Assertions.h"
37 :
38 : // The behavior of many of the functions in the <ctype.h> header is dependent
39 : // on the current locale. But in the WebKit project, all uses of those functions
40 : // are in code processing something that's not locale-specific. These equivalents
41 : // for some of the <ctype.h> functions are named more explicitly, not dependent
42 : // on the C library locale, and we should also optimize them as needed.
43 :
44 : // All functions return false or leave the character unchanged if passed a character
45 : // that is outside the range 0-7F. So they can be used on Unicode strings or
46 : // characters if the intent is to do processing only if the character is ASCII.
47 :
48 : namespace WTF {
49 :
50 : inline bool isASCII(char c) { return !(c & ~0x7F); }
51 22060 : inline bool isASCII(unsigned short c) { return !(c & ~0x7F); }
52 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
53 : inline bool isASCII(wchar_t c) { return !(c & ~0x7F); }
54 : #endif
55 : inline bool isASCII(int c) { return !(c & ~0x7F); }
56 : inline bool isASCII(unsigned c) { return !(c & ~0x7F); }
57 :
58 : inline bool isASCIIAlpha(char c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
59 25602 : inline bool isASCIIAlpha(unsigned short c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
60 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
61 : inline bool isASCIIAlpha(wchar_t c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
62 : #endif
63 0 : inline bool isASCIIAlpha(int c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
64 : inline bool isASCIIAlpha(unsigned c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; }
65 :
66 : inline bool isASCIIAlphanumeric(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
67 : inline bool isASCIIAlphanumeric(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
68 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
69 : inline bool isASCIIAlphanumeric(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
70 : #endif
71 0 : inline bool isASCIIAlphanumeric(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
72 : inline bool isASCIIAlphanumeric(unsigned c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); }
73 :
74 : inline bool isASCIIDigit(char c) { return (c >= '0') & (c <= '9'); }
75 : inline bool isASCIIDigit(unsigned short c) { return (c >= '0') & (c <= '9'); }
76 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
77 : inline bool isASCIIDigit(wchar_t c) { return (c >= '0') & (c <= '9'); }
78 : #endif
79 36625 : inline bool isASCIIDigit(int c) { return (c >= '0') & (c <= '9'); }
80 : inline bool isASCIIDigit(unsigned c) { return (c >= '0') & (c <= '9'); }
81 :
82 : inline bool isASCIIHexDigit(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
83 : inline bool isASCIIHexDigit(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
84 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
85 : inline bool isASCIIHexDigit(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
86 : #endif
87 34492 : inline bool isASCIIHexDigit(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
88 : inline bool isASCIIHexDigit(unsigned c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
89 :
90 : inline bool isASCIIOctalDigit(char c) { return (c >= '0') & (c <= '7'); }
91 : inline bool isASCIIOctalDigit(unsigned short c) { return (c >= '0') & (c <= '7'); }
92 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
93 : inline bool isASCIIOctalDigit(wchar_t c) { return (c >= '0') & (c <= '7'); }
94 : #endif
95 44 : inline bool isASCIIOctalDigit(int c) { return (c >= '0') & (c <= '7'); }
96 : inline bool isASCIIOctalDigit(unsigned c) { return (c >= '0') & (c <= '7'); }
97 :
98 10920 : inline bool isASCIILower(char c) { return c >= 'a' && c <= 'z'; }
99 : inline bool isASCIILower(unsigned short c) { return c >= 'a' && c <= 'z'; }
100 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
101 : inline bool isASCIILower(wchar_t c) { return c >= 'a' && c <= 'z'; }
102 : #endif
103 : inline bool isASCIILower(int c) { return c >= 'a' && c <= 'z'; }
104 : inline bool isASCIILower(unsigned c) { return c >= 'a' && c <= 'z'; }
105 :
106 7342 : inline bool isASCIIUpper(char c) { return c >= 'A' && c <= 'Z'; }
107 : inline bool isASCIIUpper(unsigned short c) { return c >= 'A' && c <= 'Z'; }
108 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
109 : inline bool isASCIIUpper(wchar_t c) { return c >= 'A' && c <= 'Z'; }
110 : #endif
111 : inline bool isASCIIUpper(int c) { return c >= 'A' && c <= 'Z'; }
112 : inline bool isASCIIUpper(unsigned c) { return c >= 'A' && c <= 'Z'; }
113 :
114 : /*
115 : Statistics from a run of Apple's page load test for callers of isASCIISpace:
116 :
117 : character count
118 : --------- -----
119 : non-spaces 689383
120 : 20 space 294720
121 : 0A \n 89059
122 : 09 \t 28320
123 : 0D \r 0
124 : 0C \f 0
125 : 0B \v 0
126 : */
127 : inline bool isASCIISpace(char c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
128 : inline bool isASCIISpace(unsigned short c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
129 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
130 : inline bool isASCIISpace(wchar_t c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
131 : #endif
132 : inline bool isASCIISpace(int c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
133 : inline bool isASCIISpace(unsigned c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); }
134 :
135 : inline char toASCIILower(char c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
136 3578 : inline unsigned short toASCIILower(unsigned short c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
137 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
138 : inline wchar_t toASCIILower(wchar_t c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
139 : #endif
140 : inline int toASCIILower(int c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
141 : inline unsigned toASCIILower(unsigned c) { return c | ((c >= 'A' && c <= 'Z') << 5); }
142 :
143 : // FIXME: Why do these need static_cast?
144 : inline char toASCIIUpper(char c) { return static_cast<char>(c & ~((c >= 'a' && c <= 'z') << 5)); }
145 3578 : inline unsigned short toASCIIUpper(unsigned short c) { return static_cast<unsigned short>(c & ~((c >= 'a' && c <= 'z') << 5)); }
146 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
147 : inline wchar_t toASCIIUpper(wchar_t c) { return static_cast<wchar_t>(c & ~((c >= 'a' && c <= 'z') << 5)); }
148 : #endif
149 : inline int toASCIIUpper(int c) { return static_cast<int>(c & ~((c >= 'a' && c <= 'z') << 5)); }
150 : inline unsigned toASCIIUpper(unsigned c) { return static_cast<unsigned>(c & ~((c >= 'a' && c <= 'z') << 5)); }
151 :
152 : inline int toASCIIHexValue(char c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
153 : inline int toASCIIHexValue(unsigned short c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
154 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
155 : inline int toASCIIHexValue(wchar_t c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
156 : #endif
157 17246 : inline int toASCIIHexValue(int c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
158 : inline int toASCIIHexValue(unsigned c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; }
159 :
160 : inline bool isASCIIPrintable(char c) { return c >= ' ' && c <= '~'; }
161 : inline bool isASCIIPrintable(unsigned short c) { return c >= ' ' && c <= '~'; }
162 : #if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED)
163 : inline bool isASCIIPrintable(wchar_t c) { return c >= ' ' && c <= '~'; }
164 : #endif
165 : inline bool isASCIIPrintable(int c) { return c >= ' ' && c <= '~'; }
166 : inline bool isASCIIPrintable(unsigned c) { return c >= ' ' && c <= '~'; }
167 : }
168 :
169 : using WTF::isASCII;
170 : using WTF::isASCIIAlpha;
171 : using WTF::isASCIIAlphanumeric;
172 : using WTF::isASCIIDigit;
173 : using WTF::isASCIIHexDigit;
174 : using WTF::isASCIILower;
175 : using WTF::isASCIIOctalDigit;
176 : using WTF::isASCIIPrintable;
177 : using WTF::isASCIISpace;
178 : using WTF::isASCIIUpper;
179 : using WTF::toASCIIHexValue;
180 : using WTF::toASCIILower;
181 : using WTF::toASCIIUpper;
182 :
183 : #endif
|