js/src/yarr/ASCIICType.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial