/*
 * (C) 1999 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple 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.
 *
 */

#ifndef WTFString_h
#define WTFString_h

// This file would be called String.h, but that conflicts with <string.h>
// on systems without case-sensitive file systems.

#include <wtf/text/ASCIIFastPath.h>
#include <wtf/text/StringImpl.h>

#ifdef __OBJC__
#include <objc/objc.h>
#endif

#if USE(CF)
typedef const struct __CFString * CFStringRef;
#endif

#if PLATFORM(QT)
QT_BEGIN_NAMESPACE
class QString;
QT_END_NAMESPACE
#include <QDataStream>
#endif

#if PLATFORM(WX)
class wxString;
#endif

#if PLATFORM(BLACKBERRY)
namespace BlackBerry {
namespace WebKit {
    class WebString;
}
}
#endif

namespace WTF {

class CString;
struct StringHash;

// Declarations of string operations

int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
WTF_EXPORT_PRIVATE int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
WTF_EXPORT_PRIVATE unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = 0, int base = 10);

int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
WTF_EXPORT_PRIVATE int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage

// FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
// Like the non-strict functions above, these return the value when there is trailing garbage.
// It would be better if these were more consistent with the above functions instead.
WTF_EXPORT_PRIVATE double charactersToDouble(const LChar*, size_t, bool* ok = 0);
WTF_EXPORT_PRIVATE double charactersToDouble(const UChar*, size_t, bool* ok = 0);
float charactersToFloat(const LChar*, size_t, bool* ok = 0);
WTF_EXPORT_PRIVATE float charactersToFloat(const UChar*, size_t, bool* ok = 0);
WTF_EXPORT_PRIVATE float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
WTF_EXPORT_PRIVATE float charactersToFloat(const UChar*, size_t, size_t& parsedLength);

enum FloatConversionFlags {
    ShouldRoundSignificantFigures = 1 << 0,
    ShouldRoundDecimalPlaces = 1 << 1,
    ShouldTruncateTrailingZeros = 1 << 2
};

class ASCIILiteral {
public:
    template<unsigned charactersCount>
    explicit ALWAYS_INLINE ASCIILiteral(const char (&characters)[charactersCount])
    : m_characters(characters)
    , m_len(charactersCount - 1)
    { }

    operator const char*() { return m_characters; }

    const char* characters() const { return m_characters; }
    unsigned length() const { return m_len; }

private:
    const char* m_characters;
    const unsigned m_len;
};

enum TrailingZerosTruncatingPolicy {
    KeepTrailingZeros,
    TruncateTrailingZeros
};

template<bool isSpecialCharacter(UChar), typename CharacterType>
bool isAllSpecialCharacters(const CharacterType*, size_t);

class String {
	WTF_MAKE_FAST_ALLOCATED;
public:
    // Construct a null string, distinguishable from an empty string.
	ALWAYS_INLINE String() { }

    // Construct a string with UTF-16 data.
    WTF_EXPORT_PRIVATE String(const UChar* characters, unsigned length);

    // Construct a string by copying the contents of a vector.  To avoid
    // copying, consider using String::adopt instead.
    // This method will never create a null string. Vectors with size() == 0
    // will return the empty string.
    // NOTE: This is different from String(vector.data(), vector.size())
    // which will sometimes return a null string when vector.data() is null
    // which can only occur for vectors without inline capacity.
    // See: https://bugs.webkit.org/show_bug.cgi?id=109792
    template<size_t inlineCapacity>
    explicit String(const Vector<UChar, inlineCapacity>&);

    // Construct a string with UTF-16 data, from a null-terminated source.
    WTF_EXPORT_PRIVATE String(const UChar*);

    // Construct a string with latin1 data.
    WTF_EXPORT_PRIVATE String(const LChar* characters, unsigned length);
    WTF_EXPORT_PRIVATE String(const char* characters, unsigned length);

