1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* Copyright (C) 2007-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <array>
#include <concepts>
#include <type_traits>
#include <wtf/Assertions.h>
#include <wtf/text/Latin1Character.h>
// The behavior of many of the functions in the <ctype.h> header is dependent
// on the current locale. But in the WebKit project, all uses of those functions
// are in code processing something that's not locale-specific. These equivalents
// for some of the <ctype.h> functions are named more explicitly, not dependent
// on the C library locale, and we should also optimize them as needed.
// All functions return false or leave the character unchanged if passed a character
// that is outside the range 0-7F. So they can be used on Unicode strings or
// characters if the intent is to do processing only if the character is ASCII.
namespace WTF {
template<typename T>
concept Character = std::convertible_to<std::remove_cv_t<T>, char>;
template<Character CharacterType> constexpr bool isASCII(CharacterType);
template<Character CharacterType> constexpr bool isASCIIAlpha(CharacterType);
template<Character CharacterType> constexpr bool isASCIIAlphanumeric(CharacterType);
template<Character CharacterType> constexpr bool isASCIIBinaryDigit(CharacterType);
template<Character CharacterType> constexpr bool isASCIIDigit(CharacterType);
template<Character CharacterType> constexpr bool isASCIIDigitOrPunctuation(CharacterType);
template<Character CharacterType> constexpr bool isASCIIHexDigit(CharacterType);
template<Character CharacterType> constexpr bool isASCIILower(CharacterType);
template<Character CharacterType> constexpr bool isASCIIOctalDigit(CharacterType);
template<Character CharacterType> constexpr bool isASCIIPrintable(CharacterType);
template<Character CharacterType> constexpr bool isASCIIGraphic(CharacterType);
template<Character CharacterType> constexpr bool isTabOrSpace(CharacterType);
template<Character CharacterType> constexpr bool isASCIIWhitespace(CharacterType);
template<Character CharacterType> constexpr bool isUnicodeCompatibleASCIIWhitespace(CharacterType);
template<Character CharacterType> constexpr bool isASCIIUpper(CharacterType);
// Inverse of isASCIIWhitespace for predicates
template<Character CharacterType> constexpr bool isNotASCIIWhitespace(CharacterType);
template<Character CharacterType> CharacterType toASCIILower(CharacterType);
template<Character CharacterType> CharacterType toASCIIUpper(CharacterType);
template<Character CharacterType> uint8_t toASCIIHexValue(CharacterType);
template<Character CharacterType> uint8_t toASCIIHexValue(CharacterType firstCharacter, CharacterType secondCharacter);
constexpr char lowerNibbleToASCIIHexDigit(uint8_t);
constexpr char upperNibbleToASCIIHexDigit(uint8_t);
constexpr char lowerNibbleToLowercaseASCIIHexDigit(uint8_t);
constexpr char upperNibbleToLowercaseASCIIHexDigit(uint8_t);
template<Character CharacterType> constexpr bool isASCIIAlphaCaselessEqual(CharacterType, char expectedASCIILowercaseLetter);
// The toASCIILowerUnchecked function can be used for comparing any input character
// to a lowercase English character. The isASCIIAlphaCaselessEqual function should
// be used for regular comparison of ASCII alpha characters, but switch statements
// in the CSS tokenizer, for example, instead make direct use toASCIILowerUnchecked.
template<Character CharacterType> constexpr CharacterType toASCIILowerUnchecked(CharacterType);
extern WTF_EXPORT_PRIVATE const std::array<uint8_t, 256> asciiCaseFoldTable;
template<Character CharacterType> constexpr bool isASCII(CharacterType character)
{
return !(character & ~0x7F);
}
template<Character CharacterType> constexpr bool isASCIILower(CharacterType character)
{
return character >= 'a' && character <= 'z';
}
template<Character CharacterType> constexpr CharacterType toASCIILowerUnchecked(CharacterType character)
{
// This function can be used for comparing any input character
// to a lowercase English character. The isASCIIAlphaCaselessEqual
// below should be used for regular comparison of ASCII alpha
// characters, but switch statements in CSS tokenizer instead make
// direct use of this function.
return character | 0x20;
}
template<Character CharacterType> constexpr bool isASCIIAlpha(CharacterType character)
{
return isASCIILower(toASCIILowerUnchecked(character));
}
template<Character CharacterType> constexpr bool isASCIIDigit(CharacterType character)
{
return character >= '0' && character <= '9';
}
template<Character CharacterType> constexpr bool isASCIIAlphanumeric(CharacterType character)
{
return isASCIIDigit(character) || isASCIIAlpha(character);
}
template<Character CharacterType> constexpr bool isASCIIHexDigit(CharacterType character)
{
return isASCIIDigit(character) || (toASCIILowerUnchecked(character) >= 'a' && toASCIILowerUnchecked(character) <= 'f');
}
template<Character CharacterType> constexpr bool isASCIIBinaryDigit(CharacterType character)
{
return character == '0' || character == '1';
}
template<Character CharacterType> constexpr bool isASCIIOctalDigit(CharacterType character)
{
return character >= '0' && character <= '7';
}
template<Character CharacterType> constexpr bool isASCIIPrintable(CharacterType character)
{
return character >= ' ' && character <= '~';
}
template<Character CharacterType> constexpr bool isASCIIGraphic(CharacterType character)
{
return character >= '!' && character <= '~';
}
template<Character CharacterType> constexpr bool isTabOrSpace(CharacterType character)
{
return character == ' ' || character == '\t';
}
// Infra's "ASCII whitespace" <https://infra.spec.whatwg.org/#ascii-whitespace>
template<Character CharacterType> constexpr bool isASCIIWhitespace(CharacterType character)
{
return character == ' ' || character == '\n' || character == '\t' || character == '\r' || character == '\f';
}
template<Character CharacterType> constexpr bool isASCIIWhitespaceWithoutFF(CharacterType character)
{
// This is different from isASCIIWhitespace: JSON/HTTP/XML do not accept \f as a whitespace.
// ECMA-404 specifies the following:
// > Whitespace is any sequence of one or more of the following code points:
// > character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020).
//
// This matches HTTP whitespace:
// https://fetch.spec.whatwg.org/#http-whitespace-byte
//
// And XML whitespace:
// https://www.w3.org/TR/2008/REC-xml-20081126/#NT-S
return character == ' ' || character == '\n' || character == '\t' || character == '\r';
}
template<Character CharacterType> constexpr bool isUnicodeCompatibleASCIIWhitespace(CharacterType character)
{
return isASCIIWhitespace(character) || character == '\v';
}
template<Character CharacterType> constexpr bool isASCIIUpper(CharacterType character)
{
return character >= 'A' && character <= 'Z';
}
template<Character CharacterType> constexpr bool isNotASCIIWhitespace(CharacterType character)
{
return !isASCIIWhitespace(character);
}
template<Character CharacterType> inline CharacterType toASCIILower(CharacterType character)
{
return character | (isASCIIUpper(character) << 5);
}
template<> inline char toASCIILower(char character)
{
return static_cast<char>(asciiCaseFoldTable[static_cast<uint8_t>(character)]);
}
template<> inline Latin1Character toASCIILower(Latin1Character character)
{
return asciiCaseFoldTable[character];
}
template<Character CharacterType> inline CharacterType toASCIIUpper(CharacterType character)
{
return character & ~(isASCIILower(character) << 5);
}
template<Character CharacterType> inline uint8_t toASCIIHexValue(CharacterType character)
{
ASSERT(isASCIIHexDigit(character));
return character < 'A' ? character - '0' : (character - 'A' + 10) & 0xF;
}
template<Character CharacterType> inline uint8_t toASCIIHexValue(CharacterType firstCharacter, CharacterType secondCharacter)
{
return toASCIIHexValue(firstCharacter) << 4 | toASCIIHexValue(secondCharacter);
}
constexpr char lowerNibbleToASCIIHexDigit(uint8_t value)
{
uint8_t nibble = value & 0xF;
return nibble + (nibble < 10 ? '0' : 'A' - 10);
}
constexpr char upperNibbleToASCIIHexDigit(uint8_t value)
{
uint8_t nibble = value >> 4;
return nibble + (nibble < 10 ? '0' : 'A' - 10);
}
constexpr char lowerNibbleToLowercaseASCIIHexDigit(uint8_t value)
{
uint8_t nibble = value & 0xF;
return nibble + (nibble < 10 ? '0' : 'a' - 10);
}
constexpr char upperNibbleToLowercaseASCIIHexDigit(uint8_t value)
{
uint8_t nibble = value >> 4;
return nibble + (nibble < 10 ? '0' : 'a' - 10);
}
template<Character CharacterType> constexpr bool isASCIIAlphaCaselessEqual(CharacterType inputCharacter, char expectedASCIILowercaseLetter)
{
// Name of this argument says this must be a lowercase letter, but it can actually be:
// - a lowercase letter
// - a numeric digit
// - a space
// - punctuation in the range 0x21-0x3F, including "-", "/", and "+"
// It cannot be:
// - an uppercase letter
// - a non-ASCII character
// - other punctuation, such as underscore and backslash
// - a control character such as "\n"
// FIXME: Would be nice to make both the function name and expectedASCIILowercaseLetter argument name clearer.
ASSERT(toASCIILowerUnchecked(expectedASCIILowercaseLetter) == expectedASCIILowercaseLetter);
if (toASCIILowerUnchecked(inputCharacter) == static_cast<CharacterType>(expectedASCIILowercaseLetter)) [[likely]]
return true;
return false;
}
template<Character CharacterType> constexpr bool isASCIIDigitOrPunctuation(CharacterType character)
{
return (character >= '!' && character <= '@') || (character >= '[' && character <= '`') || (character >= '{' && character <= '~');
}
}
using WTF::isASCII;
using WTF::isASCIIAlpha;
using WTF::isASCIIAlphaCaselessEqual;
using WTF::isASCIIAlphanumeric;
using WTF::isASCIIBinaryDigit;
using WTF::isASCIIDigit;
using WTF::isASCIIDigitOrPunctuation;
using WTF::isASCIIHexDigit;
using WTF::isASCIILower;
using WTF::isASCIIOctalDigit;
using WTF::isASCIIPrintable;
using WTF::isASCIIUpper;
using WTF::isASCIIWhitespace;
using WTF::isASCIIWhitespaceWithoutFF;
using WTF::isNotASCIIWhitespace;
using WTF::isTabOrSpace;
using WTF::isUnicodeCompatibleASCIIWhitespace;
using WTF::lowerNibbleToASCIIHexDigit;
using WTF::lowerNibbleToLowercaseASCIIHexDigit;
using WTF::toASCIIHexValue;
using WTF::toASCIILower;
using WTF::toASCIILowerUnchecked;
using WTF::toASCIIUpper;
using WTF::upperNibbleToASCIIHexDigit;
using WTF::upperNibbleToLowercaseASCIIHexDigit;
|