wxString::replace() assert fixed, test for it added

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7164 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-04-14 18:08:35 +00:00
parent 27df708603
commit f568f52dd8
2 changed files with 52 additions and 5 deletions

View File

@@ -48,10 +48,10 @@
//#define TEST_MIME //#define TEST_MIME
//#define TEST_INFO_FUNCTIONS //#define TEST_INFO_FUNCTIONS
//#define TEST_SOCKETS //#define TEST_SOCKETS
//#define TEST_STRINGS #define TEST_STRINGS
//#define TEST_THREADS //#define TEST_THREADS
//#define TEST_TIMER //#define TEST_TIMER
#define TEST_WCHAR //#define TEST_WCHAR
// ============================================================================ // ============================================================================
// implementation // implementation
@@ -1067,6 +1067,7 @@ static void TestStopWatch()
#ifdef TEST_WCHAR #ifdef TEST_WCHAR
#include <wx/strconv.h> #include <wx/strconv.h>
#include <wx/buffer.h>
static void TestUtf8() static void TestUtf8()
{ {
@@ -2585,6 +2586,49 @@ static void TestStringTokenizer()
puts(""); puts("");
} }
static void TestStringReplace()
{
puts("*** Testing wxString::replace ***");
static const struct StringReplaceTestData
{
const wxChar *original; // original test string
size_t start, len; // the part to replace
const wxChar *replacement; // the replacement string
const wxChar *result; // and the expected result
} stringReplaceTestData[] =
{
{ _T("012-AWORD-XYZ"), 4, 5, _T("BWORD"), _T("012-BWORD-XYZ") },
{ _T("increase"), 0, 2, _T("de"), _T("decrease") },
{ _T("wxWindow"), 8, 0, _T("s"), _T("wxWindows") },
{ _T("foobar"), 3, 0, _T("-"), _T("foo-bar") },
{ _T("barfoo"), 0, 6, _T("foobar"), _T("foobar") },
};
for ( size_t n = 0; n < WXSIZEOF(stringReplaceTestData); n++ )
{
const StringReplaceTestData data = stringReplaceTestData[n];
wxString original = data.original;
original.replace(data.start, data.len, data.replacement);
wxPrintf(_T("wxString(\"%s\").replace(%u, %u, %s) = %s "),
data.original, data.start, data.len, data.replacement,
original.c_str());
if ( original == data.result )
{
puts("(ok)");
}
else
{
wxPrintf(_T("(ERROR: should be '%s')\n"), data.result);
}
}
puts("");
}
#endif // TEST_STRINGS #endif // TEST_STRINGS
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -2652,6 +2696,7 @@ int main(int argc, char **argv)
TestStringFind(); TestStringFind();
TestStringTokenizer(); TestStringTokenizer();
} }
TestStringReplace();
#endif // TEST_STRINGS #endif // TEST_STRINGS
#ifdef TEST_ARRAYS #ifdef TEST_ARRAYS

View File

@@ -1717,13 +1717,15 @@ wxString& wxString::erase(size_t nStart, size_t nLen)
wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz) wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz)
{ {
wxASSERT( nStart + nLen <= wxStrlen(sz) ); wxASSERT_MSG( nStart + nLen <= Len(),
_T("index out of bounds in wxString::replace") );
wxString strTmp; wxString strTmp;
strTmp.Alloc(Len()); // micro optimisation to avoid multiple mem allocs
if ( nStart != 0 ) if ( nStart != 0 )
strTmp.append(c_str(), nStart); strTmp.append(c_str(), nStart);
strTmp += sz; strTmp << sz << c_str() + nStart + nLen;
strTmp.append(c_str() + nStart + nLen);
*this = strTmp; *this = strTmp;
return *this; return *this;