Title: [133731] trunk
Revision
133731
Author
[email protected]
Date
2012-11-07 01:43:05 -0800 (Wed, 07 Nov 2012)

Log Message

Add replaceWithLiteral() method to WTF::String
https://bugs.webkit.org/show_bug.cgi?id=101257

Patch by Christophe Dumez <[email protected]> on 2012-11-07
Reviewed by Benjamin Poulain.

Source/WebCore:

Substitute String::replace() calls by String::replaceWithLiteral() where
adequate, for efficiency.

No new tests, no behavior change.

* dom/Node.cpp:
(WebCore::Node::showNode):
* editing/EditingStyle.cpp:
(WebCore::StyleChange::extractTextStyles):
* editing/MarkupAccumulator.cpp:
(WebCore::MarkupAccumulator::appendQuotedURLAttributeValue):
* html/HTMLAnchorElement.cpp:
(WebCore::HTMLAnchorElement::setSearch):
* loader/FormSubmission.cpp:
(WebCore::appendMailtoPostFormDataToURL):
* platform/network/soup/ProxyResolverSoup.cpp:
(soupProxyResolverWkSetProperty):
* plugins/PluginView.cpp:
(WebCore::makeURL):
* rendering/InlineTextBox.cpp:
(WebCore::InlineTextBox::showBox):
* xml/XSLTProcessor.cpp:
(WebCore::transformTextStringToXHTMLDocumentString):

Source/WebKit/blackberry:

Substitute String::replace() calls by String::replaceWithLiteral() where
adequate, for efficiency.

* WebCoreSupport/SelectPopupClient.cpp:
(WebCore::SelectPopupClient::generateHTML):

Source/WebKit2:

Substitute String::replace() calls by String::replaceWithLiteral() where
adequate, for efficiency.

* WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp:
(WebKit::makeURLString):

Source/WTF:

Add replaceWithLiteral() method to WTF::String that takes
replacement string as a literal to avoid uselessly constructing
a String object.

* wtf/text/StringImpl.cpp:
(WTF::StringImpl::replace):
(WTF):
* wtf/text/StringImpl.h:
(WTF::StringImpl::replace):
(StringImpl):
* wtf/text/WTFString.h:
(String):
(WTF::String::replaceWithLiteral):

Tools:

Add API tests for String::replaceWithLiteral() and corresponding
StringImpl methods.

* TestWebKitAPI/CMakeLists.txt: Add WTFString API tests to CMake.
* TestWebKitAPI/Tests/WTF/StringImpl.cpp:
(TestWebKitAPI::TEST):
(TestWebKitAPI):
* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (133730 => 133731)


--- trunk/Source/WTF/ChangeLog	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WTF/ChangeLog	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1,3 +1,24 @@
+2012-11-07  Christophe Dumez  <[email protected]>
+
+        Add replaceWithLiteral() method to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=101257
+
+        Reviewed by Benjamin Poulain.
+
+        Add replaceWithLiteral() method to WTF::String that takes
+        replacement string as a literal to avoid uselessly constructing
+        a String object.
+
+        * wtf/text/StringImpl.cpp:
+        (WTF::StringImpl::replace):
+        (WTF):
+        * wtf/text/StringImpl.h:
+        (WTF::StringImpl::replace):
+        (StringImpl):
+        * wtf/text/WTFString.h:
+        (String):
+        (WTF::String::replaceWithLiteral):
+
 2012-11-06  Michael Saboff  <[email protected]>
 
         StringBuilder::append(UChar) with an 8 bit quantity shouldn't change the contents to 16 bits

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (133730 => 133731)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1382,21 +1382,30 @@
 {
     if (!replacement)
         return this;
-        
-    unsigned repStrLength = replacement->length();
+
+    if (replacement->is8Bit())
+        return replace(pattern, replacement->m_data8, replacement->length());
+
+    return replace(pattern, replacement->m_data16, replacement->length());
+}
+
+PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, const LChar* replacement, unsigned repStrLength)
+{
+    ASSERT(replacement);
+
     size_t srcSegmentStart = 0;
     unsigned matchCount = 0;
-    
+
     // Count the matches.
     while ((srcSegmentStart = find(pattern, srcSegmentStart)) != notFound) {
         ++matchCount;
         ++srcSegmentStart;
     }
-    
+
     // If we have 0 matches then we don't have to do any more work.
     if (!matchCount)
         return this;
-    
+
     if (repStrLength && matchCount > numeric_limits<unsigned>::max() / repStrLength)
         CRASH();
 
@@ -1412,16 +1421,8 @@
     unsigned srcSegmentLength;
     srcSegmentStart = 0;
     unsigned dstOffset = 0;
-    bool srcIs8Bit = is8Bit();
-    bool replacementIs8Bit = replacement->is8Bit();
-    
-    // There are 4 cases:
-    // 1. This and replacement are both 8 bit.
-    // 2. This and replacement are both 16 bit.
-    // 3. This is 8 bit and replacement is 16 bit.
-    // 4. This is 16 bit and replacement is 8 bit.
-    if (srcIs8Bit && replacementIs8Bit) {
-        // Case 1
+
+    if (is8Bit()) {
         LChar* data;
         RefPtr<StringImpl> newImpl = createUninitialized(newSize, data);
 
@@ -1429,7 +1430,7 @@
             srcSegmentLength = srcSegmentEnd - srcSegmentStart;
             memcpy(data + dstOffset, m_data8 + srcSegmentStart, srcSegmentLength * sizeof(LChar));
             dstOffset += srcSegmentLength;
-            memcpy(data + dstOffset, replacement->m_data8, repStrLength * sizeof(LChar));
+            memcpy(data + dstOffset, replacement, repStrLength * sizeof(LChar));
             dstOffset += repStrLength;
             srcSegmentStart = srcSegmentEnd + 1;
         }
@@ -1447,37 +1448,99 @@
 
     while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != notFound) {
         srcSegmentLength = srcSegmentEnd - srcSegmentStart;
-        if (srcIs8Bit) {
-            // Case 3.
-            for (unsigned i = 0; i < srcSegmentLength; i++)
-                data[i + dstOffset] = m_data8[i + srcSegmentStart];
-        } else {
-            // Cases 2 & 4.
-            memcpy(data + dstOffset, m_data16 + srcSegmentStart, srcSegmentLength * sizeof(UChar));
-        }
+        memcpy(data + dstOffset, m_data16 + srcSegmentStart, srcSegmentLength * sizeof(UChar));
+
         dstOffset += srcSegmentLength;
-        if (replacementIs8Bit) {
-            // Case 4.
-            for (unsigned i = 0; i < repStrLength; i++)
-                data[i + dstOffset] = replacement->m_data8[i];
-        } else {
-            // Cases 2 & 3.
-            memcpy(data + dstOffset, replacement->m_data16, repStrLength * sizeof(UChar));
-        }
+        for (unsigned i = 0; i < repStrLength; ++i)
+            data[i + dstOffset] = replacement[i];
+
         dstOffset += repStrLength;
         srcSegmentStart = srcSegmentEnd + 1;
     }
 
     srcSegmentLength = m_length - srcSegmentStart;
-    if (srcIs8Bit) {
-        // Case 3.
-        for (unsigned i = 0; i < srcSegmentLength; i++)
+    memcpy(data + dstOffset, m_data16 + srcSegmentStart, srcSegmentLength * sizeof(UChar));
+
+    ASSERT(dstOffset + srcSegmentLength == newImpl->length());
+
+    return newImpl.release();
+}
+
+PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, const UChar* replacement, unsigned repStrLength)
+{
+    ASSERT(replacement);
+
+    size_t srcSegmentStart = 0;
+    unsigned matchCount = 0;
+
+    // Count the matches.
+    while ((srcSegmentStart = find(pattern, srcSegmentStart)) != notFound) {
+        ++matchCount;
+        ++srcSegmentStart;
+    }
+
+    // If we have 0 matches then we don't have to do any more work.
+    if (!matchCount)
+        return this;
+
+    if (repStrLength && matchCount > numeric_limits<unsigned>::max() / repStrLength)
+        CRASH();
+
+    unsigned replaceSize = matchCount * repStrLength;
+    unsigned newSize = m_length - matchCount;
+    if (newSize >= (numeric_limits<unsigned>::max() - replaceSize))
+        CRASH();
+
+    newSize += replaceSize;
+
+    // Construct the new data.
+    size_t srcSegmentEnd;
+    unsigned srcSegmentLength;
+    srcSegmentStart = 0;
+    unsigned dstOffset = 0;
+
+    if (is8Bit()) {
+        UChar* data;
+        RefPtr<StringImpl> newImpl = createUninitialized(newSize, data);
+
+        while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != notFound) {
+            srcSegmentLength = srcSegmentEnd - srcSegmentStart;
+            for (unsigned i = 0; i < srcSegmentLength; ++i)
+                data[i + dstOffset] = m_data8[i + srcSegmentStart];
+
+            dstOffset += srcSegmentLength;
+            memcpy(data + dstOffset, replacement, repStrLength * sizeof(UChar));
+
+            dstOffset += repStrLength;
+            srcSegmentStart = srcSegmentEnd + 1;
+        }
+
+        srcSegmentLength = m_length - srcSegmentStart;
+        for (unsigned i = 0; i < srcSegmentLength; ++i)
             data[i + dstOffset] = m_data8[i + srcSegmentStart];
-    } else {
-        // Cases 2 & 4.
+
+        ASSERT(dstOffset + srcSegmentLength == newImpl->length());
+
+        return newImpl.release();
+    }
+
+    UChar* data;
+    RefPtr<StringImpl> newImpl = createUninitialized(newSize, data);
+
+    while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != notFound) {
+        srcSegmentLength = srcSegmentEnd - srcSegmentStart;
         memcpy(data + dstOffset, m_data16 + srcSegmentStart, srcSegmentLength * sizeof(UChar));
+
+        dstOffset += srcSegmentLength;
+        memcpy(data + dstOffset, replacement, repStrLength * sizeof(UChar));
+
+        dstOffset += repStrLength;
+        srcSegmentStart = srcSegmentEnd + 1;
     }
 
+    srcSegmentLength = m_length - srcSegmentStart;
+    memcpy(data + dstOffset, m_data16 + srcSegmentStart, srcSegmentLength * sizeof(UChar));
+
     ASSERT(dstOffset + srcSegmentLength == newImpl->length());
 
     return newImpl.release();

Modified: trunk/Source/WTF/wtf/text/StringImpl.h (133730 => 133731)


--- trunk/Source/WTF/wtf/text/StringImpl.h	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WTF/wtf/text/StringImpl.h	2012-11-07 09:43:05 UTC (rev 133731)
@@ -709,6 +709,9 @@
 
     WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(UChar, UChar);
     WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(UChar, StringImpl*);
+    ALWAYS_INLINE PassRefPtr<StringImpl> replace(UChar pattern, const char* replacement, unsigned replacementLength) { return replace(pattern, reinterpret_cast<const LChar*>(replacement), replacementLength); }
+    WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(UChar, const LChar*, unsigned replacementLength);
+    PassRefPtr<StringImpl> replace(UChar, const UChar*, unsigned replacementLength);
     WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(StringImpl*, StringImpl*);
     WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(unsigned index, unsigned len, StringImpl*);
 

Modified: trunk/Source/WTF/wtf/text/WTFString.h (133730 => 133731)


--- trunk/Source/WTF/wtf/text/WTFString.h	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2012-11-07 09:43:05 UTC (rev 133731)
@@ -307,6 +307,15 @@
     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); }

Modified: trunk/Source/WebCore/ChangeLog (133730 => 133731)


--- trunk/Source/WebCore/ChangeLog	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/ChangeLog	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1,3 +1,34 @@
+2012-11-07  Christophe Dumez  <[email protected]>
+
+        Add replaceWithLiteral() method to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=101257
+
+        Reviewed by Benjamin Poulain.
+
+        Substitute String::replace() calls by String::replaceWithLiteral() where
+        adequate, for efficiency.
+
+        No new tests, no behavior change.
+
+        * dom/Node.cpp:
+        (WebCore::Node::showNode):
+        * editing/EditingStyle.cpp:
+        (WebCore::StyleChange::extractTextStyles):
+        * editing/MarkupAccumulator.cpp:
+        (WebCore::MarkupAccumulator::appendQuotedURLAttributeValue):
+        * html/HTMLAnchorElement.cpp:
+        (WebCore::HTMLAnchorElement::setSearch):
+        * loader/FormSubmission.cpp:
+        (WebCore::appendMailtoPostFormDataToURL):
+        * platform/network/soup/ProxyResolverSoup.cpp:
+        (soupProxyResolverWkSetProperty):
+        * plugins/PluginView.cpp:
+        (WebCore::makeURL):
+        * rendering/InlineTextBox.cpp:
+        (WebCore::InlineTextBox::showBox):
+        * xml/XSLTProcessor.cpp:
+        (WebCore::transformTextStringToXHTMLDocumentString):
+
 2012-11-07  Kenneth Rohde Christiansen  <[email protected]>
 
         Remove support for "desktop-width" in the viewport meta tag

Modified: trunk/Source/WebCore/dom/Node.cpp (133730 => 133731)


--- trunk/Source/WebCore/dom/Node.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/dom/Node.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -2105,8 +2105,8 @@
         prefix = "";
     if (isTextNode()) {
         String value = nodeValue();
-        value.replace('\\', "\\\\");
-        value.replace('\n', "\\n");
+        value.replaceWithLiteral('\\', "\\\\");
+        value.replaceWithLiteral('\n', "\\n");
         fprintf(stderr, "%s%s\t%p \"%s\"\n", prefix, nodeName().utf8().data(), this, value.utf8().data());
     } else {
         StringBuilder attrs;

Modified: trunk/Source/WebCore/editing/EditingStyle.cpp (133730 => 133731)


--- trunk/Source/WebCore/editing/EditingStyle.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/editing/EditingStyle.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1427,7 +1427,7 @@
 
     m_applyFontFace = style->getPropertyValue(CSSPropertyFontFamily);
     // Remove single quotes for Outlook 2007 compatibility. See https://bugs.webkit.org/show_bug.cgi?id=79448
-    m_applyFontFace.replace('\'', "");
+    m_applyFontFace.replaceWithLiteral('\'', "");
     style->removeProperty(CSSPropertyFontFamily);
 
     if (RefPtr<CSSValue> fontSize = style->getPropertyCSSValue(CSSPropertyFontSize)) {

Modified: trunk/Source/WebCore/editing/MarkupAccumulator.cpp (133730 => 133731)


--- trunk/Source/WebCore/editing/MarkupAccumulator.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/editing/MarkupAccumulator.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -216,7 +216,7 @@
         // minimal escaping for _javascript_ urls
         if (strippedURLString.contains('"')) {
             if (strippedURLString.contains('\''))
-                strippedURLString.replace('"', "&quot;");
+                strippedURLString.replaceWithLiteral('"', "&quot;");
             else
                 quoteChar = '\'';
         }

Modified: trunk/Source/WebCore/html/HTMLAnchorElement.cpp (133730 => 133731)


--- trunk/Source/WebCore/html/HTMLAnchorElement.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/html/HTMLAnchorElement.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -463,7 +463,7 @@
     KURL url = ""
     String newSearch = (value[0] == '?') ? value.substring(1) : value;
     // Make sure that '#' in the query does not leak to the hash.
-    url.setQuery(newSearch.replace('#', "%23"));
+    url.setQuery(newSearch.replaceWithLiteral('#', "%23"));
 
     setHref(url.string());
 }

Modified: trunk/Source/WebCore/loader/FormSubmission.cpp (133730 => 133731)


--- trunk/Source/WebCore/loader/FormSubmission.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/loader/FormSubmission.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -67,13 +67,13 @@
 
     if (equalIgnoringCase(encodingType, "text/plain")) {
         // Convention seems to be to decode, and s/&/\r\n/. Also, spaces are encoded as %20.
-        body = decodeURLEscapeSequences(body.replace('&', "\r\n").replace('+', ' ') + "\r\n");
+        body = decodeURLEscapeSequences(body.replaceWithLiteral('&', "\r\n").replace('+', ' ') + "\r\n");
     }
 
     Vector<char> bodyData;
     bodyData.append("body=", 5);
     FormDataBuilder::encodeStringAsFormData(bodyData, body.utf8());
-    body = String(bodyData.data(), bodyData.size()).replace('+', "%20");
+    body = String(bodyData.data(), bodyData.size()).replaceWithLiteral('+', "%20");
 
     String query = url.query();
     if (!query.isEmpty())

Modified: trunk/Source/WebCore/platform/network/soup/ProxyResolverSoup.cpp (133730 => 133731)


--- trunk/Source/WebCore/platform/network/soup/ProxyResolverSoup.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/platform/network/soup/ProxyResolverSoup.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -84,7 +84,7 @@
     case PROP_NO_PROXY:
         priv->noProxy = g_value_get_string(value);
         priv->proxyExceptions.clear();
-        String::fromUTF8(priv->noProxy.data()).replace(' ', "").split(',', priv->proxyExceptions);
+        String::fromUTF8(priv->noProxy.data()).replaceWithLiteral(' ', "").split(',', priv->proxyExceptions);
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, pspec);

Modified: trunk/Source/WebCore/plugins/PluginView.cpp (133730 => 133731)


--- trunk/Source/WebCore/plugins/PluginView.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/plugins/PluginView.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -548,8 +548,8 @@
     String urlString = relativeURLString;
 
     // Strip return characters.
-    urlString.replace('\n', "");
-    urlString.replace('\r', "");
+    urlString.replaceWithLiteral('\n', "");
+    urlString.replaceWithLiteral('\r', "");
 
     return KURL(baseURL, urlString);
 }

Modified: trunk/Source/WebCore/rendering/InlineTextBox.cpp (133730 => 133731)


--- trunk/Source/WebCore/rendering/InlineTextBox.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/rendering/InlineTextBox.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1436,8 +1436,8 @@
     const RenderText* obj = toRenderText(renderer());
     String value = obj->text();
     value = value.substring(start(), len());
-    value.replace('\\', "\\\\");
-    value.replace('\n', "\\n");
+    value.replaceWithLiteral('\\', "\\\\");
+    value.replaceWithLiteral('\n', "\\n");
     printedCharacters += fprintf(stderr, "%s\t%p", boxName(), this);
     for (; printedCharacters < showTreeCharacterOffset; printedCharacters++)
         fputc(' ', stderr);

Modified: trunk/Source/WebCore/xml/XSLTProcessor.cpp (133730 => 133731)


