X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fd242375198e16a92283f633dfe0bdb80d1ffa58..2da2f941bbd25599c2c1453204b5f1a4e116ae91:/src/common/string.cpp diff --git a/src/common/string.cpp b/src/common/string.cpp index 25e4aa3de3..c43bde8e7d 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -245,7 +245,7 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) size_t nLen; if ( psz ) { - // calculate the needed size ourselves or use a provide one + // calculate the needed size ourselves or use the provided one nLen = nLength == wxSTRING_MAXLEN ? conv.MB2WC(NULL, psz, 0) : nLength; } else @@ -260,17 +260,22 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) 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 ) + else { - // initialized ok - m_pchData[nLen] = 0; - return; + // MB2WC wants the buffer size, not the string length hence +1 + nLen = conv.MB2WC(m_pchData, psz, nLen + 1); + + if ( nLen != (size_t)-1 ) + { + // initialized ok, set the real length as nLength specified by + // the caller could be greater than the real string length + GetStringData()->nDataLength = nLen; + m_pchData[nLen] = 0; + return; + } + //else: the conversion failed -- leave the string empty (what else?) } - //else: the conversion failed -- leave the string empty (what else?) } Init(); @@ -286,7 +291,7 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) size_t nLen; if ( pwz ) { - // calculate the needed size ourselves or use a provide one + // calculate the needed size ourselves or use the provided one nLen = nLength == wxSTRING_MAXLEN ? conv.WC2MB(NULL, pwz, 0) : nLength; } else @@ -301,16 +306,17 @@ wxString::wxString(const wchar_t *pwz, wxMBConv& conv, size_t nLength) 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 ) + else { - // initialized ok - 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?) } - //else: the conversion failed -- leave the string empty (what else?) } Init(); @@ -1753,6 +1759,10 @@ wxString& wxString::replace(size_t nStart, size_t nLen, // ArrayString // ============================================================================ +#include "wx/arrstr.h" + +#if !wxUSE_STL + // size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT) #define ARRAY_MAXSIZE_INCREMENT 4096 @@ -1906,6 +1916,8 @@ void wxArrayString::Shrink() } } +#if WXWIN_COMPATIBILITY_2_4 + // return a wxString[] as required for some control ctors. wxString* wxArrayString::GetStringArray() const { @@ -1921,6 +1933,8 @@ wxString* wxArrayString::GetStringArray() const return array; } +#endif // WXWIN_COMPATIBILITY_2_4 + // searches the array for an item (forward or backwards) int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const { @@ -2051,7 +2065,7 @@ void wxArrayString::SetCount(size_t count) } // removes item from array (by index) -void wxArrayString::Remove(size_t nIndex, size_t nRemove) +void wxArrayString::RemoveAt(size_t nIndex, size_t nRemove) { wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArrayString::Remove") ); wxCHECK_RET( nIndex + nRemove <= m_nCount, @@ -2074,7 +2088,7 @@ void wxArrayString::Remove(const wxChar *sz) wxCHECK_RET( iIndex != wxNOT_FOUND, wxT("removing inexistent element in wxArrayString::Remove") ); - Remove(iIndex); + RemoveAt(iIndex); } // ---------------------------------------------------------------------------- @@ -2142,16 +2156,16 @@ void wxArrayString::Sort(CompareFunction compareFunction) END_SORT(); } -void wxArrayString::Sort(bool reverseOrder) -{ - START_SORT(); +typedef int (wxC_CALLING_CONV * wxStringCompareFn)(const void *first, const void *second); - wxASSERT( !gs_compareFunction ); // must have been reset to NULL - gs_sortAscending = !reverseOrder; - - DoSort(); +void wxArrayString::Sort(CompareFunction2 compareFunction) +{ + qsort(m_pItems, m_nCount, sizeof(wxChar *), (wxStringCompareFn)compareFunction); +} - END_SORT(); +void wxArrayString::Sort(bool reverseOrder) +{ + Sort(reverseOrder ? wxStringSortDescending : wxStringSortAscending); } void wxArrayString::DoSort() @@ -2177,3 +2191,14 @@ bool wxArrayString::operator==(const wxArrayString& a) const return TRUE; } +#endif // !wxUSE_STL + +int wxStringSortAscending(wxString* s1, wxString* s2) +{ + return wxStrcmp(s1->c_str(), s2->c_str()); +} + +int wxStringSortDescending(wxString* s1, wxString* s2) +{ + return -wxStrcmp(s1->c_str(), s2->c_str()); +}