X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6f5941be5f080ca42cb5fed4b672f5db16c0d8e..68698f0308f5c46966b7de12b34f68ec23dd3ec8:/src/common/string.cpp diff --git a/src/common/string.cpp b/src/common/string.cpp index e2563e5c82..791c5ce049 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -6,7 +6,7 @@ // Created: 29/01/98 // RCS-ID: $Id$ // Copyright: (c) 1998 Vadim Zeitlin -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -229,24 +229,38 @@ wxString::wxString(const void *pStart, const void *pEnd) // from multibyte string wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) { - // first get necessary size - size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0; + // first get the size of the buffer we need + size_t nLen; + if ( psz ) + { + // calculate the needed size ourselves or use a provide one + nLen = nLength == wxSTRING_MAXLEN ? conv.MB2WC(NULL, psz, 0) : nLength; + } + else + { + // nothing to convert + nLen = 0; + } - // nLength is number of *Unicode* characters here! - if ((nLen != (size_t)-1) && (nLen > nLength)) - nLen = nLength; + // anything to do? + if ( (nLen != 0) && (nLen != (size_t)-1) ) + { + if ( !AllocBuffer(nLen) ) + { + wxFAIL_MSG( _T("out of memory in wxString::wxString") ); + return; + } - // empty? - if ( (nLen != 0) && (nLen != (size_t)-1) ) { - if ( !AllocBuffer(nLen) ) { - wxFAIL_MSG( _T("out of memory in wxString::wxString") ); - return; + // MB2WC wants the buffer size, not the string length + if ( conv.MB2WC(m_pchData, psz, nLen + 1) != (size_t)-1 ) + { + // initialized ok + return; + } + //else: the conversion failed -- leave the string empty (what else?) } - conv.MB2WC(m_pchData, psz, nLen); - } - else { + Init(); - } } #else // ANSI @@ -255,27 +269,38 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) // from wide string wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) { - // first get necessary size - size_t nLen = 0; - if (pwz) - { - if (nLength == wxSTRING_MAXLEN) - nLen = conv.WC2MB((char *) NULL, pwz, 0); + // first get the size of the buffer we need + size_t nLen; + if ( pwz ) + { + // calculate the needed size ourselves or use a provide one + nLen = nLength == wxSTRING_MAXLEN ? conv.WC2MB(NULL, pwz, 0) : nLength; + } else - nLen = nLength; - } + { + // nothing to convert + nLen = 0; + } - // empty? - if ( (nLen != 0) && (nLen != (size_t)-1) ) { - if ( !AllocBuffer(nLen) ) { - wxFAIL_MSG( _T("out of memory in wxString::wxString") ); - return; + // anything to do? + if ( (nLen != 0) && (nLen != (size_t)-1) ) + { + if ( !AllocBuffer(nLen) ) + { + wxFAIL_MSG( _T("out of memory in wxString::wxString") ); + return; + } + + // WC2MB wants the buffer size, not the string length + if ( conv.WC2MB(m_pchData, pwz, nLen + 1) != (size_t)-1 ) + { + // initialized ok + return; + } + //else: the conversion failed -- leave the string empty (what else?) } - conv.WC2MB(m_pchData, pwz, nLen); - } - else { + Init(); - } } #endif // wxUSE_WCHAR_T @@ -583,7 +608,7 @@ wxString& wxString::operator=(const wchar_t *pwz) // --------------------------------------------------------------------------- // add something to this string -bool wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData) +bool wxString::ConcatSelf(size_t nSrcLen, const wxChar *pszSrcData) { STATISTICS_ADD(SummandLength, nSrcLen); @@ -730,6 +755,16 @@ wxString wxString::FromAscii(const char *ascii) return res; } +wxString wxString::FromAscii(const char ascii) +{ + // What do we do with '\0' ? + + wxString res; + res += (wchar_t)(unsigned char) ascii; + + return res; +} + const wxCharBuffer wxString::ToAscii() const { // this will allocate enough space for the terminating NUL too @@ -1752,7 +1787,7 @@ void wxArrayString::Copy(const wxArrayString& src) void wxArrayString::Grow(size_t nIncrement) { // only do it if no more place - if ( m_nCount == m_nSize ) { + if ( (m_nSize - m_nCount) < nIncrement ) { // if ARRAY_DEFAULT_INITIAL_SIZE were set to 0, the initially empty would // be never resized! #if ARRAY_DEFAULT_INITIAL_SIZE == 0 @@ -1762,6 +1797,8 @@ void wxArrayString::Grow(size_t nIncrement) if ( m_nSize == 0 ) { // was empty, alloc some memory m_nSize = ARRAY_DEFAULT_INITIAL_SIZE; + if (m_nSize < nIncrement) + m_nSize = nIncrement; m_pItems = new wxChar *[m_nSize]; } else { @@ -2054,7 +2091,7 @@ static wxArrayString::CompareFunction gs_compareFunction = NULL; static bool gs_sortAscending = TRUE; // function which is called by quick sort -extern "C" int LINKAGEMODE +extern "C" int wxC_CALLING_CONV // LINKAGEMODE wxStringCompareFunction(const void *first, const void *second) { wxString *strFirst = (wxString *)first;