diff options
author | Iain Merrick <husky@google.com> | 2010-08-19 17:55:56 +0100 |
---|---|---|
committer | Iain Merrick <husky@google.com> | 2010-08-23 11:05:40 +0100 |
commit | f486d19d62f1bc33246748b14b14a9dfa617b57f (patch) | |
tree | 195485454c93125455a30e553a73981c3816144d /JavaScriptCore/wtf/text/WTFString.h | |
parent | 6ba0b43722d16bc295606bec39f396f596e4fef1 (diff) | |
download | external_webkit-f486d19d62f1bc33246748b14b14a9dfa617b57f.zip external_webkit-f486d19d62f1bc33246748b14b14a9dfa617b57f.tar.gz external_webkit-f486d19d62f1bc33246748b14b14a9dfa617b57f.tar.bz2 |
Merge WebKit at r65615 : Initial merge by git.
Change-Id: Ifbf384f4531e3b58475a662e38195c2d9152ae79
Diffstat (limited to 'JavaScriptCore/wtf/text/WTFString.h')
-rw-r--r-- | JavaScriptCore/wtf/text/WTFString.h | 226 |
1 files changed, 122 insertions, 104 deletions
diff --git a/JavaScriptCore/wtf/text/WTFString.h b/JavaScriptCore/wtf/text/WTFString.h index 6af519c..fafef12 100644 --- a/JavaScriptCore/wtf/text/WTFString.h +++ b/JavaScriptCore/wtf/text/WTFString.h @@ -1,6 +1,6 @@ /* * (C) 1999 Lars Knoll (knoll@kde.org) - * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * 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 @@ -72,45 +72,43 @@ intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // ignores trai double charactersToDouble(const UChar*, size_t, bool* ok = 0); float charactersToFloat(const UChar*, size_t, bool* ok = 0); -int find(const UChar*, size_t, UChar, int startPosition = 0); -int reverseFind(const UChar*, size_t, UChar, int startPosition = -1); - class String { public: - String() { } // gives null string, distinguishable from an empty string - String(const UChar* str, unsigned len) - { - if (!str) - return; - m_impl = StringImpl::create(str, len); - } - String(const char* str) - { - if (!str) - return; - m_impl = StringImpl::create(str); - } - String(const char* str, unsigned length) - { - if (!str) - return; - m_impl = StringImpl::create(str, length); - } - String(const UChar*); // Specifically for null terminated UTF-16 - String(StringImpl* i) : m_impl(i) { } - String(PassRefPtr<StringImpl> i) : m_impl(i) { } - String(RefPtr<StringImpl> i) : m_impl(i) { } + // Construct a null string, distinguishable from an empty string. + String() { } - void swap(String& o) { m_impl.swap(o.m_impl); } + // Construct a string with UTF-16 data. + String(const UChar* characters, unsigned length); - // Hash table deleted values, which are only constructed and never copied or destroyed. - String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { } - bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); } + // Construct a string with UTF-16 data, from a null-terminated source. + String(const UChar*); + + // Construct a string with latin1 data. + String(const char* characters, unsigned length); + + // Construct a string with latin1 data, from a null-terminated source. + String(const char* characters); + + // Construct a string referencing an existing StringImpl. + String(StringImpl* impl) : m_impl(impl) { } + String(PassRefPtr<StringImpl> impl) : m_impl(impl) { } + String(RefPtr<StringImpl> impl) : m_impl(impl) { } + + // Inline the destructor. + ALWAYS_INLINE ~String() { } + + void swap(String& o) { m_impl.swap(o.m_impl); } static String adopt(StringBuffer& buffer) { return StringImpl::adopt(buffer); } - static String adopt(Vector<UChar>& vector) { return StringImpl::adopt(vector); } + template<size_t inlineCapacity> + static String adopt(Vector<UChar, inlineCapacity>& vector) { return StringImpl::adopt(vector); } - ALWAYS_INLINE unsigned length() const + bool isNull() const { return !m_impl; } + bool isEmpty() const { return !m_impl || !m_impl->length(); } + + StringImpl* impl() const { return m_impl.get(); } + + unsigned length() const { if (!m_impl) return 0; @@ -124,34 +122,67 @@ public: return m_impl->characters(); } - const UChar* charactersWithNullTermination(); - - UChar operator[](unsigned i) const // if i >= length(), returns 0 + CString ascii() const; + CString latin1() const; + CString utf8(bool strict = false) const; + + UChar operator[](unsigned index) const { - if (!m_impl || i >= m_impl->length()) + if (!m_impl || index >= m_impl->length()) return 0; - return m_impl->characters()[i]; + return m_impl->characters()[index]; } - UChar32 characterStartingAt(unsigned) const; // Ditto. + + static String number(short); + static String number(unsigned short); + static String number(int); + static String number(unsigned); + static String number(long); + static String number(unsigned long); + static String number(long long); + static String number(unsigned long long); + static String number(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, unsigned start = 0) 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 char* str, unsigned start = 0) const + { return m_impl ? m_impl->find(str, 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 char* 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 char* 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); } + + const UChar* charactersWithNullTermination(); - bool contains(UChar c) const { return find(c) != -1; } - bool contains(const char* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != -1; } - bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != -1; } - - int find(UChar c, int start = 0) const - { return m_impl ? m_impl->find(c, start) : -1; } - int find(CharacterMatchFunctionPtr matchFunction, int start = 0) const - { return m_impl ? m_impl->find(matchFunction, start) : -1; } - int find(const char* str, int start = 0, bool caseSensitive = true) const - { return m_impl ? m_impl->find(str, start, caseSensitive) : -1; } - int find(const String& str, int start = 0, bool caseSensitive = true) const - { return m_impl ? m_impl->find(str.impl(), start, caseSensitive) : -1; } - - int reverseFind(UChar c, int start = -1) const - { return m_impl ? m_impl->reverseFind(c, start) : -1; } - int reverseFind(const String& str, int start = -1, bool caseSensitive = true) const - { return m_impl ? m_impl->reverseFind(str.impl(), start, caseSensitive) : -1; } + UChar32 characterStartingAt(unsigned) const; // Ditto. + bool contains(UChar c) const { return find(c) != notFound; } + bool contains(const char* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != notFound; } + bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != notFound; } + bool startsWith(const String& s, bool caseSensitive = true) const { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); } bool endsWith(const String& s, bool caseSensitive = true) const @@ -177,6 +208,7 @@ public: void remove(unsigned pos, int len = 1); String substring(unsigned pos, unsigned len = UINT_MAX) const; + String substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const; String left(unsigned len) const { return substring(0, len); } String right(unsigned len) const { return substring(length() - len, len); } @@ -192,17 +224,11 @@ public: // Return the string with case folded for case insensitive comparison. String foldCase() const; - static String number(short); - static String number(unsigned short); - static String number(int); - static String number(unsigned); - static String number(long); - static String number(unsigned long); - static String number(long long); - static String number(unsigned long long); - static String number(double); - +#if !PLATFORM(QT) static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2); +#else + 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. @@ -238,11 +264,6 @@ public: // to ever prefer copy() over plain old assignment. String threadsafeCopy() const; - bool isNull() const { return !m_impl; } - ALWAYS_INLINE bool isEmpty() const { return !m_impl || !m_impl->length(); } - - StringImpl* impl() const { return m_impl.get(); } - #if PLATFORM(CF) String(CFStringRef); CFStringRef createCFString() const; @@ -272,11 +293,6 @@ public: operator BString() const; #endif - Vector<char> ascii() const; - - CString latin1() const; - CString utf8() const; - static String fromUTF8(const char*, size_t); static String fromUTF8(const char*); @@ -288,6 +304,10 @@ public: bool containsOnlyASCII() const { return charactersAreAllASCII(characters(), length()); } + // Hash table deleted values, which are only constructed and never copied or destroyed. + String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { } + bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); } + private: RefPtr<StringImpl> m_impl; }; @@ -345,43 +365,37 @@ inline bool charactersAreAllASCII(const UChar* characters, size_t length) int codePointCompare(const String&, const String&); -inline int find(const UChar* characters, size_t length, UChar character, int startPosition) +inline size_t find(const UChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0) { - if (startPosition >= static_cast<int>(length)) - return -1; - for (size_t i = startPosition; i < length; ++i) { - if (characters[i] == character) - return static_cast<int>(i); + while (index < length) { + if (characters[index] == matchCharacter) + return index; + ++index; } - return -1; + return notFound; } -inline int find(const UChar* characters, size_t length, CharacterMatchFunctionPtr matchFunction, int startPosition) +inline size_t find(const UChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0) { - if (startPosition >= static_cast<int>(length)) - return -1; - for (size_t i = startPosition; i < length; ++i) { - if (matchFunction(characters[i])) - return static_cast<int>(i); + while (index < length) { + if (matchFunction(characters[index])) + return index; + ++index; } - return -1; + return notFound; } -inline int reverseFind(const UChar* characters, size_t length, UChar character, int startPosition) +inline size_t reverseFind(const UChar* characters, unsigned length, UChar matchCharacter, unsigned index = UINT_MAX) { - if (startPosition >= static_cast<int>(length) || !length) - return -1; - if (startPosition < 0) - startPosition += static_cast<int>(length); - while (true) { - if (characters[startPosition] == character) - return startPosition; - if (!startPosition) - return -1; - startPosition--; + if (!length) + return notFound; + if (index >= length) + index = length - 1; + while (characters[index] != matchCharacter) { + if (!index--) + return notFound; } - ASSERT_NOT_REACHED(); - return -1; + return index; } inline void append(Vector<UChar>& vector, const String& string) @@ -417,6 +431,11 @@ template<> struct DefaultHash<String> { typedef StringHash Hash; }; +template <> struct VectorTraits<String> : SimpleClassVectorTraits +{ + static const bool canInitializeWithMemset = true; +}; + } using WTF::CString; @@ -433,6 +452,5 @@ using WTF::charactersAreAllASCII; using WTF::charactersToInt; using WTF::charactersToFloat; using WTF::charactersToDouble; -using WTF::operator+; #endif |