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
|
/*
* Copyright (C) 2004-2017 Apple Inc. All rights reserved.
* Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <wtf/text/AtomString.h>
#include <mutex>
#include <wtf/Algorithms.h>
#include <wtf/dtoa.h>
#include <wtf/text/IntegerToStringConversion.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/unicode/CharacterNames.h>
namespace WTF {
const StaticAtomString nullAtomData { nullptr };
const StaticAtomString emptyAtomData { &StringImpl::s_emptyAtomString };
template<AtomString::CaseConvertType type>
ALWAYS_INLINE AtomString AtomString::convertASCIICase() const
{
StringImpl* impl = this->impl();
if (UNLIKELY(!impl))
return nullAtom();
// Convert short strings without allocating a new StringImpl, since
// there's a good chance these strings are already in the atom
// string table and so no memory allocation will be required.
unsigned length;
const unsigned localBufferSize = 100;
if (impl->is8Bit() && (length = impl->length()) <= localBufferSize) {
auto characters = impl->span8();
unsigned failingIndex;
for (unsigned i = 0; i < length; ++i) {
if (type == CaseConvertType::Lower ? UNLIKELY(isASCIIUpper(characters[i])) : LIKELY(isASCIILower(characters[i]))) {
failingIndex = i;
goto SlowPath;
}
}
return *this;
SlowPath:
std::array<LChar, localBufferSize> localBuffer;
for (unsigned i = 0; i < failingIndex; ++i)
localBuffer[i] = characters[i];
for (unsigned i = failingIndex; i < length; ++i)
localBuffer[i] = type == CaseConvertType::Lower ? toASCIILower(characters[i]) : toASCIIUpper(characters[i]);
return std::span<const LChar> { localBuffer }.first(length);
}
Ref<StringImpl> convertedString = type == CaseConvertType::Lower ? impl->convertToASCIILowercase() : impl->convertToASCIIUppercase();
if (LIKELY(convertedString.ptr() == impl))
return *this;
AtomString result;
result.m_string = AtomStringImpl::add(convertedString.ptr());
return result;
}
AtomString AtomString::convertToASCIILowercase() const
{
return convertASCIICase<CaseConvertType::Lower>();
}
AtomString AtomString::convertToASCIIUppercase() const
{
return convertASCIICase<CaseConvertType::Upper>();
}
AtomString AtomString::number(int number)
{
return numberToStringSigned<AtomString>(number);
}
AtomString AtomString::number(unsigned number)
{
return numberToStringUnsigned<AtomString>(number);
}
AtomString AtomString::number(unsigned long number)
{
return numberToStringUnsigned<AtomString>(number);
}
AtomString AtomString::number(unsigned long long number)
{
return numberToStringUnsigned<AtomString>(number);
}
AtomString AtomString::number(float number)
{
NumberToStringBuffer buffer;
auto span = numberToStringAndSize(number, buffer);
return AtomString { byteCast<LChar>(span) };
}
AtomString AtomString::number(double number)
{
NumberToStringBuffer buffer;
auto span = numberToStringAndSize(number, buffer);
return AtomString { byteCast<LChar>(span) };
}
AtomString AtomString::fromUTF8Internal(std::span<const char> characters)
{
ASSERT(!characters.empty());
return AtomStringImpl::add(byteCast<char8_t>(characters));
}
#ifndef NDEBUG
void AtomString::show() const
{
m_string.show();
}
#endif
static inline StringBuilder replaceUnpairedSurrogatesWithReplacementCharacterInternal(StringView view)
{
// Slow path: https://infra.spec.whatwg.org/#javascript-string-convert
// Replaces unpaired surrogates with the replacement character.
StringBuilder result;
result.reserveCapacity(view.length());
for (auto codePoint : view.codePoints()) {
if (U_IS_SURROGATE(codePoint))
result.append(replacementCharacter);
else
result.append(codePoint);
}
return result;
}
AtomString replaceUnpairedSurrogatesWithReplacementCharacter(AtomString&& string)
{
// Fast path for the case where there are no unpaired surrogates.
if (LIKELY(!hasUnpairedSurrogate(string)))
return WTFMove(string);
return replaceUnpairedSurrogatesWithReplacementCharacterInternal(string).toAtomString();
}
String replaceUnpairedSurrogatesWithReplacementCharacter(String&& string)
{
// Fast path for the case where there are no unpaired surrogates.
if (LIKELY(!hasUnpairedSurrogate(string)))
return WTFMove(string);
return replaceUnpairedSurrogatesWithReplacementCharacterInternal(string).toString();
}
} // namespace WTF
|