Reduce usages of String::createUninitialized
authorsullivan@chromium.org <sullivan@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Aug 2011 04:22:59 +0000 (04:22 +0000)
committersullivan@chromium.org <sullivan@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Aug 2011 04:22:59 +0000 (04:22 +0000)
https://bugs.webkit.org/show_bug.cgi?id=66399

Constructs Strings with StringBuilder instead of calling createUninitialized.

Reviewed by Adam Barth.

No new tests; no functional change.

* dom/StyleElement.cpp:
(WebCore::StyleElement::process):
* dom/Text.cpp:
(WebCore::Text::wholeText):
* html/parser/HTMLSourceTracker.cpp:
(WebCore::HTMLSourceTracker::sourceForToken):
* platform/graphics/Font.cpp:
(WebCore::Font::normalizeSpaces):
* platform/text/TextCodecUserDefined.cpp:
(WebCore::TextCodecUserDefined::decode):

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

Source/WebCore/ChangeLog
Source/WebCore/dom/StyleElement.cpp
Source/WebCore/dom/Text.cpp
Source/WebCore/html/parser/HTMLSourceTracker.cpp
Source/WebCore/platform/graphics/Font.cpp
Source/WebCore/platform/text/TextCodecUserDefined.cpp

index 76ca1b2..e3d8ebd 100644 (file)
@@ -1,3 +1,25 @@
+2011-08-17  Annie Sullivan  <sullivan@chromium.org>
+
+        Reduce usages of String::createUninitialized
+        https://bugs.webkit.org/show_bug.cgi?id=66399
+
+        Constructs Strings with StringBuilder instead of calling createUninitialized.
+
+        Reviewed by Adam Barth.
+
+        No new tests; no functional change.
+
+        * dom/StyleElement.cpp:
+        (WebCore::StyleElement::process):
+        * dom/Text.cpp:
+        (WebCore::Text::wholeText):
+        * html/parser/HTMLSourceTracker.cpp:
+        (WebCore::HTMLSourceTracker::sourceForToken):
+        * platform/graphics/Font.cpp:
+        (WebCore::Font::normalizeSpaces):
+        * platform/text/TextCodecUserDefined.cpp:
+        (WebCore::TextCodecUserDefined::decode):
+
 2011-08-17  Emil A Eklund  <eae@chromium.org>
 
         Switch FrameView to to new layout types
index a366de9..c32111a 100644 (file)
@@ -28,6 +28,7 @@
 #include "MediaList.h"
 #include "MediaQueryEvaluator.h"
 #include "ScriptableDocumentParser.h"
+#include <wtf/text/StringBuilder.h>
 
 namespace WebCore {
 
@@ -125,21 +126,17 @@ void StyleElement::process(Element* e)
             resultLength += length;
         }
     }
-    UChar* text;
-    String sheetText = String::createUninitialized(resultLength, text);
+    StringBuilder sheetText;
+    sheetText.reserveCapacity(resultLength);
 
-    UChar* p = text;
     for (Node* c = e->firstChild(); c; c = c->nextSibling()) {
         if (isValidStyleChild(c)) {
-            String nodeValue = c->nodeValue();
-            unsigned nodeLength = nodeValue.length();
-            memcpy(p, nodeValue.characters(), nodeLength * sizeof(UChar));
-            p += nodeLength;
+            sheetText.append(c->nodeValue());
         }
     }
-    ASSERT(p == text + resultLength);
+    ASSERT(sheetText.length() == resultLength);
 
-    createSheet(e, m_startLineNumber, sheetText);
+    createSheet(e, m_startLineNumber, sheetText.toString());
 }
 
 void StyleElement::createSheet(Element* e, int startLineNumber, const String& text)
index 490e13f..b88c829 100644 (file)
@@ -33,6 +33,7 @@
 #endif
 
 #include <wtf/text/CString.h>
+#include <wtf/text/StringBuilder.h>
 
 using namespace std;
 
