Title: [293379] trunk/Source
Revision
293379
Author
cdu...@apple.com
Date
2022-04-25 17:12:01 -0700 (Mon, 25 Apr 2022)

Log Message

Introduce new makeStringByReplacingAll(StringView, UChar, UChar) function
https://bugs.webkit.org/show_bug.cgi?id=239728

Reviewed by Darin Adler.

Introduce new makeStringByReplacingAll(StringView, UChar, UChar) function, to complement
the existing makeStringByReplacingAll(const String&, UChar, UChar). This allows several
call sites to avoid a temporary String constructing since they have a StringView.

* Source/WebCore/PAL/pal/text/TextCodecICU.cpp:
(PAL::TextCodecICU::encode const):
* Source/WTF/wtf/URLParser.cpp:
(WTF::URLParser::parseURLEncodedForm):
* Source/WTF/wtf/text/StringImpl.cpp:
(WTF::StringImpl::replace):
* Source/WTF/wtf/text/StringImpl.h:
(WTF::StringImpl::createByReplacingInCharacters):
* Source/WTF/wtf/text/StringView.cpp:
(WTF::makeStringByReplacingAll):
* Source/WTF/wtf/text/StringView.h:
* Source/WebCore/Modules/plugins/YouTubePluginReplacement.cpp:
(WebCore::queryKeysAndValues):
* Source/WebCore/editing/cocoa/HTMLConverter.mm:
(WebCore::editingAttributedString):

Canonical link: https://commits.webkit.org/249969@main

Modified Paths

Diff

Modified: trunk/Source/WTF/wtf/URLParser.cpp (293378 => 293379)


--- trunk/Source/WTF/wtf/URLParser.cpp	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WTF/wtf/URLParser.cpp	2022-04-26 00:12:01 UTC (rev 293379)
@@ -2887,12 +2887,12 @@
     for (StringView bytes : input.split('&')) {
         auto equalIndex = bytes.find('=');
         if (equalIndex == notFound) {
-            auto name = formURLDecode(makeStringByReplacingAll(bytes.toString(), '+', 0x20));
+            auto name = formURLDecode(makeStringByReplacingAll(bytes, '+', 0x20));
             if (name)
                 output.append({ name.value(), emptyString() });
         } else {
-            auto name = formURLDecode(makeStringByReplacingAll(bytes.left(equalIndex).toString(), '+', 0x20));
-            auto value = formURLDecode(makeStringByReplacingAll(bytes.substring(equalIndex + 1).toString(), '+', 0x20));
+            auto name = formURLDecode(makeStringByReplacingAll(bytes.left(equalIndex), '+', 0x20));
+            auto value = formURLDecode(makeStringByReplacingAll(bytes.substring(equalIndex + 1), '+', 0x20));
             if (name && value)
                 output.append({ name.value(), value.value() });
         }

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (293378 => 293379)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2022-04-26 00:12:01 UTC (rev 293379)
@@ -200,6 +200,9 @@
     return constructInternal<CharacterType>(*string, length);
 }
 
