Fixed: FString::StripLeftRight() lost the right character when copying to a new buffer

- Also, some minor improvements to the strip functions to avoid doing
  extra work.
This commit is contained in:
Marisa Heit 2016-11-11 23:55:02 -06:00
parent 513ad7f75f
commit 1e497e0b3e

View file

@ -635,6 +635,10 @@ void FString::StripLeft ()
if (!isspace(Chars[i])) if (!isspace(Chars[i]))
break; break;
} }
if (i == 0)
{ // Nothing to strip.
return;
}
if (Data()->RefCount <= 1) if (Data()->RefCount <= 1)
{ {
for (j = 0; i <= max; ++j, ++i) for (j = 0; i <= max; ++j, ++i)
@ -666,6 +670,10 @@ void FString::StripLeft (const char *charset)
if (!strchr (charset, Chars[i])) if (!strchr (charset, Chars[i]))
break; break;
} }
if (i == 0)
{ // Nothing to strip.
return;
}
if (Data()->RefCount <= 1) if (Data()->RefCount <= 1)
{ {
for (j = 0; i <= max; ++j, ++i) for (j = 0; i <= max; ++j, ++i)
@ -686,11 +694,16 @@ void FString::StripLeft (const char *charset)
void FString::StripRight () void FString::StripRight ()
{ {
size_t max = Len(), i; size_t max = Len(), i;
for (i = max; i-- > 0; ) if (max == 0) return;
for (i = --max; i-- > 0; )
{ {
if (!isspace(Chars[i])) if (!isspace(Chars[i]))
break; break;
} }
if (i == max)
{ // Nothing to strip.
return;
}
if (Data()->RefCount <= 1) if (Data()->RefCount <= 1)
{ {
Chars[i+1] = '\0'; Chars[i+1] = '\0';
@ -714,11 +727,15 @@ void FString::StripRight (const char *charset)
{ {
size_t max = Len(), i; size_t max = Len(), i;
if (max == 0) return; if (max == 0) return;
for (i = max; i-- > 0; ) for (i = --max; i-- > 0; )
{ {
if (!strchr (charset, Chars[i])) if (!strchr (charset, Chars[i]))
break; break;
} }
if (i == max)
{ // Nothing to strip.
return;
}
if (Data()->RefCount <= 1) if (Data()->RefCount <= 1)
{ {
Chars[i+1] = '\0'; Chars[i+1] = '\0';
@ -747,6 +764,10 @@ void FString::StripLeftRight ()
if (!isspace(Chars[j])) if (!isspace(Chars[j]))
break; break;
} }
if (i == 0 && j == max - 1)
{ // Nothing to strip.
return;
}
if (Data()->RefCount <= 1) if (Data()->RefCount <= 1)
{ {
for (k = 0; i <= j; ++i, ++k) for (k = 0; i <= j; ++i, ++k)
@ -759,8 +780,8 @@ void FString::StripLeftRight ()
else else
{ {
FStringData *old = Data(); FStringData *old = Data();
AllocBuffer (j - i); AllocBuffer(j - i + 1);
StrCopy (Chars, old->Chars(), j - i); StrCopy(Chars, old->Chars(), j - i + 1);
old->Release(); old->Release();
} }
} }