1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/yarr/ASCIICType.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,183 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * 1.7 + * ***** BEGIN LICENSE BLOCK ***** 1.8 + * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. 1.9 + * 1.10 + * Redistribution and use in source and binary forms, with or without 1.11 + * modification, are permitted provided that the following conditions 1.12 + * are met: 1.13 + * 1.14 + * 1. Redistributions of source code must retain the above copyright 1.15 + * notice, this list of conditions and the following disclaimer. 1.16 + * 2. Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.20 + * its contributors may be used to endorse or promote products derived 1.21 + * from this software without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.24 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.25 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.26 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.27 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.28 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.29 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.30 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.31 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.32 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 + * 1.34 + * ***** END LICENSE BLOCK ***** */ 1.35 + 1.36 +#ifndef yarr_ASCIICType_h 1.37 +#define yarr_ASCIICType_h 1.38 + 1.39 +#include "assembler/wtf/Assertions.h" 1.40 + 1.41 +// The behavior of many of the functions in the <ctype.h> header is dependent 1.42 +// on the current locale. But in the WebKit project, all uses of those functions 1.43 +// are in code processing something that's not locale-specific. These equivalents 1.44 +// for some of the <ctype.h> functions are named more explicitly, not dependent 1.45 +// on the C library locale, and we should also optimize them as needed. 1.46 + 1.47 +// All functions return false or leave the character unchanged if passed a character 1.48 +// that is outside the range 0-7F. So they can be used on Unicode strings or 1.49 +// characters if the intent is to do processing only if the character is ASCII. 1.50 + 1.51 +namespace WTF { 1.52 + 1.53 + inline bool isASCII(char c) { return !(c & ~0x7F); } 1.54 + inline bool isASCII(unsigned short c) { return !(c & ~0x7F); } 1.55 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.56 + inline bool isASCII(wchar_t c) { return !(c & ~0x7F); } 1.57 +#endif 1.58 + inline bool isASCII(int c) { return !(c & ~0x7F); } 1.59 + inline bool isASCII(unsigned c) { return !(c & ~0x7F); } 1.60 + 1.61 + inline bool isASCIIAlpha(char c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; } 1.62 + inline bool isASCIIAlpha(unsigned short c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; } 1.63 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.64 + inline bool isASCIIAlpha(wchar_t c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; } 1.65 +#endif 1.66 + inline bool isASCIIAlpha(int c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; } 1.67 + inline bool isASCIIAlpha(unsigned c) { return (c | 0x20) >= 'a' && (c | 0x20) <= 'z'; } 1.68 + 1.69 + inline bool isASCIIAlphanumeric(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } 1.70 + inline bool isASCIIAlphanumeric(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } 1.71 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.72 + inline bool isASCIIAlphanumeric(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } 1.73 +#endif 1.74 + inline bool isASCIIAlphanumeric(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } 1.75 + inline bool isASCIIAlphanumeric(unsigned c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'z'); } 1.76 + 1.77 + inline bool isASCIIDigit(char c) { return (c >= '0') & (c <= '9'); } 1.78 + inline bool isASCIIDigit(unsigned short c) { return (c >= '0') & (c <= '9'); } 1.79 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.80 + inline bool isASCIIDigit(wchar_t c) { return (c >= '0') & (c <= '9'); } 1.81 +#endif 1.82 + inline bool isASCIIDigit(int c) { return (c >= '0') & (c <= '9'); } 1.83 + inline bool isASCIIDigit(unsigned c) { return (c >= '0') & (c <= '9'); } 1.84 + 1.85 + inline bool isASCIIHexDigit(char c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); } 1.86 + inline bool isASCIIHexDigit(unsigned short c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); } 1.87 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.88 + inline bool isASCIIHexDigit(wchar_t c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); } 1.89 +#endif 1.90 + inline bool isASCIIHexDigit(int c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); } 1.91 + inline bool isASCIIHexDigit(unsigned c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); } 1.92 + 1.93 + inline bool isASCIIOctalDigit(char c) { return (c >= '0') & (c <= '7'); } 1.94 + inline bool isASCIIOctalDigit(unsigned short c) { return (c >= '0') & (c <= '7'); } 1.95 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.96 + inline bool isASCIIOctalDigit(wchar_t c) { return (c >= '0') & (c <= '7'); } 1.97 +#endif 1.98 + inline bool isASCIIOctalDigit(int c) { return (c >= '0') & (c <= '7'); } 1.99 + inline bool isASCIIOctalDigit(unsigned c) { return (c >= '0') & (c <= '7'); } 1.100 + 1.101 + inline bool isASCIILower(char c) { return c >= 'a' && c <= 'z'; } 1.102 + inline bool isASCIILower(unsigned short c) { return c >= 'a' && c <= 'z'; } 1.103 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.104 + inline bool isASCIILower(wchar_t c) { return c >= 'a' && c <= 'z'; } 1.105 +#endif 1.106 + inline bool isASCIILower(int c) { return c >= 'a' && c <= 'z'; } 1.107 + inline bool isASCIILower(unsigned c) { return c >= 'a' && c <= 'z'; } 1.108 + 1.109 + inline bool isASCIIUpper(char c) { return c >= 'A' && c <= 'Z'; } 1.110 + inline bool isASCIIUpper(unsigned short c) { return c >= 'A' && c <= 'Z'; } 1.111 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.112 + inline bool isASCIIUpper(wchar_t c) { return c >= 'A' && c <= 'Z'; } 1.113 +#endif 1.114 + inline bool isASCIIUpper(int c) { return c >= 'A' && c <= 'Z'; } 1.115 + inline bool isASCIIUpper(unsigned c) { return c >= 'A' && c <= 'Z'; } 1.116 + 1.117 + /* 1.118 + Statistics from a run of Apple's page load test for callers of isASCIISpace: 1.119 + 1.120 + character count 1.121 + --------- ----- 1.122 + non-spaces 689383 1.123 + 20 space 294720 1.124 + 0A \n 89059 1.125 + 09 \t 28320 1.126 + 0D \r 0 1.127 + 0C \f 0 1.128 + 0B \v 0 1.129 + */ 1.130 + inline bool isASCIISpace(char c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); } 1.131 + inline bool isASCIISpace(unsigned short c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); } 1.132 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.133 + inline bool isASCIISpace(wchar_t c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); } 1.134 +#endif 1.135 + inline bool isASCIISpace(int c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); } 1.136 + inline bool isASCIISpace(unsigned c) { return c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9)); } 1.137 + 1.138 + inline char toASCIILower(char c) { return c | ((c >= 'A' && c <= 'Z') << 5); } 1.139 + inline unsigned short toASCIILower(unsigned short c) { return c | ((c >= 'A' && c <= 'Z') << 5); } 1.140 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.141 + inline wchar_t toASCIILower(wchar_t c) { return c | ((c >= 'A' && c <= 'Z') << 5); } 1.142 +#endif 1.143 + inline int toASCIILower(int c) { return c | ((c >= 'A' && c <= 'Z') << 5); } 1.144 + inline unsigned toASCIILower(unsigned c) { return c | ((c >= 'A' && c <= 'Z') << 5); } 1.145 + 1.146 + // FIXME: Why do these need static_cast? 1.147 + inline char toASCIIUpper(char c) { return static_cast<char>(c & ~((c >= 'a' && c <= 'z') << 5)); } 1.148 + inline unsigned short toASCIIUpper(unsigned short c) { return static_cast<unsigned short>(c & ~((c >= 'a' && c <= 'z') << 5)); } 1.149 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.150 + inline wchar_t toASCIIUpper(wchar_t c) { return static_cast<wchar_t>(c & ~((c >= 'a' && c <= 'z') << 5)); } 1.151 +#endif 1.152 + inline int toASCIIUpper(int c) { return static_cast<int>(c & ~((c >= 'a' && c <= 'z') << 5)); } 1.153 + inline unsigned toASCIIUpper(unsigned c) { return static_cast<unsigned>(c & ~((c >= 'a' && c <= 'z') << 5)); } 1.154 + 1.155 + inline int toASCIIHexValue(char c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; } 1.156 + inline int toASCIIHexValue(unsigned short c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; } 1.157 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.158 + inline int toASCIIHexValue(wchar_t c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; } 1.159 +#endif 1.160 + inline int toASCIIHexValue(int c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; } 1.161 + inline int toASCIIHexValue(unsigned c) { ASSERT(isASCIIHexDigit(c)); return c < 'A' ? c - '0' : (c - 'A' + 10) & 0xF; } 1.162 + 1.163 + inline bool isASCIIPrintable(char c) { return c >= ' ' && c <= '~'; } 1.164 + inline bool isASCIIPrintable(unsigned short c) { return c >= ' ' && c <= '~'; } 1.165 +#if !WTF_COMPILER_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 1.166 + inline bool isASCIIPrintable(wchar_t c) { return c >= ' ' && c <= '~'; } 1.167 +#endif 1.168 + inline bool isASCIIPrintable(int c) { return c >= ' ' && c <= '~'; } 1.169 + inline bool isASCIIPrintable(unsigned c) { return c >= ' ' && c <= '~'; } 1.170 +} 1.171 + 1.172 +using WTF::isASCII; 1.173 +using WTF::isASCIIAlpha; 1.174 +using WTF::isASCIIAlphanumeric; 1.175 +using WTF::isASCIIDigit; 1.176 +using WTF::isASCIIHexDigit; 1.177 +using WTF::isASCIILower; 1.178 +using WTF::isASCIIOctalDigit; 1.179 +using WTF::isASCIIPrintable; 1.180 +using WTF::isASCIISpace; 1.181 +using WTF::isASCIIUpper; 1.182 +using WTF::toASCIIHexValue; 1.183 +using WTF::toASCIILower; 1.184 +using WTF::toASCIIUpper; 1.185 + 1.186 +#endif /* yarr_ASCIICType_h */