]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
Moved Get/SetToolBar down into frame.h/cpp
[wxWidgets.git] / src / common / string.cpp
index c2246e499151cd7ba6d47598d0a95dea457f9c10..49b3c74f866bbbc85b5b3ac86ef043c784f6a2f1 100644 (file)
@@ -282,13 +282,22 @@ void wxString::Alloc(uint nLen)
 {
   wxStringData *pData = GetStringData();
   if ( pData->nAllocLength <= nLen ) {
 {
   wxStringData *pData = GetStringData();
   if ( pData->nAllocLength <= nLen ) {
-    if ( pData->IsEmpty() )
-      AllocBuffer(nLen);
+    if ( pData->IsEmpty() ) {
+      nLen += EXTRA_ALLOC;
+
+      wxStringData* pData = (wxStringData*)
+        malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(char));
+      pData->nRefs = 1;
+      pData->nDataLength = 0;
+      pData->nAllocLength = nLen;
+      m_pchData = pData->data();  // data starts after wxStringData
+      m_pchData[0u] = '\0';
+    }
     else if ( pData->IsShared() ) {
       pData->Unlock();                // memory not freed because shared
     else if ( pData->IsShared() ) {
       pData->Unlock();                // memory not freed because shared
-      uint nLen = pData->nDataLength;
+      uint nOldLen = pData->nDataLength;
       AllocBuffer(nLen);
       AllocBuffer(nLen);
-      memcpy(m_pchData, pData->data(), nLen*sizeof(char));
+      memcpy(m_pchData, pData->data(), nOldLen*sizeof(char));
     }
     else {
       nLen += EXTRA_ALLOC;
     }
     else {
       nLen += EXTRA_ALLOC;
@@ -1064,33 +1073,24 @@ wxArrayString::wxArrayString()
 // copy ctor
 wxArrayString::wxArrayString(const wxArrayString& src)
 {
 // copy ctor
 wxArrayString::wxArrayString(const wxArrayString& src)
 {
-  m_nSize  = src.m_nSize;
-  m_nCount = src.m_nCount;
-
-  if ( m_nSize != 0 )
-    m_pItems = new char *[m_nSize];
-  else
-    m_pItems = NULL;
+  m_pItems = NULL;
 
 
-  if ( m_nCount != 0 )
-    memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(char *));
+  *this = src;
 }
 
 }
 
-// copy operator
+// assignment operator
 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
 {
   DELETEA(m_pItems);
 
 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
 {
   DELETEA(m_pItems);
 
-  m_nSize  = src.m_nSize;
-  m_nCount = src.m_nCount;
-
-  if ( m_nSize != 0 )
-    m_pItems = new char *[m_nCount];
-  else
-    m_pItems = NULL;
+  m_nSize = 0;
+  if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
+    Alloc(src.m_nCount);
 
 
-  if ( m_nCount != 0 )
-    memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(char *));
+  // we can't just copy the pointers here because otherwise we would share
+  // the strings with another array
+  for ( uint n = 0; n < src.m_nCount; n++ )
+    Add(src[n]);
 
   return *this;
 }
 
   return *this;
 }
@@ -1107,7 +1107,8 @@ void wxArrayString::Grow()
     }
     else {
       // add 50% but not too much
     }
     else {
       // add 50% but not too much
-      size_t nIncrement = m_nSize >> 1;
+      size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE 
+                          ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
       if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
         nIncrement = ARRAY_MAXSIZE_INCREMENT;
       m_nSize += nIncrement;
       if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
         nIncrement = ARRAY_MAXSIZE_INCREMENT;
       m_nSize += nIncrement;