Fix string::replace which failed replacing substrings at the end when the replacement was longer (thx @ zerochen for reporting). Corresonding tests added.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@4230 dfc29bdd-3216-0410-991c-e03cc46cb475
master
cutealien 2012-07-07 09:14:59 +00:00
parent d432f415a2
commit 238f3b7bd8
3 changed files with 77 additions and 13 deletions

View File

@ -1075,7 +1075,7 @@ public:
}
array[i-1] = 0;
used = i;
return *this;
}
@ -1094,16 +1094,13 @@ public:
if (used + len > allocated)
reallocate(used + len);
// Don't take the string terminator into account.
--used;
// Start replacing.
pos = 0;
while ((pos = find(other, pos)) != -1)
{
T* start = array + pos + other_size - 1;
T* ptr = array + used;
T* end = array + used + delta;
T* ptr = array + used - 1;
T* end = array + delta + used -1;
// Shift characters to make room for the string.
while (ptr != start)
@ -1121,9 +1118,6 @@ public:
used += delta;
}
// Terminate the string and return ourself.
array[used] = 0;
++used;
return *this;
}

View File

@ -39,6 +39,73 @@ static bool testFastAlloc()
return true;
}
static bool testReplace()
{
// test string getting longer
core::stringw str = L"no";
str.replace(L"no", L"yes");
if ( str != L"yes" )
return false;
str = L"nonono";
str.replace(L"no", L"yes");
if ( str != L"yesyesyes" )
return false;
str = L"nomaybenomaybeno";
str.replace(L"no", L"yes");
if ( str != L"yesmaybeyesmaybeyes" )
return false;
// test string staying same length
str = L"one";
str.replace(L"one", L"two");
if ( str != L"two" )
return false;
str = L"oneone";
str.replace(L"one", L"two");
if ( str != L"twotwo" )
return false;
// test string getting shorter
str = L"yes";
str.replace(L"yes", L"no");
if ( str != L"no" )
return false;
str = L"yesyes";
str.replace(L"yes", L"no");
if ( str != L"nono" )
return false;
// remove string-parts completely
str = L"killme";
str.replace(L"killme", L"");
if ( str != L"" )
return false;
str = L"killmenow";
str.replace(L"killme", L"");
if ( str != L"now" )
return false;
str = L"nowkillme";
str.replace(L"killme", L"");
if ( str != L"now" )
return false;
// remove nothing
str = L"keepme";
str.replace(L"", L"whatever");
if ( str != L"keepme" )
return false;
str = L"keepme";
str.replace(L"", L"");
if ( str != L"keepme" )
return false;
return true;
}
bool testAppendStringc()
{
@ -173,6 +240,9 @@ bool testIrrString(void)
logTestString("test fast alloc\n");
allExpected &= testFastAlloc();
logTestString("test replace\n");
allExpected &= testReplace();
if(allExpected)
logTestString("\nAll tests passed\n");
else

View File

@ -1,4 +1,4 @@
Tests finished. 1 test of 1 passed.
Compiled as DEBUG
Test suite pass at GMT Wed Jun 27 19:35:04 2012
Tests finished. 1 test of 1 passed.
Compiled as DEBUG
Test suite pass at GMT Sat Jul 7 09:09:13 2012