    // Construct a string with latin1 data, from a null-terminated source.
    WTF_EXPORT_PRIVATE String(const LChar* characters);
    WTF_EXPORT_PRIVATE String(const char* characters);

    // Construct a string referencing an existing StringImpl.
    ALWAYS_INLINE String(StringImpl& impl) : m_impl(&impl) { }
    ALWAYS_INLINE String(StringImpl* impl) : m_impl(impl) { }
    ALWAYS_INLINE String(PassRefPtr<StringImpl> impl) : m_impl(impl) { }

    String(StaticStringImpl&);
    String(StaticStringImpl*);

    // Construct a string from a constant string literal.
    WTF_EXPORT_PRIVATE String(const ASCIILiteral&);

    // Construct a string from a constant string literal.
    // This constructor is the "big" version, as it put the length in the function call and generate bigger code.
    enum ConstructFromLiteralTag { ConstructFromLiteral };
    template<unsigned charactersCount>
    ALWAYS_INLINE String(const char (&characters)[charactersCount], ConstructFromLiteralTag)
    : m_impl(StringImpl::createFromLiteral<charactersCount>(characters))
    {
    }

    ALWAYS_INLINE String(const String& other) : m_impl(other.m_impl) { }
    ALWAYS_INLINE String& operator=(const String& other) { m_impl = other.m_impl; return *this; }

    // We have to declare the copy constructor and copy assignment operator as well, otherwise
    // they'll be implicitly deleted by adding the move constructor and move assignment operator.
    String(String&& other) : m_impl(other.m_impl.release()) { }
    String& operator=(String&& other) { m_impl = other.m_impl.release(); return *this; }

    // Inline the destructor.
    ALWAYS_INLINE ~String() { }

    void swap(String& o) { m_impl.swap(o.m_impl); }

    PassRefPtr<StringImpl> release() { return m_impl.release(); }

    template<unsigned charactersCount>
    static ALWAYS_INLINE String fromLiteral(const char (&characters)[charactersCount])
    {
    	return String(characters, ConstructFromLiteral);
    }

    static ALWAYS_INLINE String fromLiteral(const char* characters)
	{
		return StringImpl::createFromLiteral(characters);
	}

    static ALWAYS_INLINE String fromLiteral(const char* characters, unsigned length)
	{
		return StringImpl::createFromLiteral(characters, length);
	}

    static ALWAYS_INLINE String adopt(StringBuffer<LChar>& buffer) { return StringImpl::adopt(buffer); }
    static ALWAYS_INLINE String adopt(StringBuffer<UChar>& buffer) { return StringImpl::adopt(buffer); }
    template<typename CharacterType, size_t inlineCapacity>
    static String adopt(Vector<CharacterType, inlineCapacity>& vector) { return StringImpl::adopt(vector); }

    ALWAYS_INLINE bool isNull() const { return !m_impl; }
    bool isEmpty() const { return !m_impl || !m_impl->length(); }

    ALWAYS_INLINE StringImpl* impl() const { return m_impl.get(); }
    PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }

    unsigned length() const
    {
        if (m_impl)
        	return m_impl->length();
        return 0;
    }
    
    const LChar* characters8() const
    {
        if (!m_impl)
            return 0;
        ASSERT(m_impl->is8Bit());
        return m_impl->characters8();
    }

    const UChar* characters16() const
    {
        if (!m_impl)
            return 0;
        ASSERT(!m_impl->is8Bit());
        return m_impl->characters16();
    }

    // Return characters8() or characters16() depending on CharacterType.
    template <typename CharacterType>
    inline const CharacterType* getCharacters() const;

    ALWAYS_INLINE bool is8Bit() const { return m_impl->is8Bit(); }

    unsigned sizeInBytes() const
    {
        if (!m_impl)
            return 0;
        return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
    }

    WTF_EXPORT_PRIVATE CString ascii() const;
    WTF_EXPORT_PRIVATE CString latin1() const;

    CString utf8(ConversionMode = LenientConversion) const;

    // Workaround for a compiler bug. Use operator[] instead.
    ALWAYS_INLINE UChar uncheckedCharacterAt(unsigned index) const
    {
        return (*m_impl)[index];
    }

    UChar at(unsigned index) const
    {
        if (!m_impl || index >= m_impl->length())
            return 0;
        return uncheckedCharacterAt(index);
    }
    ALWAYS_INLINE UChar operator[](unsigned index) const { return at(index); }

    ALWAYS_INLINE UChar characterAt(unsigned index) const
	{
		return at(index);
	}

    WTF_EXPORT_PRIVATE static String number(int);
    WTF_EXPORT_PRIVATE static String number(unsigned int);
    WTF_EXPORT_PRIVATE static String number(long);
    WTF_EXPORT_PRIVATE static String number(unsigned long);
    WTF_EXPORT_PRIVATE static String number(long long);
    WTF_EXPORT_PRIVATE static String number(unsigned long long);

    WTF_EXPORT_PRIVATE static String number(double, unsigned = ShouldRoundSignificantFigures | ShouldTruncateTrailingZeros, unsigned precision = 6);

    // Number to String conversion following the ECMAScript definition.
    WTF_EXPORT_PRIVATE static String numberToStringECMAScript(double);

    // Find a single character or string, also with match function & latin1 forms.
    size_t find(UChar c, unsigned start = 0) const
        { return m_impl ? m_impl->find(c, start) : notFound; }

    size_t find(const String& str) const
        { return m_impl ? m_impl->find(str.impl()) : notFound; }
    size_t find(const String& str, unsigned start) const
        { return m_impl ? m_impl->find(str.impl(), start) : notFound; }

    size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
        { return m_impl ? m_impl->find(matchFunction, start) : notFound; }
    size_t find(const LChar* str, unsigned start = 0) const
        { return m_impl ? m_impl->find(str, start) : notFound; }

    size_t findNextLineStart(unsigned start = 0) const
        { return m_impl ? m_impl->findNextLineStart(start) : notFound; }

    // Find the last instance of a single character or string.
    size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
        { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
    size_t reverseFind(const String& str, unsigned start = UINT_MAX) const
        { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }

    // Case insensitive string matching.
    size_t findIgnoringCase(const LChar* str, unsigned start = 0) const
        { return m_impl ? m_impl->findIgnoringCase(str, start) : notFound; }
    size_t findIgnoringCase(const String& str, unsigned start = 0) const
        { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : notFound; }
    size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const
        { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : notFound; }

    // Wrappers for find & reverseFind adding dynamic sensitivity check.
    size_t find(const LChar* str, unsigned start, bool caseSensitive) const
        { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
    size_t find(const String& str, unsigned start, bool caseSensitive) const
        { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
    size_t reverseFind(const String& str, unsigned start, bool caseSensitive) const
        { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }

    WTF_EXPORT_STRING_API Vector<UChar> charactersWithNullTermination() const;
    
    WTF_EXPORT_PRIVATE UChar32 characterStartingAt(unsigned) const; // Ditto.
    
    ALWAYS_INLINE bool contains(UChar c) const { return find(c) != notFound; }
    bool contains(const LChar* str, bool caseSensitive = true, unsigned startOffset = 0) const
        { return find(str, startOffset, caseSensitive) != notFound; }
    bool contains(const String& str, bool caseSensitive = true, unsigned startOffset = 0) const
        { return find(str, startOffset, caseSensitive) != notFound; }

    bool startsWith(const String& s) const
        { return m_impl ? m_impl->startsWith(s.impl()) : s.isEmpty(); }
    bool startsWithIgnoringASCIICase(const String& s) const
		{ return m_impl ? m_impl->startsWithIgnoringASCIICase(s.impl()) : s.isEmpty(); }
    bool startsWith(const String& s, bool caseSensitive) const
        { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
    bool startsWith(UChar character) const
        { return m_impl ? m_impl->startsWith(character) : false; }
    template<unsigned matchLength>
    bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
        { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
    bool hasInfixStartingAt(const String& prefix, unsigned startOffset) const
		{ return m_impl && prefix.impl() ? m_impl->hasInfixStartingAt(*prefix.impl(), startOffset) : false; }

    bool endsWith(const String& s) const
        { return m_impl ? m_impl->endsWith(s.impl()) : s.isEmpty(); }
    bool endsWithIgnoringASCIICase(const String& s) const
        { return m_impl ? m_impl->endsWithIgnoringASCIICase(s.impl()) : s.isEmpty(); }
    bool endsWith(const String& s, bool caseSensitive) const
        { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
    bool endsWith(UChar character) const
        { return m_impl ? m_impl->endsWith(character) : false; }
    bool endsWith(char character) const { return endsWith(static_cast<UChar>(character)); }
    template<unsigned matchLength>
    bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
        { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
    bool hasInfixEndingAt(const String& suffix, unsigned endOffset) const
		{ return m_impl && suffix.impl() ? m_impl->hasInfixEndingAt(*suffix.impl(), endOffset) : false; }

    WTF_EXPORT_PRIVATE void append(const String&);
    WTF_EXPORT_PRIVATE void append(LChar);
    ALWAYS_INLINE void append(char c) { append(static_cast<LChar>(c)); };
    WTF_EXPORT_PRIVATE void append(UChar);
    WTF_EXPORT_PRIVATE void append(const LChar*, unsigned length);
    WTF_EXPORT_PRIVATE void append(const UChar*, unsigned length);
    WTF_EXPORT_PRIVATE void insert(const String&, unsigned pos);

    String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
    String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
    String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
    String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }

    template<unsigned charactersCount>
    ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount])
    {
        if (m_impl)
            m_impl = m_impl->replace(a, characters, charactersCount - 1);

        return *this;
    }

    void makeLower() { if (m_impl) m_impl = m_impl->lower(); }
    void makeUpper() { if (m_impl) m_impl = m_impl->upper(); }
    void fill(UChar c) { if (m_impl) m_impl = m_impl->fill(c); }

    WTF_EXPORT_PRIVATE void truncate(unsigned len);
    WTF_EXPORT_PRIVATE void remove(unsigned pos, int len = 1);

    WTF_EXPORT_PRIVATE String substring(unsigned pos, unsigned len = UINT_MAX) const;
    WTF_EXPORT_PRIVATE String substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
    ALWAYS_INLINE String left(unsigned len) const { return substring(0, len); }
    ALWAYS_INLINE String right(unsigned len) const { return substring(length() - len, len); }

    // Returns a lowercase/uppercase version of the string.
    // The convertToASCIILowercase is useful in many contexts such as HTML where we don't
    // want to do any conversion for non-ASCII letters.
    WTF_EXPORT_STRING_API String convertToASCIILowercase() const;
    WTF_EXPORT_STRING_API String convertToASCIIUppercase() const;
    WTF_EXPORT_STRING_API String convertToLowercaseWithoutLocale() const;
	WTF_EXPORT_STRING_API String convertToLowercaseWithoutLocaleStartingAtFailingIndex8Bit(unsigned) const;
	WTF_EXPORT_STRING_API String convertToUppercaseWithoutLocale() const;
	WTF_EXPORT_STRING_API String convertToLowercaseWithLocale(const AtomicString& localeIdentifier) const;
	WTF_EXPORT_STRING_API String convertToUppercaseWithLocale(const AtomicString& localeIdentifier) const;

    WTF_EXPORT_PRIVATE String lower() const;
    WTF_EXPORT_PRIVATE String upper() const;

    WTF_EXPORT_PRIVATE String stripWhiteSpace() const;
    WTF_EXPORT_PRIVATE String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
    WTF_EXPORT_PRIVATE String simplifyWhiteSpace() const;
    WTF_EXPORT_PRIVATE String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr) const;

    WTF_EXPORT_PRIVATE String removeCharacters(CharacterMatchFunctionPtr) const;
    template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;

    // Returns the string with case folded for case insensitive comparison.
    // Use convertToASCIILowercase instead if ASCII case insensitive comparison is desired.
    WTF_EXPORT_PRIVATE String foldCase() const;

#if !PLATFORM(QT)
    WTF_EXPORT_PRIVATE static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
#else
    WTF_EXPORT_PRIVATE static String format(const char *, ...);
#endif

    // Returns an uninitialized string. The characters needs to be written
    // into the buffer returned in data before the returned string is used.
    // Failure to do this will have unpredictable results.
    static ALWAYS_INLINE String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
    static ALWAYS_INLINE String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }

    static ALWAYS_INLINE String tryCreateUninitialized(unsigned length, UChar*& data) { return StringImpl::tryCreateUninitialized(length, data); }
    static ALWAYS_INLINE String tryCreateUninitialized(unsigned length, LChar*& data) { return StringImpl::tryCreateUninitialized(length, data); }

    WTF_EXPORT_PRIVATE void split(const String& separator, Vector<String>& result) const;
    WTF_EXPORT_PRIVATE void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
    WTF_EXPORT_PRIVATE void split(UChar separator, Vector<String>& result) const;
    WTF_EXPORT_PRIVATE void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;

    WTF_EXPORT_PRIVATE int toIntStrict(bool* ok = 0, int base = 10) const;
    WTF_EXPORT_PRIVATE unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
    WTF_EXPORT_PRIVATE int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
    uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
    intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const;

    WTF_EXPORT_PRIVATE int toInt(bool* ok = 0) const;
    WTF_EXPORT_PRIVATE unsigned toUInt(bool* ok = 0) const;
    int64_t toInt64(bool* ok = 0) const;
    WTF_EXPORT_PRIVATE uint64_t toUInt64(bool* ok = 0) const;
    WTF_EXPORT_PRIVATE intptr_t toIntPtr(bool* ok = 0) const;

    // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
    // Like the non-strict functions above, these return the value when there is trailing garbage.
    // It would be better if these were more consistent with the above functions instead.
    WTF_EXPORT_PRIVATE double toDouble(bool* ok = 0) const;
    WTF_EXPORT_PRIVATE float toFloat(bool* ok = 0) const;

    bool percentage(int& percentage) const;

    WTF_EXPORT_PRIVATE String isolatedCopy() const;

    // Prevent Strings from being implicitly convertable to bool as it will be ambiguous on any platform that
    // allows implicit conversion to another pointer type (e.g., Mac allows implicit conversion to NSString*).
    typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*UnspecifiedBoolTypeA);
    typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*UnspecifiedBoolTypeB);
    operator UnspecifiedBoolTypeA() const;
    operator UnspecifiedBoolTypeB() const;

