Title: [174234] trunk/Source/WTF
Revision
174234
Author
[email protected]
Date
2014-10-02 15:48:48 -0700 (Thu, 02 Oct 2014)

Log Message

Simplify StringTypeAdapter templates
https://bugs.webkit.org/show_bug.cgi?id=137356

Reviewed by Andreas Kling.

* wtf/text/StringConcatenate.h:
Use StringView for copying characters. Use inheritance for char* vs const char* etc. Make all StringAdapter member functions const.

* wtf/text/StringView.h:
Move enough functions out of line so we can include WTFString.h after the StringView class definition.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (174233 => 174234)


--- trunk/Source/WTF/ChangeLog	2014-10-02 22:25:50 UTC (rev 174233)
+++ trunk/Source/WTF/ChangeLog	2014-10-02 22:48:48 UTC (rev 174234)
@@ -1,3 +1,16 @@
+2014-10-02  Anders Carlsson  <[email protected]>
+
+        Simplify StringTypeAdapter templates
+        https://bugs.webkit.org/show_bug.cgi?id=137356
+
+        Reviewed by Andreas Kling.
+
+        * wtf/text/StringConcatenate.h:
+        Use StringView for copying characters. Use inheritance for char* vs const char* etc. Make all StringAdapter member functions const.
+
+        * wtf/text/StringView.h:
+        Move enough functions out of line so we can include WTFString.h after the StringView class definition.
+
 2014-10-01  Christophe Dumez  <[email protected]>
 
         Have is<>(T*) function do a null check on the pointer argument

Modified: trunk/Source/WTF/wtf/text/StringConcatenate.h (174233 => 174234)


--- trunk/Source/WTF/wtf/text/StringConcatenate.h	2014-10-02 22:25:50 UTC (rev 174233)
+++ trunk/Source/WTF/wtf/text/StringConcatenate.h	2014-10-02 22:48:48 UTC (rev 174234)
@@ -32,6 +32,10 @@
 #include <wtf/text/AtomicString.h>
 #endif
 
+#ifndef StringView_h
+#include <wtf/text/StringView.h>
+#endif
+
 // This macro is helpful for testing how many intermediate Strings are created while evaluating an
 // _expression_ containing operator+.
 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING
@@ -41,379 +45,204 @@
 namespace WTF {
 
 template<typename StringType>
-class StringTypeAdapter {
-};
+class StringTypeAdapter;
 
 template<>
 class StringTypeAdapter<char> {
 public:
-    StringTypeAdapter<char>(char buffer)
-        : m_buffer(buffer)
+    StringTypeAdapter<char>(char character)
+        : m_character(character)
     {
     }
 
     unsigned length() { return 1; }
-
     bool is8Bit() { return true; }
 
-    void writeTo(LChar* destination)
+    void writeTo(LChar* destination) const
     {
-        *destination = m_buffer;
+        *destination = m_character;
     }
 
-    void writeTo(UChar* destination) { *destination = m_buffer; }
-
-private:
-    unsigned char m_buffer;
-};
-
-template<>
-class StringTypeAdapter<LChar> {
-public:
-    StringTypeAdapter<LChar>(LChar buffer)
-        : m_buffer(buffer)
+    void writeTo(UChar* destination) const
     {
+        *destination = m_character;
     }
 
-    unsigned length() { return 1; }
-
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
-    {
-        *destination = m_buffer;
-    }
-
-    void writeTo(UChar* destination) { *destination = m_buffer; }
-
 private:
-    LChar m_buffer;
+    char m_character;
 };
 
 template<>
 class StringTypeAdapter<UChar> {
 public:
-    StringTypeAdapter<UChar>(UChar buffer)
-        : m_buffer(buffer)
+    StringTypeAdapter<UChar>(UChar character)
+        : m_character(character)
     {
     }
 
-    unsigned length() { return 1; }
+    unsigned length() const { return 1; }
+    bool is8Bit() const { return m_character <= 0xff; }
 
-    bool is8Bit() { return m_buffer <= 0xff; }
-
-    void writeTo(LChar* destination)
+    void writeTo(LChar* destination) const
     {
         ASSERT(is8Bit());
-        *destination = static_cast<LChar>(m_buffer);
+        *destination = static_cast<LChar>(m_character);
     }
 
-    void writeTo(UChar* destination) { *destination = m_buffer; }
-
-private:
-    UChar m_buffer;
-};
-
-template<>
-class StringTypeAdapter<char*> {
-public:
-    StringTypeAdapter<char*>(char* buffer)
-        : m_buffer(buffer)
-        , m_length(strlen(buffer))
+    void writeTo(UChar* destination) const
     {
+        *destination = m_character;
     }
 
-    unsigned length() { return m_length; }
-
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
-    {
-        for (unsigned i = 0; i < m_length; ++i)
-            destination[i] = static_cast<LChar>(m_buffer[i]);
-    }
-
-    void writeTo(UChar* destination)
-    {
-        for (unsigned i = 0; i < m_length; ++i) {
-            unsigned char c = m_buffer[i];
-            destination[i] = c;
-        }
-    }
-
 private:
-    const char* m_buffer;
-    unsigned m_length;
+    UChar m_character;
 };
 
 template<>
-class StringTypeAdapter<LChar*> {
+class StringTypeAdapter<const LChar*> {
 public:
-    StringTypeAdapter<LChar*>(LChar* buffer)
-    : m_buffer(buffer)
-    , m_length(strlen(reinterpret_cast<char*>(buffer)))
+    StringTypeAdapter(const LChar* characters)
+        : m_characters(characters)
+        , m_length(strlen(reinterpret_cast<const char*>(characters)))
     {
     }
 
-    unsigned length() { return m_length; }
+    unsigned length() const { return m_length; }
+    bool is8Bit() const { return true; }
 
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
+    void writeTo(LChar* destination) const
     {
-        memcpy(destination, m_buffer, m_length * sizeof(LChar));
+        StringView(m_characters, m_length).getCharactersWithUpconvert(destination);
     }
 
-    void writeTo(UChar* destination)
+    void writeTo(UChar* destination) const
     {
-        StringImpl::copyChars(destination, m_buffer, m_length);
+        StringView(m_characters, m_length).getCharactersWithUpconvert(destination);
     }
 
 private:
-    const LChar* m_buffer;
+    const LChar* m_characters;
     unsigned m_length;
 };
 
 template<>
 class StringTypeAdapter<const UChar*> {
 public:
-    StringTypeAdapter<const UChar*>(const UChar* buffer)
-        : m_buffer(buffer)
+    StringTypeAdapter(const UChar* characters)
+        : m_characters(characters)
     {
-        size_t len = 0;
-        while (m_buffer[len] != UChar(0))
-            ++len;
+        unsigned length = 0;
+        while (m_characters[length])
+            ++length;
 
-        if (len > std::numeric_limits<unsigned>::max())
+        if (length > std::numeric_limits<unsigned>::max())
             CRASH();
 
-        m_length = len;
+        m_length = length;
     }
 
-    unsigned length() { return m_length; }
+    unsigned length() const { return m_length; }
+    bool is8Bit() const { return false; }
 
-    bool is8Bit() { return false; }
-
-    NO_RETURN_DUE_TO_CRASH void writeTo(LChar*)
+    NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) const
     {
         CRASH();
     }
 
-    void writeTo(UChar* destination)
+    void writeTo(UChar* destination) const
     {
-        memcpy(destination, m_buffer, m_length * sizeof(UChar));
+        memcpy(destination, m_characters, m_length * sizeof(UChar));
     }
 
 private:
-    const UChar* m_buffer;
+    const UChar* m_characters;
     unsigned m_length;
 };
 
 template<>
-class StringTypeAdapter<const char*> {
+class StringTypeAdapter<const char*> : public StringTypeAdapter<const LChar*> {
 public:
-    StringTypeAdapter<const char*>(const char* buffer)
-        : m_buffer(buffer)
-        , m_length(strlen(buffer))
+    StringTypeAdapter(const char* characters)
+        : StringTypeAdapter<const LChar*>(reinterpret_cast<const LChar*>(characters))
     {
     }
-
-    unsigned length() { return m_length; }
-
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
-    {
-        memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar));
-    }
-
-    void writeTo(UChar* destination)
-    {
-        for (unsigned i = 0; i < m_length; ++i) {
-            unsigned char c = m_buffer[i];
-            destination[i] = c;
-        }
-    }
-
-private:
-    const char* m_buffer;
-    unsigned m_length;
 };
 
 template<>
-class StringTypeAdapter<const LChar*> {
+class StringTypeAdapter<char*> : public StringTypeAdapter<const char*> {
 public:
-    StringTypeAdapter<const LChar*>(const LChar* buffer)
-        : m_buffer(buffer)
-        , m_length(strlen(reinterpret_cast<const char*>(buffer)))
+    StringTypeAdapter(const char* characters)
+        : StringTypeAdapter<const char*>(characters)
     {
     }
-
-    unsigned length() { return m_length; }
-
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
-    {
-        memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar));
-    }
-
-    void writeTo(UChar* destination)
-    {
-        StringImpl::copyChars(destination, m_buffer, m_length);
-    }
-
-private:
-    const LChar* m_buffer;
-    unsigned m_length;
 };
 
 template<>
-class StringTypeAdapter<ASCIILiteral> {
+class StringTypeAdapter<ASCIILiteral> : public StringTypeAdapter<const char*> {
 public:
-    StringTypeAdapter<ASCIILiteral>(ASCIILiteral buffer)
-        : m_buffer(reinterpret_cast<const LChar*>(static_cast<const char*>(buffer)))
-        , m_length(strlen(buffer))
+    StringTypeAdapter(ASCIILiteral characters)
+        : StringTypeAdapter<const char*>(characters)
     {
     }
-
-    size_t length() { return m_length; }
-
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
-    {
-        memcpy(destination, m_buffer, static_cast<size_t>(m_length));
-    }
-
-    void writeTo(UChar* destination)
-    {
-        StringImpl::copyChars(destination, m_buffer, m_length);
-    }
-
-private:
-    const LChar* m_buffer;
-    unsigned m_length;
 };
 
 template<>
 class StringTypeAdapter<Vector<char>> {
 public:
-    StringTypeAdapter<Vector<char>>(const Vector<char>& buffer)
-        : m_buffer(buffer)
+    StringTypeAdapter(const Vector<char>& vector)
+        : m_vector(vector)
     {
     }
 
-    size_t length() { return m_buffer.size(); }
+    size_t length() const { return m_vector.size(); }
+    bool is8Bit() const { return true; }
 
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
+    void writeTo(LChar* destination) const
     {
-        for (size_t i = 0; i < m_buffer.size(); ++i)
-            destination[i] = static_cast<unsigned char>(m_buffer[i]);
+        StringView(reinterpret_cast<const LChar*>(m_vector.data()), m_vector.size()).getCharactersWithUpconvert(destination);
     }
 
-    void writeTo(UChar* destination)
+    void writeTo(UChar* destination) const
     {
-        for (size_t i = 0; i < m_buffer.size(); ++i)
-            destination[i] = static_cast<unsigned char>(m_buffer[i]);
+        StringView(reinterpret_cast<const LChar*>(m_vector.data()), m_vector.size()).getCharactersWithUpconvert(destination);
     }
 
 private:
-    const Vector<char>& m_buffer;
+    const Vector<char>& m_vector;
 };
 
 template<>
-class StringTypeAdapter<Vector<LChar>> {
-public:
-    StringTypeAdapter<Vector<LChar>>(const Vector<LChar>& buffer)
-        : m_buffer(buffer)
-    {
-    }
-
-    size_t length() { return m_buffer.size(); }
-
-    bool is8Bit() { return true; }
-
-    void writeTo(LChar* destination)
-    {
-        for (size_t i = 0; i < m_buffer.size(); ++i)
-            destination[i] = m_buffer[i];
-    }
-
-    void writeTo(UChar* destination)
-    {
-        for (size_t i = 0; i < m_buffer.size(); ++i)
-            destination[i] = m_buffer[i];
-    }
-
-private:
-    const Vector<LChar>& m_buffer;
-};
-
-template<>
 class StringTypeAdapter<String> {
 public:
     StringTypeAdapter<String>(const String& string)
-        : m_buffer(string)
+        : m_string(string)
     {
     }
 
-    unsigned length() { return m_buffer.length(); }
+    unsigned length() const { return m_string.length(); }
+    bool is8Bit() const { return m_string.isNull() || m_string.is8Bit(); }
 
-    bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); }
-
-    void writeTo(LChar* destination)
+    void writeTo(LChar* destination) const
     {
-        unsigned length = m_buffer.length();
-
-        ASSERT(is8Bit());
-        const LChar* data = ""
-        for (unsigned i = 0; i < length; ++i)
-            destination[i] = data[i];
-        
-        WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
+        StringView(m_string).getCharactersWithUpconvert(destination);
     }
 
