Remove String::deprecatedCharactersWithNullTermination() and related code
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 29 Jun 2013 01:39:28 +0000 (01:39 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 29 Jun 2013 01:39:28 +0000 (01:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=118211

Reviewed by Benjamin Poulain.

Source/JavaScriptCore:

* API/JSStringRef.cpp:
(JSStringCreateWithCharactersNoCopy):
Update call to StringImpl::createWithoutCopying.

Source/WTF:

Remove String::deprecatedCharactersWithNullTermination, StringImpl::createWithTerminatingNullCharacter
and the s_hashFlagHasTerminatingNullCharacter flag. We no longer care about whether strings have a
terminating null character.

* wtf/text/StringImpl.cpp:
(WTF::StringImpl::createFromLiteral):
(WTF::StringImpl::createWithoutCopying):
(WTF::StringImpl::getData16SlowCase):
(WTF::StringImpl::sizeInBytes):
* wtf/text/StringImpl.h:
(WTF::StringImpl::StringImpl):
(WTF::StringImpl::createFromLiteral):
(WTF::StringImpl::isolatedCopy):
* wtf/text/WTFString.cpp:
* wtf/text/WTFString.h:

Tools:

Remove all hasTerminatingNullCharacter() checks.

* TestWebKitAPI/Tests/WTF/AtomicString.cpp:
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WTF/StringImpl.cpp:
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@152201 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/API/JSStringRef.cpp
Source/JavaScriptCore/ChangeLog
Source/WTF/ChangeLog
Source/WTF/wtf/text/StringImpl.cpp
Source/WTF/wtf/text/StringImpl.h
Source/WTF/wtf/text/WTFString.cpp
Source/WTF/wtf/text/WTFString.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/AtomicString.cpp
Tools/TestWebKitAPI/Tests/WTF/StringImpl.cpp
Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp

index 437cab3..9de5081 100644 (file)
@@ -63,7 +63,7 @@ JSStringRef JSStringCreateWithUTF8CString(const char* string)
 JSStringRef JSStringCreateWithCharactersNoCopy(const JSChar* chars, size_t numChars)
 {
     initializeThreading();
-    return OpaqueJSString::create(StringImpl::createWithoutCopying(chars, numChars, WTF::DoesNotHaveTerminatingNullCharacter)).leakRef();
+    return OpaqueJSString::create(StringImpl::createWithoutCopying(chars, numChars)).leakRef();
 }
 
 JSStringRef JSStringRetain(JSStringRef string)
index 8084594..f02f490 100644 (file)
@@ -1,3 +1,14 @@
+2013-06-28  Anders Carlsson  <andersca@apple.com>
+
+        Remove String::deprecatedCharactersWithNullTermination() and related code
+        https://bugs.webkit.org/show_bug.cgi?id=118211
+
+        Reviewed by Benjamin Poulain.
+
+        * API/JSStringRef.cpp:
+        (JSStringCreateWithCharactersNoCopy):
+        Update call to StringImpl::createWithoutCopying.
+
 2013-06-27  Timothy Hatcher  <timothy@apple.com>
 
         Notify the debugger about functions created from source code via new Function() or WebCore::JSLazyEventListener.
index c424e5e..df702bc 100644 (file)
@@ -1,3 +1,26 @@
+2013-06-28  Anders Carlsson  <andersca@apple.com>
+
+        Remove String::deprecatedCharactersWithNullTermination() and related code
+        https://bugs.webkit.org/show_bug.cgi?id=118211
+
+        Reviewed by Benjamin Poulain.
+
+        Remove String::deprecatedCharactersWithNullTermination, StringImpl::createWithTerminatingNullCharacter
+        and the s_hashFlagHasTerminatingNullCharacter flag. We no longer care about whether strings have a
+        terminating null character.
+
+        * wtf/text/StringImpl.cpp:
+        (WTF::StringImpl::createFromLiteral):
+        (WTF::StringImpl::createWithoutCopying):
+        (WTF::StringImpl::getData16SlowCase):
+        (WTF::StringImpl::sizeInBytes):
+        * wtf/text/StringImpl.h:
+        (WTF::StringImpl::StringImpl):
+        (WTF::StringImpl::createFromLiteral):
+        (WTF::StringImpl::isolatedCopy):
+        * wtf/text/WTFString.cpp:
+        * wtf/text/WTFString.h:
+
 2013-06-27  Anders Carlsson  <andersca@apple.com>
 
         Add a new String::charactersWithNullTermination() function that returns a vector
index a3428ef..8462631 100644 (file)
@@ -158,7 +158,7 @@ PassRefPtr<StringImpl> StringImpl::createFromLiteral(const char* characters, uns
 {
     ASSERT_WITH_MESSAGE(length, "Use StringImpl::empty() to create an empty string");
     ASSERT(charactersAreAllASCII<LChar>(reinterpret_cast<const LChar*>(characters), length));
-    return adoptRef(new StringImpl(reinterpret_cast<const LChar*>(characters), length, DoesHaveTerminatingNullCharacter, ConstructWithoutCopying));
+    return adoptRef(new StringImpl(reinterpret_cast<const LChar*>(characters), length, ConstructWithoutCopying));
 }
 
 PassRefPtr<StringImpl> StringImpl::createFromLiteral(const char* characters)
@@ -166,20 +166,20 @@ PassRefPtr<StringImpl> StringImpl::createFromLiteral(const char* characters)
     return createFromLiteral(characters, strlen(characters));
 }
 
-PassRefPtr<StringImpl> StringImpl::createWithoutCopying(const UChar* characters, unsigned length, HasTerminatingNullCharacter hasTerminatingNullCharacter)
+PassRefPtr<StringImpl> StringImpl::createWithoutCopying(const UChar* characters, unsigned length)
 {
     if (!length)
         return empty();
 
-    return adoptRef(new StringImpl(characters, length, hasTerminatingNullCharacter, ConstructWithoutCopying));
+    return adoptRef(new StringImpl(characters, length, ConstructWithoutCopying));
 }
 
-PassRefPtr<StringImpl> StringImpl::createWithoutCopying(const LChar* characters, unsigned length, HasTerminatingNullCharacter hasTerminatingNullCharacter)
+PassRefPtr<StringImpl> StringImpl::createWithoutCopying(const LChar* characters, unsigned length)
 {
     if (!length)
         return empty();
 
-    return adoptRef(new StringImpl(characters, length, hasTerminatingNullCharacter, ConstructWithoutCopying));
+    return adoptRef(new StringImpl(characters, length, ConstructWithoutCopying));
 }
 
 PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, LChar*& data)
@@ -333,8 +333,6 @@ const UChar* StringImpl::getData16SlowCase() const
     STRING_STATS_ADD_UPCONVERTED_STRING(m_length);
     
     unsigned len = length();
-    if (hasTerminatingNullCharacter())
-        ++len;
 
     m_copyData16 = static_cast<UChar*>(fastMalloc(len * sizeof(UChar)));
 
@@ -1955,30 +1953,6 @@ PassRefPtr<StringImpl> StringImpl::adopt(QStringData* qStringData)
 }
 #endif
 
-PassRefPtr<StringImpl> StringImpl::createWithTerminatingNullCharacter(const StringImpl& string)
-{
-    // Use createUninitialized instead of 'new StringImpl' so that the string and its buffer
-    // get allocated in a single memory block.
-    unsigned length = string.m_length;
-    if (length >= numeric_limits<unsigned>::max())
-        CRASH();
-    RefPtr<StringImpl> terminatedString;
-    if (string.is8Bit()) {
-        LChar* data;
-        terminatedString = createUninitialized(length + 1, data);
-        memcpy(data, string.m_data8, length * sizeof(LChar));
-        data[length] = 0;
-    } else {
-        UChar* data;
-        terminatedString = createUninitialized(length + 1, data);
-        memcpy(data, string.m_data16, length * sizeof(UChar));
-        data[length] = 0;
-    }
-    --(terminatedString->m_length);
-    terminatedString->m_hashAndFlags = (string.m_hashAndFlags & (~s_flagMask | s_hashFlag8BitBuffer)) | s_hashFlagHasTerminatingNullCharacter;
-    return terminatedString.release();
-}
-
 size_t StringImpl::sizeInBytes() const
 {
     // FIXME: support substrings
@@ -1986,8 +1960,6 @@ size_t StringImpl::sizeInBytes() const
     if (is8Bit()) {
         if (has16BitShadow()) {
             size += 2 * size;
-            if (hasTerminatingNullCharacter())
-                size += 2;
         }
     } else
         size *= 2;
index f88a3a2..d3ca1ff 100644 (file)
@@ -73,12 +73,6 @@ enum TextCaseSensitivity {
     TextCaseInsensitive
 };
 
-enum HasTerminatingNullCharacter {
-    DoesNotHaveTerminatingNullCharacter,
-    DoesHaveTerminatingNullCharacter,
-};
-
-
 typedef bool (*CharacterMatchFunctionPtr)(UChar);
 typedef bool (*IsWhiteSpaceFunctionPtr)(UChar);
 
@@ -249,30 +243,28 @@ private:
     }
 
     enum ConstructWithoutCopyingTag { ConstructWithoutCopying };
