BString: Treat NULL passed as replaceWith as an empty string.

Fixes the tests added in the previous commit, and also #8552.

Change-Id: Idf9459474bc66054f94cf66065ed6fcf9c60cece
Reviewed-on: https://review.haiku-os.org/572
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Augustin Cavalier 2018-09-16 13:55:06 -04:00 committed by waddlesplash
parent 38a133eb8a
commit f436972c14
1 changed files with 14 additions and 10 deletions

View File

@ -1545,7 +1545,7 @@ BString::Replace(char replaceThis, char withThis, int32 maxReplaceCount,
BString&
BString::ReplaceFirst(const char* replaceThis, const char* withThis)
{
if (!replaceThis || !withThis || FindFirst(replaceThis) < 0)
if (replaceThis == NULL || FindFirst(replaceThis) < 0)
return *this;
if (_MakeWritable() != B_OK)
@ -1558,8 +1558,10 @@ BString::ReplaceFirst(const char* replaceThis, const char* withThis)
BString&
BString::ReplaceLast(const char* replaceThis, const char* withThis)
{
if (!replaceThis || !withThis)
if (replaceThis == NULL)
return *this;
if (withThis == NULL)
withThis = "";
int32 replaceThisLength = strlen(replaceThis);
int32 pos = _FindBefore(replaceThis, Length(), replaceThisLength);
@ -1589,7 +1591,7 @@ BString&
BString::ReplaceAll(const char* replaceThis, const char* withThis,
int32 fromOffset)
{
if (!replaceThis || !withThis || FindFirst(replaceThis) < 0)
if (replaceThis == NULL || FindFirst(replaceThis) < 0)
return *this;
if (_MakeWritable() != B_OK)
@ -1604,7 +1606,7 @@ BString&
BString::Replace(const char* replaceThis, const char* withThis,
int32 maxReplaceCount, int32 fromOffset)
{
if (!replaceThis || !withThis || maxReplaceCount <= 0
if (replaceThis == NULL || maxReplaceCount <= 0
|| FindFirst(replaceThis) < 0)
return *this;
@ -1696,7 +1698,7 @@ BString::IReplace(char replaceThis, char withThis, int32 maxReplaceCount,
BString&
BString::IReplaceFirst(const char* replaceThis, const char* withThis)
{
if (!replaceThis || !withThis || IFindFirst(replaceThis) < 0)
if (replaceThis == NULL || IFindFirst(replaceThis) < 0)
return *this;
if (_MakeWritable() != B_OK)
@ -1708,8 +1710,10 @@ BString::IReplaceFirst(const char* replaceThis, const char* withThis)
BString&
BString::IReplaceLast(const char* replaceThis, const char* withThis)
{
if (!replaceThis || !withThis)
if (replaceThis == NULL)
return *this;
if (withThis == NULL)
withThis = "";
int32 replaceThisLength = strlen(replaceThis);
int32 pos = _IFindBefore(replaceThis, Length(), replaceThisLength);
@ -1739,7 +1743,7 @@ BString&
BString::IReplaceAll(const char* replaceThis, const char* withThis,
int32 fromOffset)
{
if (!replaceThis || !withThis || IFindFirst(replaceThis) < 0)
if (replaceThis == NULL || IFindFirst(replaceThis) < 0)
return *this;
if (_MakeWritable() != B_OK)
@ -1754,7 +1758,7 @@ BString&
BString::IReplace(const char* replaceThis, const char* withThis,
int32 maxReplaceCount, int32 fromOffset)
{
if (!replaceThis || !withThis || maxReplaceCount <= 0
if (replaceThis == NULL || maxReplaceCount <= 0
|| FindFirst(replaceThis) < 0)
return *this;
@ -2634,7 +2638,7 @@ BString::_DoReplace(const char* findThis, const char* replaceWith,
TFindMethod findMethod = ignoreCase
? &BString::_IFindAfter : &BString::_FindAfter;
if (!replaceWith)
if (replaceWith == NULL)
replaceWith = "";
int32 replaceLen = strlen(replaceWith);