--- trunk/Source/WebCore/xml/XSLTProcessor.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebCore/xml/XSLTProcessor.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -49,8 +49,8 @@
 static inline void transformTextStringToXHTMLDocumentString(String& text)
 {
     // Modify the output so that it is a well-formed XHTML document with a <pre> tag enclosing the text.
-    text.replace('&', "&amp;");
-    text.replace('<', "&lt;");
+    text.replaceWithLiteral('&', "&amp;");
+    text.replaceWithLiteral('<', "&lt;");
     text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
         "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
         "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"

Modified: trunk/Source/WebKit/blackberry/ChangeLog (133730 => 133731)


--- trunk/Source/WebKit/blackberry/ChangeLog	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebKit/blackberry/ChangeLog	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1,3 +1,16 @@
+2012-11-07  Christophe Dumez  <[email protected]>
+
+        Add replaceWithLiteral() method to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=101257
+
+        Reviewed by Benjamin Poulain.
+
+        Substitute String::replace() calls by String::replaceWithLiteral() where
+        adequate, for efficiency.
+
+        * WebCoreSupport/SelectPopupClient.cpp:
+        (WebCore::SelectPopupClient::generateHTML):
+
 2012-11-07  Kenneth Rohde Christiansen  <[email protected]>
 
         Remove support for "desktop-width" in the viewport meta tag

Modified: trunk/Source/WebKit/blackberry/WebCoreSupport/SelectPopupClient.cpp (133730 => 133731)


--- trunk/Source/WebKit/blackberry/WebCoreSupport/SelectPopupClient.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebKit/blackberry/WebCoreSupport/SelectPopupClient.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -84,7 +84,7 @@
     // Add labels.
     source.append('[');
     for (int i = 0; i < size; i++) {
-        source.append("'" + String(labels[i]).replace('\\', "\\\\").replace('\'', "\\'") + "'");
+        source.append("'" + String(labels[i]).replaceWithLiteral('\\', "\\\\").replaceWithLiteral('\'', "\\'") + "'");
         // Don't append ',' to last element.
         if (i != size - 1)
             source.appendLiteral(", ");

Modified: trunk/Source/WebKit2/ChangeLog (133730 => 133731)


--- trunk/Source/WebKit2/ChangeLog	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebKit2/ChangeLog	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1,3 +1,16 @@
+2012-11-07  Christophe Dumez  <[email protected]>
+
+        Add replaceWithLiteral() method to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=101257
+
+        Reviewed by Benjamin Poulain.
+
+        Substitute String::replace() calls by String::replaceWithLiteral() where
+        adequate, for efficiency.
+
+        * WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp:
+        (WebKit::makeURLString):
+
 2012-11-06  Vivek Galatage  <[email protected]>
 
         Move DrawingAreaImpl methods graphicsLayerFactory, setRootCompositingLayer & scheduleCompositingLayerSync under ACCELERATED_COMPOSITING

Modified: trunk/Source/WebKit2/WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp (133730 => 133731)


--- trunk/Source/WebKit2/WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Source/WebKit2/WebProcess/Plugins/Netscape/NetscapeBrowserFuncs.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -273,8 +273,8 @@
     String urlString(url);
     
     // Strip return characters.
-    urlString.replace('\r', "");
-    urlString.replace('\n', "");
+    urlString.replaceWithLiteral('\r', "");
+    urlString.replaceWithLiteral('\n', "");
 
     return urlString;
 }

Modified: trunk/Tools/ChangeLog (133730 => 133731)


--- trunk/Tools/ChangeLog	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Tools/ChangeLog	2012-11-07 09:43:05 UTC (rev 133731)
@@ -1,3 +1,20 @@
+2012-11-07  Christophe Dumez  <[email protected]>
+
+        Add replaceWithLiteral() method to WTF::String
+        https://bugs.webkit.org/show_bug.cgi?id=101257
+
+        Reviewed by Benjamin Poulain.
+
+        Add API tests for String::replaceWithLiteral() and corresponding
+        StringImpl methods.
+
+        * TestWebKitAPI/CMakeLists.txt: Add WTFString API tests to CMake.
+        * TestWebKitAPI/Tests/WTF/StringImpl.cpp:
+        (TestWebKitAPI::TEST):
+        (TestWebKitAPI):
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::TEST):
+
 2012-11-07  Oswald Buddenhagen <[email protected]>
 
         [Qt] Fix build of modules depending on QtWebKit when using prefix

Modified: trunk/Tools/TestWebKitAPI/CMakeLists.txt (133730 => 133731)


--- trunk/Tools/TestWebKitAPI/CMakeLists.txt	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Tools/TestWebKitAPI/CMakeLists.txt	2012-11-07 09:43:05 UTC (rev 133731)
@@ -84,6 +84,7 @@
     ${TESTWEBKITAPI_DIR}/Tests/WTF/Vector.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/VectorBasic.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/VectorReverse.cpp
+    ${TESTWEBKITAPI_DIR}/Tests/WTF/WTFString.cpp
 )
 
 TARGET_LINK_LIBRARIES(test_wtf ${test_wtf_LIBRARIES})

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/StringImpl.cpp (133730 => 133731)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/StringImpl.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/StringImpl.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -26,6 +26,7 @@
 #include "config.h"
 
 #include <wtf/text/StringImpl.h>
+#include <wtf/text/WTFString.h>
 
 namespace TestWebKitAPI {
 
@@ -68,4 +69,48 @@
     }
 }
 
