Reviewed by Anders
authorbeidson <beidson@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 16 Sep 2006 01:40:34 +0000 (01:40 +0000)
committerbeidson <beidson@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 16 Sep 2006 01:40:34 +0000 (01:40 +0000)
        Added the ability to get a mutable char* from a CString, which will copy the internal
        buffer if the ref count is greater than 1 so your mutable char* won't affect any other
        referrer of that buffer.

        * platform/CString.cpp:
        (WebCore::CString::mutableData):
        (WebCore::CString::copyBufferIfNeeded):
        * platform/CString.h:

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

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

index 142b5debb80a7c7fc50a8603cd7b9d3b7f2301b4..e3239a8407bbfcf9a5ec6acb5ff40f67ac144ab9 100644 (file)
@@ -1,3 +1,16 @@
+2006-09-15  Brady Eidson  <beidson@apple.com>
+
+        Reviewed by Anders
+
+        Added the ability to get a mutable char* from a CString, which will copy the internal
+        buffer if the ref count is greater than 1 so your mutable char* won't affect any other
+        referrer of that buffer.
+
+        * platform/CString.cpp:
+        (WebCore::CString::mutableData):
+        (WebCore::CString::copyBufferIfNeeded):
+        * platform/CString.h:
+
 2006-09-15  Justin Garcia  <justin.garcia@apple.com>
 
         Reviewed by harrison
index 93a3da4dab61792cb306fda862883801b3c3a958..7c33adb816b1321a4787fbaa703d0cc36a4f30ce 100644 (file)
@@ -59,6 +59,14 @@ const char* CString::data() const
 {
     return m_buffer ? m_buffer->data() : 0;
 }
+
+char* CString::mutableData()
+{
+    copyBufferIfNeeded();
+    if (!m_buffer)
+        return 0;
+    return m_buffer->data();
+}
     
 unsigned CString::length() const
 {
@@ -80,4 +88,15 @@ CString CString::newUninitialized(size_t length, char*& characterBuffer)
     return result;
 }
 
+void CString::copyBufferIfNeeded()
+{
+    if (!m_buffer || m_buffer->hasOneRef())
+        return;
+        
+    int len = m_buffer->length();
+    RefPtr<CStringBuffer> m_temp = m_buffer;
+    m_buffer = new CStringBuffer(len);
+    memcpy(m_buffer->data(), m_temp->data(), len);
+}
+
 }
index dc7d02e10a8304a7bfc042917e4b5109033ad0b5..ac32caa37476c0b0b6907fd7cf9ef5b047a5e2d7 100644 (file)
@@ -52,6 +52,7 @@ namespace WebCore {
         static CString newUninitialized(size_t length, char*& characterBuffer);
 
         const char* data() const;
+        char* mutableData();
         unsigned length() const;
 
         operator const char*() const { return data(); }        
@@ -62,6 +63,7 @@ namespace WebCore {
         DeprecatedCString deprecatedCString() const;
 
     private:
+        void copyBufferIfNeeded();
         void init(const char*, unsigned length);
         RefPtr<CStringBuffer> m_buffer;
     };