-    void writeTo(UChar* destination)
+    void writeTo(UChar* destination) const
     {
-        unsigned length = m_buffer.length();
-
-        if (is8Bit()) {
-            const LChar* data = ""
-            for (unsigned i = 0; i < length; ++i)
-                destination[i] = data[i];
-        } else {
-            const UChar* data = ""
-            for (unsigned i = 0; i < length; ++i)
-                destination[i] = data[i];
-        }
-        
-        WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
+        StringView(m_string).getCharactersWithUpconvert(destination);
     }
 
 private:
-    const String& m_buffer;
+    const String& m_string;
 };
 
 template<>
-class StringTypeAdapter<AtomicString> {
+class StringTypeAdapter<AtomicString> : public StringTypeAdapter<String> {
 public:
-    StringTypeAdapter<AtomicString>(const AtomicString& string)
-        : m_adapter(string.string())
+    StringTypeAdapter(const AtomicString& string)
+        : StringTypeAdapter<String>(string.string())
     {
     }
-
-    unsigned length() { return m_adapter.length(); }
-
-    bool is8Bit() { return m_adapter.is8Bit(); }
-
-    void writeTo(LChar* destination) { m_adapter.writeTo(destination); }
-    void writeTo(UChar* destination) { m_adapter.writeTo(destination); }
-
-private:
-    StringTypeAdapter<String> m_adapter;
 };
 
 inline void sumWithOverflow(unsigned& total, unsigned addend, bool& overflow)

Modified: trunk/Source/WTF/wtf/text/StringView.h (174233 => 174234)


--- trunk/Source/WTF/wtf/text/StringView.h	2014-10-02 22:25:50 UTC (rev 174233)
+++ trunk/Source/WTF/wtf/text/StringView.h	2014-10-02 22:48:48 UTC (rev 174234)
@@ -26,7 +26,11 @@
 #ifndef StringView_h
 #define StringView_h
 
-#include <wtf/text/StringConcatenate.h>
+#include <unicode/utypes.h>
+#include <wtf/Forward.h>
+#include <wtf/RetainPtr.h>
+#include <wtf/Vector.h>
+#include <wtf/text/LChar.h>
 
 namespace WTF {
 
@@ -53,28 +57,9 @@
         initialize(characters, length);
     }
 
-    StringView(const StringImpl& string)
-    {
-        if (string.is8Bit())
-            initialize(string.characters8(), string.length());
-        else
-            initialize(string.characters16(), string.length());
-    }
+    StringView(const StringImpl&);
+    StringView(const String&);
 
-    StringView(const String& string)
-    {
-        if (!string.impl()) {
-            m_characters = nullptr;
-            m_length = 0;
-            return;
-        }
-        if (string.is8Bit()) {
-            initialize(string.characters8(), string.length());
-            return;
-        }
-        initialize(string.characters16(), string.length());
-    }
-
     static StringView empty()
     {
         return StringView(reinterpret_cast<const LChar*>(""), 0);
@@ -134,36 +119,13 @@
         return StringView(characters16() + start, length);
     }
 
-    String toString() const
-    {
-        if (is8Bit())
-            return String(characters8(), length());
+    String toString() const;
 
-        return String(characters16(), length());
-    }
+    float toFloat(bool& isValid) const;
+    int toInt(bool& isValid) const;
 
-    float toFloat(bool& isValid)
-    {
-        if (is8Bit())
-            return charactersToFloat(characters8(), length(), &isValid);
-        return charactersToFloat(characters16(), length(), &isValid);
-    }
+    String toStringWithoutCopying() const;
 
-    int toInt(bool& isValid)
-    {
-        if (is8Bit())
-            return charactersToInt(characters8(), length(), &isValid);
-        return charactersToInt(characters16(), length(), &isValid);
-    }
-
-    String toStringWithoutCopying() const
-    {
-        if (is8Bit())
-            return StringImpl::createWithoutCopying(characters8(), length());
-
-        return StringImpl::createWithoutCopying(characters16(), length());
-    }
-
     UChar operator[](unsigned index) const
     {
         ASSERT(index < length());
@@ -172,12 +134,7 @@
         return characters16()[index];
     }
 
-    size_t find(UChar character, unsigned start = 0) const
-    {
-        if (is8Bit())
-            return WTF::find(characters8(), length(), character, start);
-        return WTF::find(characters16(), length(), character, start);
-    }
+    size_t find(UChar character, unsigned start = 0) const;
 
     bool contains(UChar c) const { return find(c) != notFound; }
 
@@ -215,6 +172,34 @@
     unsigned m_length;
 };
 