+template Ref<StringImpl> StringImpl::createUninitializedInternalNonEmpty(unsigned length, LChar*& data);
+template Ref<StringImpl> StringImpl::createUninitializedInternalNonEmpty(unsigned length, UChar*& data);
+
 Ref<StringImpl> StringImpl::createUninitialized(unsigned length, LChar*& data)
 {
     return createUninitializedInternal(length, data);
@@ -1092,14 +1095,6 @@
 {
     if (target == replacement)
         return *this;
-    unsigned i;
-    for (i = 0; i != m_length; ++i) {
-        UChar character = is8Bit() ? m_data8[i] : m_data16[i];
-        if (character == target)
-            break;
-    }
-    if (i == m_length)
-        return *this;
 
     if (is8Bit()) {
         if (!isLatin1(target)) {
@@ -1106,47 +1101,24 @@
             // Looking for a 16-bit character in an 8-bit string, so we're done.
             return *this;
         }
-
-        if (isLatin1(replacement)) {
-            LChar* data;
-            LChar oldChar = static_cast<LChar>(target);
-            LChar newChar = static_cast<LChar>(replacement);
-
-            auto newImpl = createUninitializedInternalNonEmpty(m_length, data);
-
-            for (i = 0; i != m_length; ++i) {
-                LChar character = m_data8[i];
-                if (character == oldChar)
-                    character = newChar;
-                data[i] = character;
-            }
-            return newImpl;
-        }
-
-        UChar* data;
-        auto newImpl = createUninitializedInternalNonEmpty(m_length, data);
-
+        unsigned i;
         for (i = 0; i != m_length; ++i) {
-            UChar character = m_data8[i];
-            if (character == target)
-                character = replacement;
-            data[i] = character;
+            if (static_cast<UChar>(m_data8[i]) == target)
+                break;
         }
-
-        return newImpl;
+        if (i == m_length)
+            return *this;
+        return createByReplacingInCharacters(m_data8, m_length, target, replacement, i);
     }
 
-    UChar* data;
-    auto newImpl = createUninitializedInternalNonEmpty(m_length, data);
-
-    copyCharacters(data, m_data16, i);
-    for (unsigned j = i; j != m_length; ++j) {
-        UChar character = m_data16[j];
-        if (character == target)
-            character = replacement;
-        data[j] = character;
+    unsigned i;
+    for (i = 0; i != m_length; ++i) {
+        if (m_data16[i] == target)
+            break;
     }
-    return newImpl;
+    if (i == m_length)
+        return *this;
+    return createByReplacingInCharacters(m_data16, m_length, target, replacement, i);
 }
 
 Ref<StringImpl> StringImpl::replace(unsigned position, unsigned lengthToReplace, StringView string)

Modified: trunk/Source/WTF/wtf/text/StringImpl.h (293378 => 293379)


--- trunk/Source/WTF/wtf/text/StringImpl.h	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WTF/wtf/text/StringImpl.h	2022-04-26 00:12:01 UTC (rev 293379)
@@ -259,6 +259,9 @@
     WTF_EXPORT_PRIVATE static Ref<StringImpl> createUninitialized(unsigned length, UChar*&);
     template<typename CharacterType> static RefPtr<StringImpl> tryCreateUninitialized(unsigned length, CharacterType*&);
 
+    static Ref<StringImpl> createByReplacingInCharacters(const LChar*, unsigned length, UChar target, UChar replacement, unsigned indexOfFirstTargetCharacter);
+    static Ref<StringImpl> createByReplacingInCharacters(const UChar*, unsigned length, UChar target, UChar replacement, unsigned indexOfFirstTargetCharacter);
+
     static Ref<StringImpl> createStaticStringImpl(const char* characters, unsigned length)
     {
         ASSERT(charactersAreAllASCII(bitwise_cast<const LChar*>(characters), length));
@@ -1324,6 +1327,44 @@
     return removeCharactersImpl(characters16(), findMatch);
 }
 
+inline Ref<StringImpl> StringImpl::createByReplacingInCharacters(const LChar* characters, unsigned length, UChar target, UChar replacement, unsigned indexOfFirstTargetCharacter)
+{
+    ASSERT(indexOfFirstTargetCharacter < length);
+    if (isLatin1(replacement)) {
+        LChar* data;
+        LChar oldChar = target;
+        LChar newChar = replacement;
+        auto newImpl = createUninitializedInternalNonEmpty(length, data);
+        memcpy(data, characters, indexOfFirstTargetCharacter);
+        for (unsigned i = indexOfFirstTargetCharacter; i != length; ++i) {
+            LChar character = characters[i];
+            data[i] = character == oldChar ? newChar : character;
+        }
+        return newImpl;
+    }
+
+    UChar* data;
+    auto newImpl = createUninitializedInternalNonEmpty(length, data);
+    for (unsigned i = 0; i != length; ++i) {
+        UChar character = characters[i];
+        data[i] = character == target ? replacement : character;
+    }
+    return newImpl;
+}
+
+inline Ref<StringImpl> StringImpl::createByReplacingInCharacters(const UChar* characters, unsigned length, UChar target, UChar replacement, unsigned indexOfFirstTargetCharacter)
+{
+    ASSERT(indexOfFirstTargetCharacter < length);
+    UChar* data;
+    auto newImpl = createUninitializedInternalNonEmpty(length, data);
+    copyCharacters(data, characters, indexOfFirstTargetCharacter);
+    for (unsigned i = indexOfFirstTargetCharacter; i != length; ++i) {
+        UChar character = characters[i];
+        data[i] = character == target ? replacement : character;
+    }
+    return newImpl;
+}
+
 } // namespace WTF
 
 using WTF::StaticStringImpl;

Modified: trunk/Source/WTF/wtf/text/StringView.cpp (293378 => 293379)


--- trunk/Source/WTF/wtf/text/StringView.cpp	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WTF/wtf/text/StringView.cpp	2022-04-26 00:12:01 UTC (rev 293379)
@@ -386,6 +386,38 @@
     return reverseFindInner(characters16(), matchString.characters16(), start, ourLength, matchLength);
 }
 
