2009-02-19 Darin Fisher <darin@chromium.org>
authordarin@chromium.org <darin@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Feb 2009 15:31:35 +0000 (15:31 +0000)
committerdarin@chromium.org <darin@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Feb 2009 15:31:35 +0000 (15:31 +0000)
        Reviewed by Eric Seidel.

        https://bugs.webkit.org/show_bug.cgi?id=24046

        Several improvements to CString:
        1- Make it possible to initialize a CString from a CStringBuffer
        2- Make it possible to get a CStringBuffer from a CString
        3- Change CStringBuffer::data() to return a const pointer to ward off mutation
        4- Remove unused releaseBuffer() methods.
        5- Make CStringBuffer::create() private to force consumers to get a CStringBuffer from a CString.

        * platform/text/CString.cpp:
        (WebCore::CString::init):
        (WebCore::CString::mutableData):
        (WebCore::CString::newUninitialized):
        (WebCore::CString::copyBufferIfNeeded):
        * platform/text/CString.h:
        (WebCore::CStringBuffer::data):
        (WebCore::CStringBuffer::length):
        (WebCore::CStringBuffer::create):
        (WebCore::CStringBuffer::mutableData):
        (WebCore::CString::CString):
        (WebCore::CString::buffer):

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

WebCore/ChangeLog
WebCore/platform/text/CString.cpp
WebCore/platform/text/CString.h

index 1946a4b..8e7f98e 100644 (file)
@@ -1,3 +1,29 @@
+2009-02-19  Darin Fisher  <darin@chromium.org>
+
+        Reviewed by Eric Seidel.
+
+        https://bugs.webkit.org/show_bug.cgi?id=24046
+
+        Several improvements to CString:
+        1- Make it possible to initialize a CString from a CStringBuffer
+        2- Make it possible to get a CStringBuffer from a CString
+        3- Change CStringBuffer::data() to return a const pointer to ward off mutation
+        4- Remove unused releaseBuffer() methods.
+        5- Make CStringBuffer::create() private to force consumers to get a CStringBuffer from a CString.
+
+        * platform/text/CString.cpp:
+        (WebCore::CString::init):
+        (WebCore::CString::mutableData):
+        (WebCore::CString::newUninitialized):
+        (WebCore::CString::copyBufferIfNeeded):
+        * platform/text/CString.h:
+        (WebCore::CStringBuffer::data):
+        (WebCore::CStringBuffer::length):
+        (WebCore::CStringBuffer::create):
+        (WebCore::CStringBuffer::mutableData):
+        (WebCore::CString::CString):
+        (WebCore::CString::buffer):
+
 2009-02-19  Dmitry Titov  <dimich@chromium.org>
 
         Reviewed by Alexey Proskuryakov.
index 8e68628..90990f8 100644 (file)
@@ -47,8 +47,8 @@ void CString::init(const char* str, unsigned length)
         return;
     
     m_buffer = CStringBuffer::create(length + 1);
-    memcpy(m_buffer->data(), str, length); 
-    m_buffer->data()[length] = '\0';
+    memcpy(m_buffer->mutableData(), str, length); 
+    m_buffer->mutableData()[length] = '\0';
 }
 
 const char* CString::data() const
@@ -61,7 +61,7 @@ char* CString::mutableData()
     copyBufferIfNeeded();
     if (!m_buffer)
         return 0;
-    return m_buffer->data();
+    return m_buffer->mutableData();
 }
     
 unsigned CString::length() const
@@ -73,7 +73,7 @@ CString CString::newUninitialized(size_t length, char*& characterBuffer)
 {
     CString result;
     result.m_buffer = CStringBuffer::create(length + 1);
-    char* bytes = result.m_buffer->data();
+    char* bytes = result.m_buffer->mutableData();
     bytes[length] = '\0';
     characterBuffer = bytes;
     return result;
@@ -87,7 +87,7 @@ void CString::copyBufferIfNeeded()
     int len = m_buffer->length();
     RefPtr<CStringBuffer> m_temp = m_buffer;
     m_buffer = CStringBuffer::create(len);
-    memcpy(m_buffer->data(), m_temp->data(), len);
+    memcpy(m_buffer->mutableData(), m_temp->data(), len);
 }
 
 bool operator==(const CString& a, const CString& b)
@@ -99,17 +99,4 @@ bool operator==(const CString& a, const CString& b)
     return !strncmp(a.data(), b.data(), min(a.length(), b.length()));
 }
 
-PassRefPtr<SharedBuffer> CString::releaseBuffer()
-{
-    if (!m_buffer)
-        return 0;
-
-    copyBufferIfNeeded();
-        
-    RefPtr<SharedBuffer> result = m_buffer->releaseBuffer();
-    m_buffer = 0;
-    return result.release();
-}
-
-
-}
+} // namespace WebCore
index 09f112f..f084ddf 100644 (file)
@@ -36,15 +36,15 @@ namespace WebCore {
 
     class CStringBuffer : public RefCounted<CStringBuffer> {
     public:
-        static PassRefPtr<CStringBuffer> create(unsigned length) { return adoptRef(new CStringBuffer(length)); }
-
-        char* data() { return m_vector.data(); }
-        size_t length() const { return m_vector.size(); }
+        const char* data() { return m_vector.data(); }
+        size_t length() { return m_vector.size(); }
         
-        PassRefPtr<SharedBuffer> releaseBuffer() { return SharedBuffer::adoptVector(m_vector); }
-
     private:
+        friend class CString;
+
+        static PassRefPtr<CStringBuffer> create(unsigned length) { return adoptRef(new CStringBuffer(length)); }
         CStringBuffer(unsigned length) : m_vector(length) { }
+        char* mutableData() { return m_vector.data(); }
 
         Vector<char> m_vector;
     };
@@ -56,6 +56,7 @@ namespace WebCore {
         CString() { }
         CString(const char*);
         CString(const char*, unsigned length);
+        CString(CStringBuffer* buffer) : m_buffer(buffer) { }
         static CString newUninitialized(size_t length, char*& characterBuffer);
 
         const char* data() const;
@@ -63,8 +64,8 @@ namespace WebCore {
         unsigned length() const;
 
         bool isNull() const { return !m_buffer; }
-        
-        PassRefPtr<SharedBuffer> releaseBuffer();
+
+        CStringBuffer* buffer() const { return m_buffer.get(); }
 
     private:
         void copyBufferIfNeeded();