+}
+
+#include <wtf/text/WTFString.h>
+
+namespace WTF {
+
+inline StringView::StringView(const StringImpl& string)
+{
+    if (string.is8Bit())
+        initialize(string.characters8(), string.length());
+    else
+        initialize(string.characters16(), string.length());
+}
+
+inline StringView::StringView(const String& string)
+{
+    if (!string.impl()) {
+        m_characters = nullptr;
+        m_length = 0;
+        return;
+    }
+    if (string.is8Bit()) {
+        initialize(string.characters8(), string.length());
+        return;
+    }
+    initialize(string.characters16(), string.length());
+}
+
 inline void StringView::getCharactersWithUpconvert(LChar* destination) const
 {
     ASSERT(is8Bit());
@@ -247,6 +232,45 @@
     m_characters = m_upconvertedCharacters.data();
 }
 
+inline String StringView::toString() const
+{
+    if (is8Bit())
+        return String(characters8(), length());
+
+    return String(characters16(), length());
+}
+
+inline float StringView::toFloat(bool& isValid) const
+{
+    if (is8Bit())
+        return charactersToFloat(characters8(), length(), &isValid);
+    return charactersToFloat(characters16(), length(), &isValid);
+}
+
+inline int StringView::toInt(bool& isValid) const
+{
+    if (is8Bit())
+        return charactersToInt(characters8(), length(), &isValid);
+    return charactersToInt(characters16(), length(), &isValid);
+}
+
+inline String StringView::toStringWithoutCopying() const
+{
+    if (is8Bit())
+        return StringImpl::createWithoutCopying(characters8(), length());
+
+    return StringImpl::createWithoutCopying(characters16(), length());
+}
+
+inline size_t StringView::find(UChar character, unsigned start) const
+{
+    if (is8Bit())
+        return WTF::find(characters8(), length(), character, start);
+    return WTF::find(characters16(), length(), character, start);
+}
+
+template<typename StringType> class StringTypeAdapter;
+
 template<> class StringTypeAdapter<StringView> {
 public:
     StringTypeAdapter<StringView>(StringView string)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to