File indexing completed on 2024-05-12 15:43:37
0001 /* 0002 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 0003 * 0004 * Redistribution and use in source and binary forms, with or without 0005 * modification, are permitted provided that the following conditions 0006 * are met: 0007 * 0008 * 1. Redistributions of source code must retain the above copyright 0009 * notice, this list of conditions and the following disclaimer. 0010 * 2. Redistributions in binary form must reproduce the above copyright 0011 * notice, this list of conditions and the following disclaimer in the 0012 * documentation and/or other materials provided with the distribution. 0013 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 0014 * its contributors may be used to endorse or promote products derived 0015 * from this software without specific prior written permission. 0016 * 0017 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 0018 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 0019 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 0020 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 0021 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 0022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 0023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 0024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0027 */ 0028 0029 #ifndef WTF_ASCIICType_h 0030 #define WTF_ASCIICType_h 0031 0032 #include <wtf/Platform.h> 0033 #include <wtf/Assertions.h> 0034 0035 // The behavior of many of the functions in the <ctype.h> header is dependent 0036 // on the current locale. But in the WebKit project, all uses of those functions 0037 // are in code processing something that's not locale-specific. These equivalents 0038 // for some of the <ctype.h> functions are named more explicitly, not dependent 0039 // on the C library locale, and we should also optimize them as needed. 0040 0041 // All functions return false or leave the character unchanged if passed a character 0042 // that is outside the range 0-7F. So they can be used on Unicode strings or 0043 // characters if the intent is to do processing only if the character is ASCII. 0044 0045 namespace WTF 0046 { 0047 0048 inline bool isASCIIAlpha(char c) 0049 { 0050 return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; 0051 } 0052 inline bool isASCIIAlpha(unsigned short c) 0053 { 0054 return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; 0055 } 0056 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0057 inline bool isASCIIAlpha(wchar_t c) 0058 { 0059 return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; 0060 } 0061 #endif 0062 inline bool isASCIIAlpha(int c) 0063 { 0064 return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; 0065 } 0066 0067 inline bool isASCIIAlphanumeric(char c) 0068 { 0069 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); 0070 } 0071 inline bool isASCIIAlphanumeric(unsigned short c) 0072 { 0073 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); 0074 } 0075 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0076 inline bool isASCIIAlphanumeric(wchar_t c) 0077 { 0078 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); 0079 } 0080 #endif 0081 inline bool isASCIIAlphanumeric(int c) 0082 { 0083 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); 0084 } 0085 0086 inline bool isASCIIDigit(char c) 0087 { 0088 return (c >= '0') & (c <= '9'); 0089 } 0090 inline bool isASCIIDigit(unsigned short c) 0091 { 0092 return (c >= '0') & (c <= '9'); 0093 } 0094 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0095 inline bool isASCIIDigit(wchar_t c) 0096 { 0097 return (c >= '0') & (c <= '9'); 0098 } 0099 #endif 0100 inline bool isASCIIDigit(int c) 0101 { 0102 return (c >= '0') & (c <= '9'); 0103 } 0104 0105 inline bool isASCIIHexDigit(char c) 0106 { 0107 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); 0108 } 0109 inline bool isASCIIHexDigit(unsigned short c) 0110 { 0111 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); 0112 } 0113 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0114 inline bool isASCIIHexDigit(wchar_t c) 0115 { 0116 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); 0117 } 0118 #endif 0119 inline bool isASCIIHexDigit(int c) 0120 { 0121 return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); 0122 } 0123 0124 inline bool isASCIILower(char c) 0125 { 0126 return c >= 'a' && c <= 'z'; 0127 } 0128 inline bool isASCIILower(unsigned short c) 0129 { 0130 return c >= 'a' && c <= 'z'; 0131 } 0132 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0133 inline bool isASCIILower(wchar_t c) 0134 { 0135 return c >= 'a' && c <= 'z'; 0136 } 0137 #endif 0138 inline bool isASCIILower(int c) 0139 { 0140 return c >= 'a' && c <= 'z'; 0141 } 0142 0143 inline bool isASCIIUpper(char c) 0144 { 0145 return c >= 'A' && c <= 'Z'; 0146 } 0147 inline bool isASCIIUpper(unsigned short c) 0148 { 0149 return c >= 'A' && c <= 'Z'; 0150 } 0151 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0152 inline bool isASCIIUpper(wchar_t c) 0153 { 0154 return c >= 'A' && c <= 'Z'; 0155 } 0156 #endif 0157 inline bool isASCIIUpper(int c) 0158 { 0159 return c >= 'A' && c <= 'Z'; 0160 } 0161 0162 /* 0163 Statistics from a run of Apple's page load test for callers of isASCIISpace: 0164 0165 character count 0166 --------- ----- 0167 non-spaces 689383 0168 20 space 294720 0169 0A \n 89059 0170 09 \t 28320 0171 0D \r 0 0172 0C \f 0 0173 0B \v 0 0174 */ 0175 inline bool isASCIISpace(char c) 0176 { 0177 return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); 0178 } 0179 inline bool isASCIISpace(unsigned short c) 0180 { 0181 return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); 0182 } 0183 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0184 inline bool isASCIISpace(wchar_t c) 0185 { 0186 return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); 0187 } 0188 #endif 0189 inline bool isASCIISpace(int c) 0190 { 0191 return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); 0192 } 0193 0194 inline char toASCIILower(char c) 0195 { 0196 return c | ((c >= 'A' && c <= 'Z') << 5); 0197 } 0198 inline unsigned short toASCIILower(unsigned short c) 0199 { 0200 return c | ((c >= 'A' && c <= 'Z') << 5); 0201 } 0202 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0203 inline wchar_t toASCIILower(wchar_t c) 0204 { 0205 return c | ((c >= 'A' && c <= 'Z') << 5); 0206 } 0207 #endif 0208 inline int toASCIILower(int c) 0209 { 0210 return c | ((c >= 'A' && c <= 'Z') << 5); 0211 } 0212 0213 inline char toASCIIUpper(char c) 0214 { 0215 return static_cast<char>(c & ~((c >= 'a' && c <= 'z') << 5)); 0216 } 0217 inline unsigned short toASCIIUpper(unsigned short c) 0218 { 0219 return static_cast<unsigned short>(c & ~((c >= 'a' && c <= 'z') << 5)); 0220 } 0221 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0222 inline wchar_t toASCIIUpper(wchar_t c) 0223 { 0224 return static_cast<wchar_t>(c & ~((c >= 'a' && c <= 'z') << 5)); 0225 } 0226 #endif 0227 inline int toASCIIUpper(int c) 0228 { 0229 return static_cast<int>(c & ~((c >= 'a' && c <= 'z') << 5)); 0230 } 0231 0232 inline int toASCIIHexValue(char c) 0233 { 0234 ASSERT(isASCIIHexDigit(c)); 0235 return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; 0236 } 0237 inline int toASCIIHexValue(unsigned short c) 0238 { 0239 ASSERT(isASCIIHexDigit(c)); 0240 return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; 0241 } 0242 #if !defined(WTF_COMPILER_MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 0243 inline int toASCIIHexValue(wchar_t c) 0244 { 0245 ASSERT(isASCIIHexDigit(c)); 0246 return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; 0247 } 0248 #endif 0249 inline int toASCIIHexValue(int c) 0250 { 0251 ASSERT(isASCIIHexDigit(c)); 0252 return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; 0253 } 0254 0255 } 0256 0257 #endif