From 30d9011f401386ccd2296b94dde6af611e8396d8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 30 Sep 1998 12:41:13 +0000 Subject: [PATCH] fixed wxString::Mid() bug git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@788 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/string.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/common/string.cpp b/src/common/string.cpp index 054074c259..88f44a8a8e 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -564,20 +564,32 @@ void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const } // 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 { - // out-of-bounds requests return sensible things - if ( nCount == 0 ) - nCount = GetStringData()->nDataLength - nFirst; + wxStringData *pData = GetStringData(); + size_t nLen = pData->nDataLength; - if ( nFirst + nCount > (size_t)GetStringData()->nDataLength ) - nCount = GetStringData()->nDataLength - nFirst; - if ( nFirst > (size_t)GetStringData()->nDataLength ) + // default value of nCount is STRING_MAXLEN and means "till the end" + if ( nCount == STRING_MAXLEN ) + { + 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; + } wxString dest; AllocCopy(dest, nCount, nFirst); + return dest; }