+TEST(WTF, StringImplReplaceWithLiteral)
+{
+    RefPtr<StringImpl> testStringImpl = StringImpl::createFromLiteral("1224");
+    ASSERT_TRUE(testStringImpl->is8Bit());
+
+    // Cases for 8Bit source.
+    testStringImpl = testStringImpl->replace('2', "", 0);
+    ASSERT_TRUE(equal(testStringImpl.get(), "14"));
+
+    testStringImpl = StringImpl::createFromLiteral("1224");
+    ASSERT_TRUE(testStringImpl->is8Bit());
+
+    testStringImpl = testStringImpl->replace('3', "NotFound", 8);
+    ASSERT_TRUE(equal(testStringImpl.get(), "1224"));
+
+    testStringImpl = testStringImpl->replace('2', "3", 1);
+    ASSERT_TRUE(equal(testStringImpl.get(), "1334"));
+
+    testStringImpl = StringImpl::createFromLiteral("1224");
+    ASSERT_TRUE(testStringImpl->is8Bit());
+    testStringImpl = testStringImpl->replace('2', "555", 3);
+    ASSERT_TRUE(equal(testStringImpl.get(), "15555554"));
+
+    // Cases for 16Bit source.
+    String testString = String::fromUTF8("résumé");
+    ASSERT_FALSE(testString.impl()->is8Bit());
+
+    testStringImpl = testString.impl()->replace('2', "NotFound", 8);
+    ASSERT_TRUE(equal(testStringImpl.get(), String::fromUTF8("résumé").impl()));
+
+    testStringImpl = testString.impl()->replace(UChar(0x00E9 /*U+00E9 is 'é'*/), "e", 1);
+    ASSERT_TRUE(equal(testStringImpl.get(), "resume"));
+
+    testString = String::fromUTF8("résumé");
+    ASSERT_FALSE(testString.impl()->is8Bit());
+    testStringImpl = testString.impl()->replace(UChar(0x00E9 /*U+00E9 is 'é'*/), "", 0);
+    ASSERT_TRUE(equal(testStringImpl.get(), "rsum"));
+
+    testString = String::fromUTF8("résumé");
+    ASSERT_FALSE(testString.impl()->is8Bit());
+    testStringImpl = testString.impl()->replace(UChar(0x00E9 /*U+00E9 is 'é'*/), "555", 3);
+    ASSERT_TRUE(equal(testStringImpl.get(), "r555sum555"));
+}
+
 } // namespace TestWebKitAPI

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp (133730 => 133731)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2012-11-07 09:32:33 UTC (rev 133730)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2012-11-07 09:43:05 UTC (rev 133731)
@@ -115,7 +115,45 @@
     testNumberToStringECMAScript(phi, "1.618033988749895");
 }
 
+TEST(WTF, StringReplaceWithLiteral)
+{
+    // Cases for 8Bit source.
+    String testString = "1224";
+    ASSERT_TRUE(testString.is8Bit());
+    testString.replaceWithLiteral('2', "");
+    ASSERT_STREQ("14", testString.utf8().data());
 
+    testString = "1224";
+    ASSERT_TRUE(testString.is8Bit());
+    testString.replaceWithLiteral('2', "3");
+    ASSERT_STREQ("1334", testString.utf8().data());
 
+    testString = "1224";
+    ASSERT_TRUE(testString.is8Bit());
+    testString.replaceWithLiteral('2', "555");
+    ASSERT_STREQ("15555554", testString.utf8().data());
 
+    testString = "1224";
+    ASSERT_TRUE(testString.is8Bit());
+    testString.replaceWithLiteral('3', "NotFound");
+    ASSERT_STREQ("1224", testString.utf8().data());
+
+    // Cases for 16Bit source.
+    testString = String::fromUTF8("résumé");
+    ASSERT_FALSE(testString.is8Bit());
+    testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is 'é'*/), "e");
+    ASSERT_STREQ("resume", testString.utf8().data());
+
+    testString = String::fromUTF8("résumé");
+    ASSERT_FALSE(testString.is8Bit());
+    testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is 'é'*/), "");
+    ASSERT_STREQ("rsum", testString.utf8().data());
+
+    testString = String::fromUTF8("résumé");
+    ASSERT_FALSE(testString.is8Bit());
+    testString.replaceWithLiteral('3', "NotFound");
+    ASSERT_STREQ("résumé", testString.utf8().data());
+}
+
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to