@@ -124,21 +125,17 @@ String Text::wholeText() const
             CRASH();
         resultLength += data.length();
     }
-    UChar* resultData;
-    String result = String::createUninitialized(resultLength, resultData);
-    UChar* p = resultData;
+    StringBuilder result;
+    result.reserveCapacity(resultLength);
     for (const Node* n = startText; n != onePastEndText; n = n->nextSibling()) {
         if (!n->isTextNode())
             continue;
         const Text* t = static_cast<const Text*>(n);
-        const String& data = t->data();
-        unsigned dataLength = data.length();
-        memcpy(p, data.characters(), dataLength * sizeof(UChar));
-        p += dataLength;
+        result.append(t->data());
     }
-    ASSERT(p == resultData + resultLength);
+    ASSERT(result.length() == resultLength);
 
-    return result;
+    return result.toString();
 }
 
 PassRefPtr<Text> Text::replaceWholeText(const String& newText, ExceptionCode&)
index 70cf709..3b0892d 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "config.h"
 #include "HTMLSourceTracker.h"
+#include <wtf/text/StringBuilder.h>
 
 namespace WebCore {
 
@@ -55,14 +56,15 @@ String HTMLSourceTracker::sourceForToken(const HTMLToken& token)
         return m_cachedSourceForToken;
 
     ASSERT(!token.startIndex());
-    UChar* data = 0;
-    int length = token.endIndex() - token.startIndex() - m_sourceFromPreviousSegments.length();
-    String source = String::createUninitialized(length, data);
+    int length = token.endIndex() - token.startIndex();
+    StringBuilder source;
+    source.reserveCapacity(length);
+    source.append(m_sourceFromPreviousSegments);
     for (int i = 0; i < length; ++i) {
-        data[i] = *m_source;
+        source.append(*m_source);
         m_source.advance();
     }
-    m_cachedSourceForToken = m_sourceFromPreviousSegments + source;
+    m_cachedSourceForToken = source.toString();
     return m_cachedSourceForToken;
 }
 
index 06d35a4..98eecf8 100644 (file)
@@ -36,6 +36,7 @@
 #include "WidthIterator.h"
 #include <wtf/MainThread.h>
 #include <wtf/MathExtras.h>
+#include <wtf/text/StringBuilder.h>
 #include <wtf/UnusedParam.h>
 
 using namespace WTF;
@@ -223,13 +224,13 @@ int Font::offsetForPosition(const TextRun& run, float x, bool includePartialGlyp
 
 String Font::normalizeSpaces(const UChar* characters, unsigned length)
 {
-    UChar* buffer;
-    String normalized = String::createUninitialized(length, buffer);
+    StringBuilder normalized;
+    normalized.reserveCapacity(length);
 
     for (unsigned i = 0; i < length; ++i)
-        buffer[i] = normalizeSpaces(characters[i]);
+        normalized.append(normalizeSpaces(characters[i]));
 
-    return normalized;
+    return normalized.toString();
 }
 
 static bool shouldUseFontSmoothing = true;
index d81dc76..06dd91f 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/StringBuffer.h>
+#include <wtf/text/StringBuilder.h>
 #include <wtf/PassOwnPtr.h>
 
 namespace WebCore {
@@ -51,15 +52,15 @@ void TextCodecUserDefined::registerCodecs(TextCodecRegistrar registrar)
 
 String TextCodecUserDefined::decode(const char* bytes, size_t length, bool, bool, bool&)
 {
-    UChar* buffer;
-    String result = String::createUninitialized(length, buffer);
+    StringBuilder result;
+    result.reserveCapacity(length);
 
     for (size_t i = 0; i < length; ++i) {
         signed char c = bytes[i];
-        buffer[i] = c & 0xF7FF;
+        result.append(static_cast<UChar>(c & 0xF7FF));
     }
 
-    return result;
+    return result.toString();
 }
 
 static CString encodeComplexUserDefined(const UChar* characters, size_t length, UnencodableHandling handling)