+String makeStringByReplacingAll(StringView string, UChar target, UChar replacement)
+{
+    if (string.is8Bit()) {
+        if (!isLatin1(target)) {
+            // Looking for a 16-bit character in an 8-bit string, so we're done.
+            return string.toString();
+        }
+
+        auto* characters = string.characters8();
+        unsigned i;
+        unsigned length = string.length();
+        for (i = 0; i != length; ++i) {
+            if (characters[i] == target)
+                break;
+        }
+        if (i == length)
+            return string.toString();
+        return StringImpl::createByReplacingInCharacters(characters, length, target, replacement, i);
+    }
+
+    auto* characters = string.characters16();
+    unsigned i;
+    unsigned length = string.length();
+    for (i = 0; i != length; ++i) {
+        if (characters[i] == target)
+            break;
+    }
+    if (i == length)
+        return string.toString();
+    return StringImpl::createByReplacingInCharacters(characters, length, target, replacement, i);
+}
+
 int codePointCompare(StringView lhs, StringView rhs)
 {
     bool lhsIs8Bit = lhs.is8Bit();

Modified: trunk/Source/WTF/wtf/text/StringView.h (293378 => 293379)


--- trunk/Source/WTF/wtf/text/StringView.h	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WTF/wtf/text/StringView.h	2022-04-26 00:12:01 UTC (rev 293379)
@@ -1329,6 +1329,8 @@
     return string;
 }
 
+WTF_EXPORT_PRIVATE String WARN_UNUSED_RETURN makeStringByReplacingAll(StringView, UChar target, UChar replacement);
+
 inline String WARN_UNUSED_RETURN makeStringBySimplifyingNewLines(const String& string)
 {
     return makeStringByReplacingAll(makeStringByReplacingAll(string, "\r\n"_s, "\n"_s), '\r', '\n');

Modified: trunk/Source/WebCore/Modules/plugins/YouTubePluginReplacement.cpp (293378 => 293379)


--- trunk/Source/WebCore/Modules/plugins/YouTubePluginReplacement.cpp	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WebCore/Modules/plugins/YouTubePluginReplacement.cpp	2022-04-26 00:12:01 UTC (rev 293379)
@@ -148,8 +148,7 @@
         // Save the key and the value.
         if (keyLength && valueLength) {
             String key = queryString.substring(keyLocation, keyLength).convertToASCIILowercase();
-            String value = queryString.substring(valueLocation, valueLength).toString();
-            value = makeStringByReplacingAll(value, '+', ' ');
+            auto value = makeStringByReplacingAll(queryString.substring(valueLocation, valueLength), '+', ' ');
 
             if (!key.isEmpty() && !value.isEmpty())
                 queryDictionary.add(key, value);

Modified: trunk/Source/WebCore/PAL/pal/text/TextCodecICU.cpp (293378 => 293379)


--- trunk/Source/WebCore/PAL/pal/text/TextCodecICU.cpp	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WebCore/PAL/pal/text/TextCodecICU.cpp	2022-04-26 00:12:01 UTC (rev 293379)
@@ -302,7 +302,7 @@
     // Encoding will change the yen sign back into a backslash.
     String copy;
     if (shouldShowBackslashAsCurrencySymbolIn(m_encodingName)) {
-        copy = makeStringByReplacingAll(string.toStringWithoutCopying(), '\\', yenSign);
+        copy = makeStringByReplacingAll(string, '\\', yenSign);
         string = copy;
     }
 

Modified: trunk/Source/WebCore/editing/cocoa/HTMLConverter.mm (293378 => 293379)


--- trunk/Source/WebCore/editing/cocoa/HTMLConverter.mm	2022-04-26 00:03:04 UTC (rev 293378)
+++ trunk/Source/WebCore/editing/cocoa/HTMLConverter.mm	2022-04-26 00:12:01 UTC (rev 293379)
@@ -2442,7 +2442,7 @@
         if (style.nbspMode() == NBSPMode::Normal)
             text = it.text().createNSStringWithoutCopying();
         else
-            text = makeStringByReplacingAll(it.text().toString(), noBreakSpace, ' ');
+            text = makeStringByReplacingAll(it.text(), noBreakSpace, ' ');
 
         [string replaceCharactersInRange:NSMakeRange(stringLength, 0) withString:text.get()];
         [string setAttributes:attrs.get() range:NSMakeRange(stringLength, currentTextLength)];
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to