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-e03cc46cb475master
parent
d432f415a2
commit
238f3b7bd8
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue