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

View File

@ -39,6 +39,73 @@ static bool testFastAlloc()
return true; 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() bool testAppendStringc()
{ {
@ -173,6 +240,9 @@ bool testIrrString(void)
logTestString("test fast alloc\n"); logTestString("test fast alloc\n");
allExpected &= testFastAlloc(); allExpected &= testFastAlloc();
logTestString("test replace\n");
allExpected &= testReplace();
if(allExpected) if(allExpected)
logTestString("\nAll tests passed\n"); logTestString("\nAll tests passed\n");
else else

View File

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