]> git.saurik.com Git - wxWidgets.git/commitdiff
fixed size of buffer returned by wxFormatConverter (it was too large before)
authorVáclav Slavík <vslavik@fastmail.fm>
Mon, 30 Mar 2009 11:54:41 +0000 (11:54 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Mon, 30 Mar 2009 11:54:41 +0000 (11:54 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59939 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/buffer.h
interface/wx/buffer.h
src/common/strvararg.cpp

index 66450e77f701c5b50ff6e655d643e6794187c494..f3c4e34f4327304f1a8299c726694b48a5b1fcd1 100644 (file)
@@ -330,6 +330,17 @@ public:
 
         return true;
     }
 
         return true;
     }
+
+    void shrink(size_t len)
+    {
+        wxASSERT_MSG( this->m_data->m_owned, "cannot shrink non-owned buffer" );
+        wxASSERT_MSG( this->m_data->m_ref == 1, "can't shrink shared buffer" );
+
+        wxASSERT( len <= this->length() );
+
+        this->m_data->m_length = len;
+        this->data()[len] = 0;
+    }
 };
 
 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
 };
 
 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer<char> )
index 3d5287b6c7270730e13ade1ffe04a03162ec5ca3..b6052f47d92118b792a8cbd5517c0172d429c03b 100644 (file)
@@ -200,8 +200,27 @@ public:
 
         Can only be called on buffers that don't share data with another
         buffer (i.e. reference count of the data is 1).
 
         Can only be called on buffers that don't share data with another
         buffer (i.e. reference count of the data is 1).
+
+        @see shrink()
      */
     bool extend(size_t len);
      */
     bool extend(size_t len);
+
+    /**
+        Shrinks the buffer to have size @a len and NUL-terminates the string
+        at this length.
+
+        Can only be called on buffers that don't share data with another
+        buffer (i.e. reference count of the data is 1).
+
+        @param len Length to shrink to. Must not be larger than current length.
+
+        @note The string is not reallocated to take less memory.
+
+        @since 2.9.0
+
+        @see extend()
+     */
+    bool shrink(size_t len);
 };
 
 /**
 };
 
 /**
index e89bdf35b593a78c80ec5a95ccf6a3776a20c961..e71c060b109706a03fd1f260dd36b38c6ce1120f 100644 (file)
@@ -152,7 +152,7 @@ public:
         m_nCopied = 0;
     }
 
         m_nCopied = 0;
     }
 
-    wxCharTypeBuffer<CharType> Convert(const CharType *format)
+    wxScopedCharTypeBuffer<CharType> Convert(const CharType *format)
     {
         // this is reset to NULL if we modify the format string
         m_fmtOrig = format;
     {
         // this is reset to NULL if we modify the format string
         m_fmtOrig = format;
@@ -263,12 +263,14 @@ public:
         // format
         if ( m_fmtOrig )
         {
         // format
         if ( m_fmtOrig )
         {
-            return wxCharTypeBuffer<CharType>::CreateNonOwned(m_fmtOrig);
+            return wxScopedCharTypeBuffer<CharType>::CreateNonOwned(m_fmtOrig);
         }
         else
         {
         }
         else
         {
-            // NULL-terminate converted format string:
-            *m_fmtLast = 0;
+            // shrink converted format string to actual size (instead of
+            // over-sized allocation from CopyAllBefore()) and NUL-terminate
+            // it:
+            m_fmt.shrink(m_fmtLast - m_fmt.data());
             return m_fmt;
         }
     }
             return m_fmt;
         }
     }