#ifndef StringImpl_h
#define StringImpl_h
#include <limits.h>
#include <wtf/ASCIICType.h>
#include <wtf/Forward.h>
#include <wtf/OwnFastMallocPtr.h>
#include <wtf/StdLibExtras.h>
#include <wtf/StringHasher.h>
#include <wtf/Vector.h>
#include <wtf/unicode/Unicode.h>
#if USE(CF)
typedef const struct __CFString * CFStringRef;
#endif
#ifdef __OBJC__
@class NSString;
#endif
namespace JSC {
struct IdentifierCStringTranslator;
template <typename T> struct IdentifierCharBufferTranslator;
struct IdentifierLCharFromUCharTranslator;
}
namespace WTF {
struct CStringTranslator;
struct HashAndCharactersTranslator;
struct HashAndUTF8CharactersTranslator;
struct UCharBufferTranslator;
enum TextCaseSensitivity { TextCaseSensitive, TextCaseInsensitive };
typedef bool (*CharacterMatchFunctionPtr)(UChar);
typedef bool (*IsWhiteSpaceFunctionPtr)(UChar);
class StringImpl {
WTF_MAKE_NONCOPYABLE(StringImpl); WTF_MAKE_FAST_ALLOCATED;
friend struct JSC::IdentifierCStringTranslator;
friend struct JSC::IdentifierCharBufferTranslator<LChar>;
friend struct JSC::IdentifierCharBufferTranslator<UChar>;
friend struct JSC::IdentifierLCharFromUCharTranslator;
friend struct WTF::CStringTranslator;
friend struct WTF::HashAndCharactersTranslator;
friend struct WTF::HashAndUTF8CharactersTranslator;
friend struct WTF::UCharBufferTranslator;
friend class AtomicStringImpl;
private:
enum BufferOwnership {
BufferInternal,
BufferOwned,
BufferSubstring,
};
enum ConstructStaticStringTag { ConstructStaticString };
StringImpl(const UChar* characters, unsigned length, ConstructStaticStringTag)
: m_refCount(s_refCountFlagIsStaticString)
, m_length(length)
, m_data16(characters)
, m_buffer(0)
, m_hashAndFlags(s_hashFlagIsIdentifier | BufferOwned)
{
hash();
}
StringImpl(const LChar* characters, unsigned length, ConstructStaticStringTag)
: m_refCount(s_refCountFlagIsStaticString)
, m_length(length)
, m_data8(characters)
, m_buffer(0)
, m_hashAndFlags(s_hashFlag8BitBuffer | s_hashFlagIsIdentifier | BufferOwned)
{
hash();
}
enum Force8Bit { Force8BitConstructor };
StringImpl(unsigned length, Force8Bit)
: m_refCount(s_refCountIncrement)
, m_length(length)
, m_data8(reinterpret_cast<const LChar*>(this + 1))
, m_buffer(0)
, m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal)
{
ASSERT(m_data8);
ASSERT(m_length);
}
StringImpl(unsigned length)
: m_refCount(s_refCountIncrement)
, m_length(length)
, m_data16(reinterpret_cast<const UChar*>(this + 1))
, m_buffer(0)
, m_hashAndFlags(BufferInternal)
{
ASSERT(m_data16);
ASSERT(m_length);
}
StringImpl(const LChar* characters, unsigned length)
: m_refCount(s_refCountIncrement)
, m_length(length)
, m_data8(characters)
, m_buffer(0)
, m_hashAndFlags(s_hashFlag8BitBuffer | BufferOwned)
{
ASSERT(m_data8);
ASSERT(m_length);
}
StringImpl(const UChar* characters, unsigned length)
: m_refCount(s_refCountIncrement)
, m_length(length)
, m_data16(characters)
, m_buffer(0)
, m_hashAndFlags(BufferOwned)
{
ASSERT(m_data16);
ASSERT(m_length);
}
StringImpl(const LChar* characters, unsigned length, PassRefPtr<StringImpl> base)
: m_refCount(s_refCountIncrement)
, m_length(length)
, m_data8(characters)
, m_substringBuffer(base.leakRef())
, m_hashAndFlags(s_hashFlag8BitBuffer | BufferSubstring)
{
ASSERT(is8Bit());
ASSERT(m_data8);
ASSERT(m_length);
ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
}
StringImpl(const UChar* characters, unsigned length, PassRefPtr<StringImpl> base)
: m_refCount(s_refCountIncrement)
, m_length(length)
, m_data16(characters)
, m_substringBuffer(base.leakRef())
, m_hashAndFlags(BufferSubstring)
{
ASSERT(!is8Bit());
ASSERT(m_data16);
ASSERT(m_length);
ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
}
public:
~StringImpl();
static PassRefPtr<StringImpl> create(const UChar*, unsigned length);
static PassRefPtr<StringImpl> create(const LChar*, unsigned length);
ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s, unsigned length) { return create(reinterpret_cast<const LChar*>(s), length); }
static PassRefPtr<StringImpl> create(const LChar*);
ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s) { return create(reinterpret_cast<const LChar*>(s)); }
static ALWAYS_INLINE PassRefPtr<StringImpl> create8(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
{
ASSERT(rep);
ASSERT(length <= rep->length());
if (!length)
return empty();
ASSERT(rep->is8Bit());
StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
return adoptRef(new StringImpl(rep->m_data8 + offset, length, ownerRep));
}
static ALWAYS_INLINE PassRefPtr<StringImpl> create(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
{
ASSERT(rep);
ASSERT(length <= rep->length());
if (!length)
return empty();
StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
if (rep->is8Bit())
return adoptRef(new StringImpl(rep->m_data8 + offset, length, ownerRep));
return adoptRef(new StringImpl(rep->m_data16 + offset, length, ownerRep));
}
static PassRefPtr<StringImpl> createUninitialized(unsigned length, LChar*& data);
static PassRefPtr<StringImpl> createUninitialized(unsigned length, UChar*& data);
template <typename T> static ALWAYS_INLINE PassRefPtr<StringImpl> tryCreateUninitialized(unsigned length, T*& output)
{
if (!length) {
output = 0;
return empty();
}
if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(T))) {
output = 0;
return 0;
}
StringImpl* resultImpl;
if (!tryFastMalloc(sizeof(T) * length + sizeof(StringImpl)).getValue(resultImpl)) {
output = 0;
return 0;
}
output = reinterpret_cast<T*>(resultImpl + 1);
if (sizeof(T) == sizeof(char))
return adoptRef(new(resultImpl) StringImpl(length, Force8BitConstructor));
return adoptRef(new(resultImpl) StringImpl(length));
}
static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data);
static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, UChar*& data);
static unsigned flagsOffset() { return OBJECT_OFFSETOF(StringImpl, m_hashAndFlags); }
static unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
static PassRefPtr<StringImpl> createWithTerminatingNullCharacter(const StringImpl&);
template<typename CharType, size_t inlineCapacity>
static PassRefPtr<StringImpl> adopt(Vector<CharType, inlineCapacity>& vector)
{
if (size_t size = vector.size()) {
ASSERT(vector.data());
if (size > std::numeric_limits<unsigned>::max())
CRASH();
return adoptRef(new StringImpl(vector.releaseBuffer(), size));
}
return empty();
}
static PassRefPtr<StringImpl> adopt(StringBuffer<LChar>& buffer);
static PassRefPtr<StringImpl> adopt(StringBuffer<UChar>& buffer);
unsigned length() const { return m_length; }
bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; }
ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_data8; }
ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; }
ALWAYS_INLINE const UChar* characters() const
{
if (!is8Bit())
return m_data16;
return getData16SlowCase();
}
template <typename CharType>
ALWAYS_INLINE const CharType * getCharacters() const;
size_t cost()
{
if (bufferOwnership() == BufferSubstring)
return m_substringBuffer->cost();
if (m_hashAndFlags & s_hashFlagDidReportCost)
return 0;
m_hashAndFlags |= s_hashFlagDidReportCost;
return m_length;
}
bool has16BitShadow() const { return m_hashAndFlags & s_hashFlagHas16BitShadow; }
void upconvertCharacters(unsigned, unsigned) const;
bool isIdentifier() const { return m_hashAndFlags & s_hashFlagIsIdentifier; }
void setIsIdentifier(bool isIdentifier)
{
ASSERT(!isStatic());
if (isIdentifier)
m_hashAndFlags |= s_hashFlagIsIdentifier;
else
m_hashAndFlags &= ~s_hashFlagIsIdentifier;
}
bool hasTerminatingNullCharacter() const { return m_hashAndFlags & s_hashFlagHasTerminatingNullCharacter; }
bool isAtomic() const { return m_hashAndFlags & s_hashFlagIsAtomic; }
void setIsAtomic(bool isIdentifier)
{
ASSERT(!isStatic());
if (isIdentifier)
m_hashAndFlags |= s_hashFlagIsAtomic;
else
m_hashAndFlags &= ~s_hashFlagIsAtomic;
}
private:
void setHash(unsigned hash) const
{
ASSERT(!hasHash());
ASSERT(hash == (is8Bit() ? StringHasher::computeHash(m_data8, m_length) : StringHasher::computeHash(m_data16, m_length)));
ASSERT(!(hash & (s_flagMask << (8 * sizeof(hash) - s_flagCount))));
hash <<= s_flagCount;
ASSERT(!(hash & m_hashAndFlags)); ASSERT(hash);
m_hashAndFlags |= hash; }
unsigned rawHash() const
{
return m_hashAndFlags >> s_flagCount;
}
public:
bool hasHash() const
{
return rawHash() != 0;
}
unsigned existingHash() const
{
ASSERT(hasHash());
return rawHash();
}
unsigned hash() const
{
if (!hasHash()) {
if (is8Bit())
setHash(StringHasher::computeHash(m_data8, m_length));
else
setHash(StringHasher::computeHash(m_data16, m_length));
}
return existingHash();
}
inline bool hasOneRef() const
{
return m_refCount == s_refCountIncrement;
}
inline void ref()
{
m_refCount += s_refCountIncrement;
}
inline void deref()
{
if (m_refCount == s_refCountIncrement) {
delete this;
return;
}
m_refCount -= s_refCountIncrement;
}
static StringImpl* empty();
template <typename T> static void copyChars(T* destination, const T* source, unsigned numCharacters)
{
if (numCharacters == 1) {
*destination = *source;
return;
}
if (numCharacters <= s_copyCharsInlineCutOff) {
unsigned i = 0;
#if (CPU(X86) || CPU(X86_64))
const unsigned charsPerInt = sizeof(uint32_t) / sizeof(T);
if (numCharacters > charsPerInt) {
unsigned stopCount = numCharacters & ~(charsPerInt - 1);
const uint32_t* srcCharacters = reinterpret_cast<const uint32_t*>(source);
uint32_t* destCharacters = reinterpret_cast<uint32_t*>(destination);
for (unsigned j = 0; i < stopCount; i += charsPerInt, ++j)
destCharacters[j] = srcCharacters[j];
}
#endif
for (; i < numCharacters; ++i)
destination[i] = source[i];
} else
memcpy(destination, source, numCharacters * sizeof(T));
}
PassRefPtr<StringImpl> isolatedCopy() const;
PassRefPtr<StringImpl> substring(unsigned pos, unsigned len = UINT_MAX);
UChar operator[](unsigned i) const
{
ASSERT(i < m_length);
if (is8Bit())
return m_data8[i];
return m_data16[i];
}
UChar32 characterStartingAt(unsigned);
bool containsOnlyWhitespace();
int toIntStrict(bool* ok = 0, int base = 10);
unsigned toUIntStrict(bool* ok = 0, int base = 10);
int64_t toInt64Strict(bool* ok = 0, int base = 10);
uint64_t toUInt64Strict(bool* ok = 0, int base = 10);
intptr_t toIntPtrStrict(bool* ok = 0, int base = 10);
int toInt(bool* ok = 0); unsigned toUInt(bool* ok = 0); int64_t toInt64(bool* ok = 0); uint64_t toUInt64(bool* ok = 0); intptr_t toIntPtr(bool* ok = 0);
double toDouble(bool* ok = 0, bool* didReadNumber = 0);
float toFloat(bool* ok = 0, bool* didReadNumber = 0);
PassRefPtr<StringImpl> lower();
PassRefPtr<StringImpl> upper();
enum LastCharacterBehavior { ObscureLastCharacter, DisplayLastCharacter };
PassRefPtr<StringImpl> secure(UChar, LastCharacterBehavior = ObscureLastCharacter);
PassRefPtr<StringImpl> foldCase();
PassRefPtr<StringImpl> stripWhiteSpace();
PassRefPtr<StringImpl> stripWhiteSpace(IsWhiteSpaceFunctionPtr);
PassRefPtr<StringImpl> simplifyWhiteSpace();
PassRefPtr<StringImpl> simplifyWhiteSpace(IsWhiteSpaceFunctionPtr);
PassRefPtr<StringImpl> removeCharacters(CharacterMatchFunctionPtr);
template <typename CharType>
ALWAYS_INLINE PassRefPtr<StringImpl> removeCharacters(const CharType* characters, CharacterMatchFunctionPtr);
size_t find(UChar, unsigned index = 0);
size_t find(CharacterMatchFunctionPtr, unsigned index = 0);
size_t find(const LChar*, unsigned index = 0);
ALWAYS_INLINE size_t find(const char* s, unsigned index = 0) { return find(reinterpret_cast<const LChar*>(s), index); };
size_t find(StringImpl*, unsigned index = 0);
size_t findIgnoringCase(const LChar*, unsigned index = 0);
ALWAYS_INLINE size_t findIgnoringCase(const char* s, unsigned index = 0) { return findIgnoringCase(reinterpret_cast<const LChar*>(s), index); };
size_t findIgnoringCase(StringImpl*, unsigned index = 0);
size_t reverseFind(UChar, unsigned index = UINT_MAX);
size_t reverseFind(StringImpl*, unsigned index = UINT_MAX);
size_t reverseFindIgnoringCase(StringImpl*, unsigned index = UINT_MAX);
bool startsWith(StringImpl* str, bool caseSensitive = true) { return (caseSensitive ? reverseFind(str, 0) : reverseFindIgnoringCase(str, 0)) == 0; }
bool endsWith(StringImpl*, bool caseSensitive = true);
PassRefPtr<StringImpl> replace(UChar, UChar);
PassRefPtr<StringImpl> replace(UChar, StringImpl*);
PassRefPtr<StringImpl> replace(StringImpl*, StringImpl*);
PassRefPtr<StringImpl> replace(unsigned index, unsigned len, StringImpl*);
WTF::Unicode::Direction defaultWritingDirection(bool* hasStrongDirectionality = 0);
#if USE(CF)
CFStringRef createCFString();
#endif
#ifdef __OBJC__
operator NSString*();
#endif
private:
static const unsigned s_copyCharsInlineCutOff = 20;
BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_hashAndFlags & s_hashMaskBufferOwnership); }
bool isStatic() const { return m_refCount & s_refCountFlagIsStaticString; }
template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacters(UCharPredicate);
template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
NEVER_INLINE const UChar* getData16SlowCase() const;
static const unsigned s_refCountFlagIsStaticString = 0x1;
static const unsigned s_refCountIncrement = 0x2;
static const unsigned s_flagCount = 8;
static const unsigned s_flagMask = (1u << s_flagCount) - 1;
COMPILE_ASSERT(s_flagCount == StringHasher::flagCount, StringHasher_reserves_enough_bits_for_StringImpl_flags);
static const unsigned s_hashFlagHas16BitShadow = 1u << 7;
static const unsigned s_hashFlag8BitBuffer = 1u << 6;
static const unsigned s_hashFlagHasTerminatingNullCharacter = 1u << 5;
static const unsigned s_hashFlagIsAtomic = 1u << 4;
static const unsigned s_hashFlagDidReportCost = 1u << 3;
static const unsigned s_hashFlagIsIdentifier = 1u << 2;
static const unsigned s_hashMaskBufferOwnership = 1u | (1u << 1);
unsigned m_refCount;
unsigned m_length;
union {
const LChar* m_data8;
const UChar* m_data16;
};
union {
void* m_buffer;
StringImpl* m_substringBuffer;
mutable UChar* m_copyData16;
};
mutable unsigned m_hashAndFlags;
};
template <>
ALWAYS_INLINE const LChar* StringImpl::getCharacters<LChar>() const { return characters8(); }
template <>
ALWAYS_INLINE const UChar* StringImpl::getCharacters<UChar>() const { return characters16(); }
bool equal(const StringImpl*, const StringImpl*);
bool equal(const StringImpl*, const LChar*);
inline bool equal(const StringImpl* a, const char* b) { return equal(a, reinterpret_cast<const LChar*>(b)); }
bool equal(const StringImpl*, const LChar*, unsigned);
inline bool equal(const StringImpl* a, const char* b, unsigned length) { return equal(a, reinterpret_cast<const LChar*>(b), length); }
inline bool equal(const LChar* a, StringImpl* b) { return equal(b, a); }
inline bool equal(const char* a, StringImpl* b) { return equal(b, reinterpret_cast<const LChar*>(a)); }
bool equal(const StringImpl*, const UChar*, unsigned);
#if CPU(X86_64)
ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
{
unsigned dwordLength = length >> 3;
if (dwordLength) {
const uint64_t* aDWordCharacters = reinterpret_cast<const uint64_t*>(a);
const uint64_t* bDWordCharacters = reinterpret_cast<const uint64_t*>(b);
for (unsigned i = 0; i != dwordLength; ++i) {
if (*aDWordCharacters++ != *bDWordCharacters++)
return false;
}
a = reinterpret_cast<const LChar*>(aDWordCharacters);
b = reinterpret_cast<const LChar*>(bDWordCharacters);
}
if (length & 4) {
if (*reinterpret_cast<const uint32_t*>(a) != *reinterpret_cast<const uint32_t*>(b))
return false;
a += 4;
b += 4;
}
if (length & 2) {
if (*reinterpret_cast<const uint16_t*>(a) != *reinterpret_cast<const uint16_t*>(b))
return false;
a += 2;
b += 2;
}
if (length & 1 && (*a != *b))
return false;
return true;
}
ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
{
unsigned dwordLength = length >> 2;
if (dwordLength) {
const uint64_t* aDWordCharacters = reinterpret_cast<const uint64_t*>(a);
const uint64_t* bDWordCharacters = reinterpret_cast<const uint64_t*>(b);
for (unsigned i = 0; i != dwordLength; ++i) {
if (*aDWordCharacters++ != *bDWordCharacters++)
return false;
}
a = reinterpret_cast<const UChar*>(aDWordCharacters);
b = reinterpret_cast<const UChar*>(bDWordCharacters);
}
if (length & 2) {
if (*reinterpret_cast<const uint32_t*>(a) != *reinterpret_cast<const uint32_t*>(b))
return false;
a += 2;
b += 2;
}
if (length & 1 && (*a != *b))
return false;
return true;
}
#elif CPU(X86)
ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
{
const uint32_t* aCharacters = reinterpret_cast<const uint32_t*>(a);
const uint32_t* bCharacters = reinterpret_cast<const uint32_t*>(b);
unsigned wordLength = length >> 2;
for (unsigned i = 0; i != wordLength; ++i) {
if (*aCharacters++ != *bCharacters++)
return false;
}
length &= 3;
if (length) {
const LChar* aRemainder = reinterpret_cast<const LChar*>(aCharacters);
const LChar* bRemainder = reinterpret_cast<const LChar*>(bCharacters);
for (unsigned i = 0; i < length; ++i) {
if (aRemainder[i] != bRemainder[i])
return false;
}
}
return true;
}
ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
{
const uint32_t* aCharacters = reinterpret_cast<const uint32_t*>(a);
const uint32_t* bCharacters = reinterpret_cast<const uint32_t*>(b);
unsigned wordLength = length >> 1;
for (unsigned i = 0; i != wordLength; ++i) {
if (*aCharacters++ != *bCharacters++)
return false;
}
if (length & 1 && *reinterpret_cast<const UChar*>(aCharacters) != *reinterpret_cast<const UChar*>(bCharacters))
return false;
return true;
}
#else
ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
{
for (unsigned i = 0; i != length; ++i) {
if (a[i] != b[i])
return false;
}
return true;
}
ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
{
for (unsigned i = 0; i != length; ++i) {
if (a[i] != b[i])
return false;
}
return true;
}
#endif
ALWAYS_INLINE bool equal(const LChar* a, const UChar* b, unsigned length)
{
for (unsigned i = 0; i != length; ++i) {
if (a[i] != b[i])
return false;
}
return true;
}
ALWAYS_INLINE bool equal(const UChar* a, const LChar* b, unsigned length)
{
for (unsigned i = 0; i != length; ++i) {
if (a[i] != b[i])
return false;
}
return true;
}
bool equalIgnoringCase(StringImpl*, StringImpl*);
bool equalIgnoringCase(StringImpl*, const LChar*);
inline bool equalIgnoringCase(const LChar* a, StringImpl* b) { return equalIgnoringCase(b, a); }
bool equalIgnoringCase(const UChar*, const LChar*, unsigned);
inline bool equalIgnoringCase(const UChar* a, const char* b, unsigned length) { return equalIgnoringCase(a, reinterpret_cast<const LChar*>(b), length); }
inline bool equalIgnoringCase(const LChar* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, a, length); }
inline bool equalIgnoringCase(const char* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, reinterpret_cast<const LChar*>(a), length); }
bool equalIgnoringNullity(StringImpl*, StringImpl*);
template<size_t inlineCapacity>
bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, StringImpl* b)
{
if (!b)
return !a.size();
if (a.size() != b->length())
return false;
return !memcmp(a.data(), b->characters(), b->length());
}
int codePointCompare(const StringImpl*, const StringImpl*);
static inline bool isSpaceOrNewline(UChar c)
{
return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF::Unicode::WhiteSpaceNeutral;
}
inline PassRefPtr<StringImpl> StringImpl::isolatedCopy() const
{
if (is8Bit())
return create(m_data8, m_length);
return create(m_data16, m_length);
}
struct StringHash;
template<typename T> struct DefaultHash;
template<> struct DefaultHash<StringImpl*> {
typedef StringHash Hash;
};
template<> struct DefaultHash<RefPtr<StringImpl> > {
typedef StringHash Hash;
};
}
using WTF::StringImpl;
using WTF::equal;
using WTF::TextCaseSensitivity;
using WTF::TextCaseSensitive;
using WTF::TextCaseInsensitive;
#endif