#if USE(CF)
    String(CFStringRef);
    CFStringRef createCFString() const;
#endif

#ifdef __OBJC__
    String(NSString*);
    
    // This conversion maps NULL to "", which loses the meaning of NULL, but we 
    // need this mapping because AppKit crashes when passed nil NSStrings.
    operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
#endif

#if PLATFORM(QT)
    String(const QString&);
    String(const QStringRef&);
    operator QString() const;
#endif

#if PLATFORM(WX)
    WTF_EXPORT_PRIVATE String(const wxString&);
    WTF_EXPORT_PRIVATE operator wxString() const;
#endif

#if PLATFORM(BLACKBERRY)
    String(const BlackBerry::WebKit::WebString&);
    operator BlackBerry::WebKit::WebString() const;
#endif

    WTF_EXPORT_PRIVATE static String make8BitFrom16BitSource(const UChar*, size_t);
    template<size_t inlineCapacity>
	static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
	{
		return make8BitFrom16BitSource(buffer.data(), buffer.size());
	}

    WTF_EXPORT_PRIVATE static String make16BitFrom8BitSource(const LChar*, size_t);

    // String::fromUTF8 will return a null string if
    // the input data contains invalid UTF-8 sequences.
    WTF_EXPORT_PRIVATE static String fromUTF8(const LChar*, size_t);
    WTF_EXPORT_PRIVATE static String fromUTF8(const LChar*);
    static ALWAYS_INLINE String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); };
    static ALWAYS_INLINE String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); };

    // Tries to convert the passed in string to UTF-8, but will fall back to Latin-1 if the string is not valid UTF-8.
    WTF_EXPORT_PRIVATE static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
    static ALWAYS_INLINE String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); };
    
    // Determines the writing direction using the Unicode Bidi Algorithm rules P2 and P3.
    UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = 0) const
    {
        if (m_impl)
            return m_impl->defaultWritingDirection(hasStrongDirectionality);
        if (hasStrongDirectionality)
            *hasStrongDirectionality = false;
        return U_LEFT_TO_RIGHT;
    }

    bool containsOnlyASCII() const;
    bool containsOnlyLatin1() const;
    bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }

    // Hash table deleted values, which are only constructed and never copied or destroyed.
    ALWAYS_INLINE String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
    ALWAYS_INLINE bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }

#ifndef NDEBUG
    WTF_EXPORT_PRIVATE void show() const;
#endif

private:
    template <typename CharacterType>
    void removeInternal(const CharacterType*, unsigned, int);

    RefPtr<StringImpl> m_impl;
};

//static_assert(sizeof(String) == sizeof(void*), "String should effectively be a pointer to a StringImpl, and efficient to pass by value");

// Construct a string with UTF-16 data.
ALWAYS_INLINE String::String(const UChar* characters, unsigned length)
: m_impl(characters ? StringImpl::create(characters, length) : nullptr)
{
}

// Construct a string with latin1 data.
ALWAYS_INLINE String::String(const LChar* characters, unsigned length)
: m_impl(characters ? StringImpl::create(characters, length) : nullptr)
{
}

// Construct a string with latin1 data, from a null-terminated source.
ALWAYS_INLINE String::String(const LChar* characters)
: m_impl(characters ? StringImpl::create(characters) : nullptr)
{
}

#if PLATFORM(QT)
QDataStream& operator<<(QDataStream& stream, const String& str);
QDataStream& operator>>(QDataStream& stream, String& str);
#endif

ALWAYS_INLINE  String& operator+=(String& a, const String& b) { a.append(b); return a; }

ALWAYS_INLINE bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
ALWAYS_INLINE bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
ALWAYS_INLINE bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
ALWAYS_INLINE bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
ALWAYS_INLINE bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
template<size_t inlineCapacity>
ALWAYS_INLINE bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
template<size_t inlineCapacity>
ALWAYS_INLINE bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }


ALWAYS_INLINE bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
ALWAYS_INLINE bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
ALWAYS_INLINE bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
ALWAYS_INLINE bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
ALWAYS_INLINE bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
template<size_t inlineCapacity>
ALWAYS_INLINE bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
template<size_t inlineCapacity>
ALWAYS_INLINE bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }

ALWAYS_INLINE bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
ALWAYS_INLINE bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
ALWAYS_INLINE bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
ALWAYS_INLINE bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
ALWAYS_INLINE bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }

inline bool equalIgnoringASCIICase(const String& a, const String& b) { return equalIgnoringASCIICase(a.impl(), b.impl()); }
template<unsigned charactersCount>
inline bool equalIgnoringASCIICase(const String& a, const char (&b)[charactersCount]) { return equalIgnoringASCIICase<charactersCount>(a.impl(), b); }

ALWAYS_INLINE  bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ignoreCase)
{
    return ignoreCase ? equalIgnoringCase(a, b) : (a == b);
}

template<unsigned length> bool equalLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
template<unsigned length> bool startsWithLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);

ALWAYS_INLINE bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }

template<size_t inlineCapacity>
ALWAYS_INLINE bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }

ALWAYS_INLINE bool operator!(const String& str) { return str.isNull(); }

ALWAYS_INLINE void swap(String& a, String& b) { a.swap(b); }