-    StringImpl(const UChar* characters, unsigned length, HasTerminatingNullCharacter hasTerminatingNullCharacter, ConstructWithoutCopyingTag)
+    StringImpl(const UChar* characters, unsigned length, ConstructWithoutCopyingTag)
         : m_refCount(s_refCountIncrement)
         , m_length(length)
         , m_data16(characters)
         , m_buffer(0)
-        , m_hashAndFlags(BufferInternal | (hasTerminatingNullCharacter ? s_hashFlagHasTerminatingNullCharacter : 0))
+        , m_hashAndFlags(BufferInternal)
     {
         ASSERT(m_data16);
         ASSERT(m_length);
-        ASSERT(!(m_hashAndFlags & s_hashFlagHasTerminatingNullCharacter) || !characters[length]);
 
         STRING_STATS_ADD_16BIT_STRING(0);
     }
 
-    StringImpl(const LChar* characters, unsigned length, HasTerminatingNullCharacter hasTerminatingNullCharacter, ConstructWithoutCopyingTag)
+    StringImpl(const LChar* characters, unsigned length, ConstructWithoutCopyingTag)
         : m_refCount(s_refCountIncrement)
         , m_length(length)
         , m_data8(characters)
         , m_buffer(0)
-        , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal | (hasTerminatingNullCharacter ? s_hashFlagHasTerminatingNullCharacter : 0))
+        , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal)
     {
         ASSERT(m_data8);
         ASSERT(m_length);
-        ASSERT(!(m_hashAndFlags & s_hashFlagHasTerminatingNullCharacter) || !characters[length]);
 
         STRING_STATS_ADD_8BIT_STRING(0);
     }
@@ -421,15 +413,15 @@ public:
         COMPILE_ASSERT(charactersCount > 1, StringImplFromLiteralNotEmpty);
         COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), StringImplFromLiteralCannotOverflow);
 
-        return createWithoutCopying(reinterpret_cast<const LChar*>(characters), charactersCount - 1, DoesHaveTerminatingNullCharacter);
+        return createWithoutCopying(reinterpret_cast<const LChar*>(characters), charactersCount - 1);
     }
 
     // FIXME: Transition off of these functions to createWithoutCopying instead.
     WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createFromLiteral(const char* characters, unsigned length);
     WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createFromLiteral(const char* characters);
 
-    WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createWithoutCopying(const UChar* characters, unsigned length, HasTerminatingNullCharacter);
-    WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createWithoutCopying(const LChar* characters, unsigned length, HasTerminatingNullCharacter);
+    WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createWithoutCopying(const UChar* characters, unsigned length);
+    WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createWithoutCopying(const LChar* characters, unsigned length);
 
     WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createUninitialized(unsigned length, LChar*& data);
     WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createUninitialized(unsigned length, UChar*& data);
@@ -471,7 +463,6 @@ public:
     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, typename OverflowHandler>
     static PassRefPtr<StringImpl> adopt(Vector<CharType, inlineCapacity, OverflowHandler>& vector)
