From 1e497e0b3e1eec847fbc2d17aabd2954b6a358e3 Mon Sep 17 00:00:00 2001 From: Marisa Heit Date: Fri, 11 Nov 2016 23:55:02 -0600 Subject: [PATCH] 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. --- src/zstring.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/zstring.cpp b/src/zstring.cpp index 63fdf6428..29270b970 100644 --- a/src/zstring.cpp +++ b/src/zstring.cpp @@ -635,6 +635,10 @@ void FString::StripLeft () if (!isspace(Chars[i])) break; } + if (i == 0) + { // Nothing to strip. + return; + } if (Data()->RefCount <= 1) { for (j = 0; i <= max; ++j, ++i) @@ -666,6 +670,10 @@ void FString::StripLeft (const char *charset) if (!strchr (charset, Chars[i])) break; } + if (i == 0) + { // Nothing to strip. + return; + } if (Data()->RefCount <= 1) { for (j = 0; i <= max; ++j, ++i) @@ -686,11 +694,16 @@ void FString::StripLeft (const char *charset) void FString::StripRight () { size_t max = Len(), i; - for (i = max; i-- > 0; ) + if (max == 0) return; + for (i = --max; i-- > 0; ) { if (!isspace(Chars[i])) break; } + if (i == max) + { // Nothing to strip. + return; + } if (Data()->RefCount <= 1) { Chars[i+1] = '\0'; @@ -714,11 +727,15 @@ void FString::StripRight (const char *charset) { size_t max = Len(), i; if (max == 0) return; - for (i = max; i-- > 0; ) + for (i = --max; i-- > 0; ) { if (!strchr (charset, Chars[i])) break; } + if (i == max) + { // Nothing to strip. + return; + } if (Data()->RefCount <= 1) { Chars[i+1] = '\0'; @@ -747,6 +764,10 @@ void FString::StripLeftRight () if (!isspace(Chars[j])) break; } + if (i == 0 && j == max - 1) + { // Nothing to strip. + return; + } if (Data()->RefCount <= 1) { for (k = 0; i <= j; ++i, ++k) @@ -759,8 +780,8 @@ void FString::StripLeftRight () else { FStringData *old = Data(); - AllocBuffer (j - i); - StrCopy (Chars, old->Chars(), j - i); + AllocBuffer(j - i + 1); + StrCopy(Chars, old->Chars(), j - i + 1); old->Release(); } }