Reduced code duplication in FString class

Moved resetting of string data to null value into own function
This commit is contained in:
alexey.lysiuk 2017-10-23 11:23:30 +03:00
parent 3cefe10818
commit 7d97963005
2 changed files with 12 additions and 11 deletions

View file

@ -66,8 +66,7 @@ FString::FString (const char *copyStr)
{ {
if (copyStr == NULL || *copyStr == '\0') if (copyStr == NULL || *copyStr == '\0')
{ {
NullString.RefCount++; ResetToNull();
Chars = &NullString.Nothing[0];
} }
else else
{ {
@ -87,8 +86,7 @@ FString::FString (char oneChar)
{ {
if (oneChar == '\0') if (oneChar == '\0')
{ {
NullString.RefCount++; ResetToNull();
Chars = &NullString.Nothing[0];
} }
else else
{ {
@ -220,8 +218,7 @@ FString &FString::operator = (const char *copyStr)
if (copyStr == NULL || *copyStr == '\0') if (copyStr == NULL || *copyStr == '\0')
{ {
Data()->Release(); Data()->Release();
NullString.RefCount++; ResetToNull();
Chars = &NullString.Nothing[0];
} }
else else
{ {
@ -362,8 +359,7 @@ FString &FString::CopyCStrPart(const char *tail, size_t tailLen)
else else
{ {
Data()->Release(); Data()->Release();
NullString.RefCount++; ResetToNull();
Chars = &NullString.Nothing[0];
} }
return *this; return *this;
} }
@ -373,8 +369,7 @@ void FString::Truncate(size_t newlen)
if (newlen == 0) if (newlen == 0)
{ {
Data()->Release(); Data()->Release();
NullString.RefCount++; ResetToNull();
Chars = &NullString.Nothing[0];
} }
else if (newlen < Len()) else if (newlen < Len())
{ {

View file

@ -120,7 +120,7 @@ enum ELumpNum
class FString class FString
{ {
public: public:
FString () : Chars(&NullString.Nothing[0]) { NullString.RefCount++; } FString () { ResetToNull(); }
// Copy constructors // Copy constructors
FString (const FString &other) { AttachToOther (other); } FString (const FString &other) { AttachToOther (other); }
@ -311,6 +311,12 @@ protected:
const FStringData *Data() const { return (FStringData *)Chars - 1; } const FStringData *Data() const { return (FStringData *)Chars - 1; }
FStringData *Data() { return (FStringData *)Chars - 1; } FStringData *Data() { return (FStringData *)Chars - 1; }
void ResetToNull()
{
NullString.RefCount++;
Chars = &NullString.Nothing[0];
}
void AttachToOther (const FString &other); void AttachToOther (const FString &other);
void AllocBuffer (size_t len); void AllocBuffer (size_t len);
void ReallocBuffer (size_t newlen); void ReallocBuffer (size_t newlen);