@@ -541,8 +532,6 @@ public:
         return !length() && !isStatic();
     }
 
-    bool hasTerminatingNullCharacter() const { return m_hashAndFlags & s_hashFlagHasTerminatingNullCharacter; }
-
     bool isAtomic() const { return m_hashAndFlags & s_hashFlagIsAtomic; }
     void setIsAtomic(bool isAtomic)
     {
@@ -788,14 +777,13 @@ private:
     static const unsigned s_refCountFlagIsStaticString = 0x1;
     static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag.
 
-    // The bottom 8 bits in the hash are flags.
-    static const unsigned s_flagCount = 8;
+    // The bottom 7 bits in the hash are flags.
+    static const unsigned s_flagCount = 7;
     static const unsigned s_flagMask = (1u << s_flagCount) - 1;
-    COMPILE_ASSERT(s_flagCount == StringHasher::flagCount, StringHasher_reserves_enough_bits_for_StringImpl_flags);
+    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_hashFlagHas16BitShadow = 1u << 6;
+    static const unsigned s_hashFlag8BitBuffer = 1u << 5;
     static const unsigned s_hashFlagIsAtomic = 1u << 4;
     static const unsigned s_hashFlagDidReportCost = 1u << 3;
     static const unsigned s_hashFlagIsIdentifier = 1u << 2;
@@ -816,7 +804,7 @@ public:
 
         // These values mimic ConstructFromLiteral.
         static const unsigned s_initialRefCount = s_refCountIncrement;
-        static const unsigned s_initialFlags = s_hashFlag8BitBuffer | BufferInternal | s_hashFlagHasTerminatingNullCharacter;
+        static const unsigned s_initialFlags = s_hashFlag8BitBuffer | BufferInternal;
         static const unsigned s_hashShift = s_flagCount;
     };
 
@@ -1362,8 +1350,8 @@ inline PassRefPtr<StringImpl> StringImpl::isolatedCopy() const
 {
     if (!requiresCopy()) {
         if (is8Bit())
-            return StringImpl::createWithoutCopying(m_data8, m_length, hasTerminatingNullCharacter() ? DoesHaveTerminatingNullCharacter : DoesNotHaveTerminatingNullCharacter);
-        return StringImpl::createWithoutCopying(m_data16, m_length, hasTerminatingNullCharacter() ? DoesHaveTerminatingNullCharacter : DoesNotHaveTerminatingNullCharacter);
+            return StringImpl::createWithoutCopying(m_data8, m_length);
+        return StringImpl::createWithoutCopying(m_data16, m_length);
     }
 
     if (is8Bit())
index 0cd2276..7c6e535 100644 (file)
@@ -416,16 +416,6 @@ Vector<UChar> String::charactersWithNullTermination() const
     return result;
 }
 
-const UChar* String::deprecatedCharactersWithNullTermination()
-{
-    if (!m_impl)
-        return 0;
-    if (m_impl->hasTerminatingNullCharacter())
-        return m_impl->characters();
-    m_impl = StringImpl::createWithTerminatingNullCharacter(*m_impl);
-    return m_impl->characters();
-}
-
 String String::format(const char *format, ...)
 {
 #if PLATFORM(QT)
index 11f29d6..482088d 100644 (file)
@@ -282,7 +282,6 @@ public:
         { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }
 
     WTF_EXPORT_STRING_API Vector<UChar> charactersWithNullTermination() const;
-    WTF_EXPORT_STRING_API const UChar* deprecatedCharactersWithNullTermination();
     
     WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned) const; // Ditto.
     
index 570a86f..2fa6461 100644 (file)
@@ -1,3 +1,19 @@
+2013-06-28  Anders Carlsson  <andersca@apple.com>
+
+        Remove String::deprecatedCharactersWithNullTermination() and related code
+        https://bugs.webkit.org/show_bug.cgi?id=118211
+
+        Reviewed by Benjamin Poulain.
+
+        Remove all hasTerminatingNullCharacter() checks.
+
+        * TestWebKitAPI/Tests/WTF/AtomicString.cpp:
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WTF/StringImpl.cpp:
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::TEST):
+
 2013-06-28  Alexey Proskuryakov  <ap@apple.com>
 
         [Mac] Crash when loading is stopped from -didReceiveResponse