// Definitions of string operations

inline String::String(StaticStringImpl& impl)
    : m_impl(reinterpret_cast<StringImpl*>(&impl))
{
}

inline String::String(StaticStringImpl* impl)
    : m_impl(reinterpret_cast<StringImpl*>(impl))
{
}

template<size_t inlineCapacity>
ALWAYS_INLINE String::String(const Vector<UChar, inlineCapacity>& vector)
: m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty())
{
}

ALWAYS_INLINE String::String(const char* characters, unsigned length)
: m_impl(characters ? StringImpl::create(reinterpret_cast<const LChar*>(characters), length) : nullptr)
{
}

ALWAYS_INLINE String::String(const char* characters)
: m_impl(characters ? StringImpl::create(reinterpret_cast<const LChar*>(characters)) : nullptr)
{
}

ALWAYS_INLINE String::String(const ASCIILiteral& literal)
: m_impl(StringImpl::createFromLiteral(literal.characters(), literal.length()))
{
}

inline void String::split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const
{
    result.clear();

    unsigned startPos = 0;
    size_t endPos;
    while ((endPos = find(separator, startPos)) != notFound) {
        if (allowEmptyEntries || startPos != endPos)
            result.append(substring(startPos, endPos - startPos));
        startPos = endPos + separator.length();
    }
    if (allowEmptyEntries || startPos != length())
        result.append(substring(startPos));
}

ALWAYS_INLINE void String::split(const String& separator, Vector<String>& result) const
{
    split(separator, false, result);
}

inline void String::split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const
{
    result.clear();

    unsigned startPos = 0;
    size_t endPos;
    while ((endPos = find(separator, startPos)) != notFound) {
        if (allowEmptyEntries || startPos != endPos)
            result.append(substring(startPos, endPos - startPos));
        startPos = endPos + 1;
    }
    if (allowEmptyEntries || startPos != length())
        result.append(substring(startPos));
}

ALWAYS_INLINE void String::split(UChar separator, Vector<String>& result) const
{
    split(separator, false, result);
}

template<>
ALWAYS_INLINE const LChar* String::getCharacters<LChar>() const
{
    ASSERT(is8Bit());
    return characters8();
}

template<>
ALWAYS_INLINE const UChar* String::getCharacters<UChar>() const
{
    ASSERT(!is8Bit());
    return characters16();
}

inline bool String::containsOnlyLatin1() const
{
    if (isEmpty())
        return true;

    if (is8Bit())
        return true;

    const UChar* characters = characters16();
    UChar ored = 0;
    for (size_t i = 0; i < m_impl->length(); ++i)
        ored |= characters[i];
    return !(ored & 0xFF00);
}


#ifdef __OBJC__
// This is for situations in WebKit where the long standing behavior has been
// "nil if empty", so we try to maintain longstanding behavior for the sake of
// entrenched clients
inline NSString* nsStringNilIfEmpty(const String& str) {  return str.isEmpty() ? nil : (NSString*)str; }
#endif

inline bool String::containsOnlyASCII() const
{
    if (isEmpty())
        return true;

    if (is8Bit())
        return charactersAreAllASCII(characters8(), m_impl->length());

    return charactersAreAllASCII(characters16(), m_impl->length());
}

WTF_EXPORT_PRIVATE int codePointCompare(const String&, const String&);

ALWAYS_INLINE bool codePointCompareLessThan(const String& a, const String& b)
{
    return codePointCompare(a.impl(), b.impl()) < 0;
}

template<typename CharacterType>
inline void appendNumber(Vector<CharacterType>& vector, unsigned char number)
{
    int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1);
    size_t vectorSize = vector.size();
    vector.grow(vectorSize + numberLength);

    switch (numberLength) {
    case 3:
        vector[vectorSize + 2] = number % 10 + '0';
        number /= 10;

    case 2:
        vector[vectorSize + 1] = number % 10 + '0';
        number /= 10;

    case 1:
        vector[vectorSize] = number % 10 + '0';
    }
}

