#ifndef StringImpl_h
#define StringImpl_h
#include <limits.h>
#include <wtf/ASCIICType.h>
#include <wtf/Forward.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>
#include <wtf/unicode/Unicode.h>
#if PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN))
typedef const struct __CFString * CFStringRef;
#endif
#ifdef __OBJC__
@class NSString;
#endif
namespace WebCore {
class AtomicString;
class StringBuffer;
struct CStringTranslator;
struct HashAndCharactersTranslator;
struct StringHash;
struct UCharBufferTranslator;
enum TextCaseSensitivity { TextCaseSensitive, TextCaseInsensitive };
typedef bool (*CharacterMatchFunctionPtr)(UChar);
class StringImpl : public RefCounted<StringImpl> {
friend class AtomicString;
friend struct CStringTranslator;
friend struct HashAndCharactersTranslator;
friend struct UCharBufferTranslator;
private:
friend class ThreadGlobalData;
StringImpl();
StringImpl(const UChar*, unsigned length);
StringImpl(const char*, unsigned length);
struct AdoptBuffer { };
StringImpl(UChar*, unsigned length, AdoptBuffer);
struct WithTerminatingNullCharacter { };
StringImpl(const StringImpl&, WithTerminatingNullCharacter);
StringImpl(const UChar*, unsigned length, unsigned hash);
StringImpl(const char*, unsigned length, unsigned hash);
public:
~StringImpl();
static PassRefPtr<StringImpl> create(const UChar*, unsigned length);
static PassRefPtr<StringImpl> create(const char*, unsigned length);
static PassRefPtr<StringImpl> create(const char*);
static PassRefPtr<StringImpl> createWithTerminatingNullCharacter(const StringImpl&);
static PassRefPtr<StringImpl> createStrippingNullCharacters(const UChar*, unsigned length);
static PassRefPtr<StringImpl> adopt(StringBuffer&);
static PassRefPtr<StringImpl> adopt(Vector<UChar>&);
const UChar* characters() { return m_data; }
unsigned length() { return m_length; }
bool hasTerminatingNullCharacter() { return m_hasTerminatingNullCharacter; }
unsigned hash() { if (m_hash == 0) m_hash = computeHash(m_data, m_length); return m_hash; }
unsigned existingHash() const { ASSERT(m_hash); return m_hash; }
static unsigned computeHash(const UChar*, unsigned len);
static unsigned computeHash(const char*);
PassRefPtr<StringImpl> copy();
PassRefPtr<StringImpl> substringCopy(unsigned pos, unsigned len = UINT_MAX);
PassRefPtr<StringImpl> substring(unsigned pos, unsigned len = UINT_MAX);
UChar operator[](unsigned i) { ASSERT(i < m_length); return m_data[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);
int toInt(bool* ok = 0); unsigned toUInt(bool* ok = 0); int64_t toInt64(bool* ok = 0); uint64_t toUInt64(bool* ok = 0);
double toDouble(bool* ok = 0);
float toFloat(bool* ok = 0);
bool isLower();
PassRefPtr<StringImpl> lower();
PassRefPtr<StringImpl> upper();
PassRefPtr<StringImpl> secure(UChar aChar, bool last = true);
PassRefPtr<StringImpl> capitalize(UChar previousCharacter);
PassRefPtr<StringImpl> foldCase();
PassRefPtr<StringImpl> stripWhiteSpace();
PassRefPtr<StringImpl> simplifyWhiteSpace();
PassRefPtr<StringImpl> removeCharacters(CharacterMatchFunctionPtr);
int find(const char*, int index = 0, bool caseSensitive = true);
int find(UChar, int index = 0);
int find(CharacterMatchFunctionPtr, int index = 0);
int find(StringImpl*, int index, bool caseSensitive = true);
int reverseFind(UChar, int index);
int reverseFind(StringImpl*, int index, bool caseSensitive = true);
bool startsWith(StringImpl* m_data, bool caseSensitive = true) { return reverseFind(m_data, 0, caseSensitive) == 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*);
static StringImpl* empty();
Vector<char> ascii();
int wordCount(int maxWordsToCount = INT_MAX);
WTF::Unicode::Direction defaultWritingDirection();
#if PLATFORM(CF) || (PLATFORM(QT) && PLATFORM(DARWIN))
CFStringRef createCFString();
#endif
#ifdef __OBJC__
operator NSString*();
#endif
private:
unsigned m_length;
const UChar* m_data;
mutable unsigned m_hash;
bool m_inTable;
bool m_hasTerminatingNullCharacter;
};
bool equal(StringImpl*, StringImpl*);
bool equal(StringImpl*, const char*);
inline bool equal(const char* a, StringImpl* b) { return equal(b, a); }
bool equalIgnoringCase(StringImpl*, StringImpl*);
bool equalIgnoringCase(StringImpl*, const char*);
inline bool equalIgnoringCase(const char* a, StringImpl* b) { return equalIgnoringCase(b, a); }
const unsigned phi = 0x9e3779b9U;
inline unsigned StringImpl::computeHash(const UChar* data, unsigned length)
{
unsigned hash = phi;
for (unsigned pairCount = length >> 1; pairCount; pairCount--) {
hash += data[0];
unsigned tmp = (data[1] << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2;
hash += hash >> 11;
}
if (length & 1) {
hash += data[0];
hash ^= hash << 11;
hash += hash >> 17;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 2;
hash += hash >> 15;
hash ^= hash << 10;
hash |= !hash << 31;
return hash;
}
inline unsigned StringImpl::computeHash(const char* data)
{
unsigned hash = phi;
for (;;) {
unsigned char b0 = data[0];
if (!b0)
break;
unsigned char b1 = data[1];
if (!b1) {
hash += b0;
hash ^= hash << 11;
hash += hash >> 17;
break;
}
hash += b0;
unsigned tmp = (b1 << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2;
hash += hash >> 11;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 2;
hash += hash >> 15;
hash ^= hash << 10;
hash |= !hash << 31;
return hash;
}
static inline bool isSpaceOrNewline(UChar c)
{
return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF::Unicode::WhiteSpaceNeutral;
}
}
namespace WTF {
template<typename T> struct DefaultHash;
template<> struct DefaultHash<WebCore::StringImpl*> {
typedef WebCore::StringHash Hash;
};
template<> struct DefaultHash<RefPtr<WebCore::StringImpl> > {
typedef WebCore::StringHash Hash;
};
}
#endif