index bddf22c..9df71ce 100644 (file)
@@ -35,14 +35,12 @@ TEST(WTF, AtomicStringCreationFromLiteral)
     ASSERT_EQ(strlen("Template Literal"), stringWithTemplate.length());
     ASSERT_TRUE(stringWithTemplate == "Template Literal");
     ASSERT_TRUE(stringWithTemplate.string().is8Bit());
-    ASSERT_TRUE(stringWithTemplate.impl()->hasTerminatingNullCharacter());
 
     const char* programmaticStringData = "Explicit Size Literal";
     AtomicString programmaticString(programmaticStringData, strlen(programmaticStringData), AtomicString::ConstructFromLiteral);
     ASSERT_EQ(strlen(programmaticStringData), programmaticString.length());
     ASSERT_TRUE(programmaticStringData == programmaticStringData);
     ASSERT_TRUE(programmaticString.string().is8Bit());
-    ASSERT_TRUE(programmaticString.impl()->hasTerminatingNullCharacter());
     ASSERT_EQ(programmaticStringData, reinterpret_cast<const char*>(programmaticString.string().characters8()));
 }
 
index 35d6484..10ab9e2 100644 (file)
@@ -37,7 +37,6 @@ TEST(WTF, StringImplCreationFromLiteral)
     ASSERT_EQ(strlen("Template Literal"), stringWithTemplate->length());
     ASSERT_TRUE(equal(stringWithTemplate.get(), "Template Literal"));
     ASSERT_TRUE(stringWithTemplate->is8Bit());
-    ASSERT_TRUE(stringWithTemplate->hasTerminatingNullCharacter());
 
     // Constructor taking the size explicitely.
     const char* programmaticStringData = "Explicit Size Literal";
@@ -46,7 +45,6 @@ TEST(WTF, StringImplCreationFromLiteral)
     ASSERT_TRUE(equal(programmaticString.get(), programmaticStringData));
     ASSERT_EQ(programmaticStringData, reinterpret_cast<const char*>(programmaticString->characters8()));
     ASSERT_TRUE(programmaticString->is8Bit());
-    ASSERT_TRUE(programmaticString->hasTerminatingNullCharacter());
 
     // Constructor without explicit size.
     const char* stringWithoutLengthLiteral = "No Size Literal";
@@ -55,7 +53,6 @@ TEST(WTF, StringImplCreationFromLiteral)
     ASSERT_TRUE(equal(programmaticStringNoLength.get(), stringWithoutLengthLiteral));
     ASSERT_EQ(stringWithoutLengthLiteral, reinterpret_cast<const char*>(programmaticStringNoLength->characters8()));
     ASSERT_TRUE(programmaticStringNoLength->is8Bit());
-    ASSERT_TRUE(programmaticStringNoLength->hasTerminatingNullCharacter());
 }
 
 TEST(WTF, StringImplFromLiteralLoop16BitConversion)
index 4a92cd4..3da40ff 100644 (file)
@@ -38,14 +38,12 @@ TEST(WTF, StringCreationFromLiteral)
     ASSERT_EQ(strlen("Explicit construction syntax"), stringFromLiteral.length());
     ASSERT_TRUE(stringFromLiteral == "Explicit construction syntax");
     ASSERT_TRUE(stringFromLiteral.is8Bit());
-    ASSERT_TRUE(stringFromLiteral.impl()->hasTerminatingNullCharacter());
     ASSERT_TRUE(String("Explicit construction syntax") == stringFromLiteral);
 
     String stringWithTemplate("Template Literal", String::ConstructFromLiteral);
     ASSERT_EQ(strlen("Template Literal"), stringWithTemplate.length());
     ASSERT_TRUE(stringWithTemplate == "Template Literal");
     ASSERT_TRUE(stringWithTemplate.is8Bit());
-    ASSERT_TRUE(stringWithTemplate.impl()->hasTerminatingNullCharacter());
     ASSERT_TRUE(String("Template Literal") == stringWithTemplate);
 }