template<bool isSpecialCharacter(UChar), typename CharacterType>
inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
{
    for (size_t i = 0; i < length; ++i) {
        if (!isSpecialCharacter(characters[i]))
            return false;
    }
    return true;
}

template<bool isSpecialCharacter(UChar)>
inline bool String::isAllSpecialCharacters() const
{
    size_t len = length();

    if (!len)
        return true;

    if (is8Bit())
        return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
    return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
}

// StringHash is the default hash for String
template<typename T> struct DefaultHash;
template<> struct DefaultHash<String> {
    typedef StringHash Hash;
};

template <> struct VectorTraits<String> : SimpleClassVectorTraits { };

// Shared global empty string.
WTF_EXPORT_PRIVATE const String& emptyString();

ALWAYS_INLINE String String::fromUTF8(const LChar* string)
{
    if (!string)
        return String();
    return fromUTF8(string, strlen(reinterpret_cast<const char*>(string)));
}

ALWAYS_INLINE String String::fromUTF8WithLatin1Fallback(const LChar* string, size_t size)
{
    String utf8 = fromUTF8(string, size);
    if (!utf8)
        return String(string, size);
    return utf8;
}

ALWAYS_INLINE bool operator==(const String& a, const ASCIILiteral& b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b.characters()), b.length()); }
ALWAYS_INLINE bool operator!=(const String& a, const ASCIILiteral& b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b.characters()), b.length()); }

ALWAYS_INLINE bool equalIgnoringCase(const String& a, const ASCIILiteral& b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b.characters()), b.length()); }

ALWAYS_INLINE bool equalIgnoringCase(const StringImpl* a, const ASCIILiteral& b) { return equalIgnoringCase(a, reinterpret_cast<const LChar*>(b.characters()), b.length()); }

template<unsigned length> inline bool equalLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
{
    return equalLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
}

template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
{
    return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
}

inline String String::stripWhiteSpace() const
{
    if (!m_impl)
        return String();
    return m_impl->stripWhiteSpace();
}

inline String String::stripWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace) const
{
    if (!m_impl)
        return String();
    return m_impl->stripWhiteSpace(isWhiteSpace);
}

void logMessage(const String&);

}

using WTF::CString;
using WTF::String;
using WTF::emptyString;
using WTF::appendNumber;
using WTF::charactersAreAllASCII;
using WTF::charactersToIntStrict;
using WTF::charactersToUIntStrict;
using WTF::charactersToInt64Strict;
using WTF::charactersToUInt64Strict;
using WTF::charactersToIntPtrStrict;
using WTF::charactersToInt;
using WTF::charactersToUInt;
using WTF::charactersToInt64;
using WTF::charactersToUInt64;
using WTF::charactersToIntPtr;
using WTF::charactersToDouble;
using WTF::charactersToFloat;
using WTF::equal;
using WTF::equalIgnoringCase;
using WTF::find;
using WTF::isAllSpecialCharacters;
using WTF::isSpaceOrNewline;
using WTF::reverseFind;
using WTF::ShouldRoundDecimalPlaces;
using WTF::ASCIILiteral;

#include <wtf/text/AtomicString.h>

#define DEFINE_STATIC_LOCAL_STRING_SAFE(name, arguments) \
    static NeverDestroyed<String> name(adoptRef(reinterpret_cast<StringImpl*>(new StaticStringImpl(arguments))))

#ifndef DEFINE_STATIC_LOCAL_STRING
#if COMPILER(GCC) && defined(__APPLE_CC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 0 && __GNUC_PATCHLEVEL__ == 1
#define DEFINE_STATIC_LOCAL_STRING(type, name, arguments) \
    static type* name##Ptr = new type arguments; \
    type& name = *name##Ptr
#else
#define DEFINE_STATIC_LOCAL_STRING(type, name, arguments) \
    static NeverDestroyed<type> name arguments
#endif
#endif

#endif
