fixed wxString::Mid() bug

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@788 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 1998-09-30 12:41:13 +00:00
parent ac9390a0bc
commit 30d9011f40

View File

@ -564,20 +564,32 @@ void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
} }
// extract string of length nCount starting at nFirst // extract string of length nCount starting at nFirst
// default value of nCount is 0 and means "till the end"
wxString wxString::Mid(size_t nFirst, size_t nCount) const wxString wxString::Mid(size_t nFirst, size_t nCount) const
{ {
// out-of-bounds requests return sensible things wxStringData *pData = GetStringData();
if ( nCount == 0 ) size_t nLen = pData->nDataLength;
nCount = GetStringData()->nDataLength - nFirst;
if ( nFirst + nCount > (size_t)GetStringData()->nDataLength ) // default value of nCount is STRING_MAXLEN and means "till the end"
nCount = GetStringData()->nDataLength - nFirst; if ( nCount == STRING_MAXLEN )
if ( nFirst > (size_t)GetStringData()->nDataLength ) {
nCount = nLen - nFirst;
}
// out-of-bounds requests return sensible things
if ( nFirst + nCount > nLen )
{
nCount = nLen - nFirst;
}
if ( nFirst > nLen )
{
// AllocCopy() will return empty string
nCount = 0; nCount = 0;
}
wxString dest; wxString dest;
AllocCopy(dest, nCount, nFirst); AllocCopy(dest, nCount, nFirst);
return dest; return dest;
} }