// allocates memory needed to store a C string of length nLen
void wxString::AllocBuffer(size_t nLen)
{
- wxASSERT( nLen > 0 ); //
- wxASSERT( nLen <= INT_MAX-1 ); // max size (enough room for 1 extra)
+ // allocating 0 sized buffer doesn't make sense, all empty strings should
+ // reuse g_strEmpty
+ wxASSERT( nLen > 0 );
+
+ // make sure that we don't overflow
+ wxASSERT( nLen < (INT_MAX / sizeof(wxChar)) -
+ (sizeof(wxStringData) + EXTRA_ALLOC + 1) );
STATISTICS_ADD(Length, nLen);
}
// ---------------------------------------------------------------------------
-// stream-like operators
+// formatted output
// ---------------------------------------------------------------------------
-wxString& wxString::operator<<(int i)
+
+/* static */
+wxString wxString::Format(const wxChar *pszFormat, ...)
{
- wxString res;
- res.Printf(wxT("%d"), i);
+ va_list argptr;
+ va_start(argptr, pszFormat);
- return (*this) << res;
-}
+ wxString s;
+ s.PrintfV(pszFormat, argptr);
-wxString& wxString::operator<<(float f)
-{
- wxString res;
- res.Printf(wxT("%f"), f);
+ va_end(argptr);
- return (*this) << res;
+ return s;
}
-wxString& wxString::operator<<(double d)
+/* static */
+wxString wxString::FormatV(const wxChar *pszFormat, va_list argptr)
{
- wxString res;
- res.Printf(wxT("%g"), d);
-
- return (*this) << res;
+ wxString s;
+ s.Printf(pszFormat, argptr);
+ return s;
}
-// ---------------------------------------------------------------------------
-// formatted output
-// ---------------------------------------------------------------------------
-
int wxString::Printf(const wxChar *pszFormat, ...)
{
va_list argptr;
if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
Alloc(src.m_nCount);
- // we can't just copy the pointers here because otherwise we would share
- // the strings with another array because strings are ref counted
-#if 0
- if ( m_nCount != 0 )
- memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(wxChar *));
-#endif // 0
-
for ( size_t n = 0; n < src.m_nCount; n++ )
Add(src[n]);
-
- // if the other array is auto sorted too, we're already sorted, but
- // otherwise we should rearrange the items
- if ( m_autoSort && !src.m_autoSort )
- Sort();
}
// grow the array