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