Skip to content

[GEN][ZH] Simplify repeated calls to removeLastChar with new truncate and trim methods #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Generals/Code/GameEngine/Include/Common/AsciiString.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ class AsciiString
*/
void trim( void );

/**
Remove trailing whitespace from the string.
*/
void trimEnd(void);

/**
Remove all consecutive occurances of c from the end of the string.
*/
void trimEnd(const char c);

/**
Make the string lowercase
*/
Expand All @@ -240,6 +250,18 @@ class AsciiString
*/
void removeLastChar();

/**
Remove the final charCount characters in the string. If the string is empty,
do nothing.
*/
void truncateBy(const Int charCount);

/**
Truncate the string to a length of maxLength characters, not including null termination,
by removing from the end. If the string is empty or shorter than maxLength, do nothing.
*/
void truncateTo(const Int maxLength);

/**
Analogous to sprintf() -- this formats a string according to the
given sprintf-style format string (and the variable argument list)
Expand Down
22 changes: 22 additions & 0 deletions Generals/Code/GameEngine/Include/Common/UnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,35 @@ class UnicodeString
*/
void trim( void );

/**
Remove trailing whitespace from the string.
*/
void trimEnd(void);

/**
Remove all consecutive occurances of c from the end of the string.
*/
void trimEnd(const WideChar c);

/**
Remove the final character in the string. If the string is empty,
do nothing. (This is a rather dorky method, but used a lot in
text editing, thus its presence here.)
*/
void removeLastChar();

/**
Remove the final charCount characters in the string. If the string is empty,
do nothing.
*/
void truncateBy(const Int charCount);

/**
Truncate the string to a length of maxLength characters, not including null termination,
by removing from the end. If the string is empty or shorter than maxLength, do nothing.
*/
void truncateTo(const Int maxLength);

/**
Analogous to sprintf() -- this formats a string according to the
given sprintf-style format string (and the variable argument list)
Expand Down
3 changes: 3 additions & 0 deletions Generals/Code/GameEngine/Include/GameClient/DisplayString.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class DisplayString : public MemoryPoolObject
virtual void setClipRegion( IRegion2D *region ); ///< clip text in this region

virtual void removeLastChar( void ); ///< remove the last character
virtual void truncateBy(const Int charCount); ///< remove the last charCount characters
virtual void truncateTo(const Int maxLength); ///< remove characters, if needed, until the string is maxLength long excluding null terminator

virtual void appendChar( WideChar c ); ///< append character to end

DisplayString *next( void ); ///< return next string
Expand Down
5 changes: 1 addition & 4 deletions Generals/Code/GameEngine/Source/Common/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ static void ConvertShortMapPathToLongMapPath(AsciiString &mapName)
DEBUG_CRASH(("Invalid map name %s", mapName.str()));
}
// remove the .map from the end.
token.removeLastChar();
token.removeLastChar();
token.removeLastChar();
token.removeLastChar();
token.truncateBy(4);

actualpath.concat(token);
actualpath.concat('\\');
Expand Down
4 changes: 1 addition & 3 deletions Generals/Code/GameEngine/Source/Common/GameEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,7 @@ void updateTGAtoDDS()
}

// replace tga with dds
filenameDDS.removeLastChar(); // a
filenameDDS.removeLastChar(); // g
filenameDDS.removeLastChar(); // t
filenameDDS.truncateBy(3); // "tga"
filenameDDS.concat("dds");

Bool needsToBeUpdated = FALSE;
Expand Down
4 changes: 1 addition & 3 deletions Generals/Code/GameEngine/Source/Common/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ void RecorderClass::cleanUpReplayFile( void )
return;

AsciiString debugFname = fname;
debugFname.removeLastChar();
debugFname.removeLastChar();
debugFname.removeLastChar();
debugFname.truncateBy(3);
debugFname.concat("txt");
UnsignedInt fileSize = 0;
FILE *fp = fopen(logFileName, "rb");
Expand Down
5 changes: 1 addition & 4 deletions Generals/Code/GameEngine/Source/Common/StatsCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,7 @@ void StatsCollector::createFileName( void )
const char *fname = name.reverseFind('\\');
if (fname)
name = fname+1;
name.removeLastChar(); // p
name.removeLastChar(); // a
name.removeLastChar(); // m
name.removeLastChar(); // .
name.truncateBy(4); // ".map"
m_statsFileName.clear();
#if defined(RTS_DEBUG)
if (TheGlobalData->m_saveStats)
Expand Down
94 changes: 76 additions & 18 deletions Generals/Code/GameEngine/Source/Common/System/AsciiString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,52 @@ void AsciiString::trim()
set(c);
}

if (m_data) // another check, because the previous set() could erase m_data
trimEnd();
}
validate();
}

// -----------------------------------------------------
void AsciiString::trimEnd()
{
validate();

if (m_data)
{
// Clip trailing white space from the string.
const int len = strlen(peek());
int index = len;
while (index > 0 && isspace(getCharAt(index - 1)))
{
// Clip trailing white space from the string.
int len = strlen(peek());
for (int index = len-1; index >= 0; index--)
{
if (isspace(getCharAt(index)))
{
removeLastChar();
}
else
{
break;
}
}
--index;
}

if (index < len)
{
truncateTo(index);
}
}
validate();
}

// -----------------------------------------------------
void AsciiString::trimEnd(const char c)
{
validate();

if (m_data)
{
// Clip trailing consecutive occurances of c from the string.
const int len = strlen(peek());
int index = len;
while (index > 0 && getCharAt(index - 1) == c)
{
--index;
}

if (index < len)
{
truncateTo(index);
}
}
validate();
Expand Down Expand Up @@ -331,15 +362,42 @@ void AsciiString::toLower()

// -----------------------------------------------------
void AsciiString::removeLastChar()
{
truncateBy(1);
}

// -----------------------------------------------------
void AsciiString::truncateBy(const Int charCount)
{
validate();
if (m_data)
if (m_data && charCount > 0)
{
int len = strlen(peek());
const size_t len = strlen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
peek()[len - 1] = 0;
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
size_t count = charCount;
if (charCount > len)
{
count = len;
}
peek()[len - count] = 0;
}
}
validate();
}

// -----------------------------------------------------
void AsciiString::truncateTo(const Int maxLength)
{
validate();
if (m_data)
{
const size_t len = strlen(peek());
if (len > maxLength)
{
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
peek()[maxLength] = 0;
}
}
validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ static void findHighFileNumber( AsciiString filename, void *userData )

// strip off the extension at the end of the filename
AsciiString nameOnly = filename;
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
nameOnly.removeLastChar();

nameOnly.truncateBy( strlen( SAVE_GAME_EXTENSION ) );

// convert filename (which is only numbers) to a number
Int fileNumber = atoi( nameOnly.str() );

Expand Down
92 changes: 75 additions & 17 deletions Generals/Code/GameEngine/Source/Common/System/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,37 +241,95 @@ void UnicodeString::trim()
set(c);
}

if (m_data) // another check, because the previous set() could erase m_data
trimEnd();
}
validate();
}

// -----------------------------------------------------
void UnicodeString::trimEnd()
{
validate();

if (m_data)
{
// Clip trailing white space from the string.
const int len = wcslen(peek());
int index = len;
while (index > 0 && iswspace(getCharAt(index - 1)))
{
--index;
}

if (index < len)
{
truncateTo(index);
}
}
validate();
}

// -----------------------------------------------------
void UnicodeString::trimEnd(const WideChar c)
{
validate();

if (m_data)
{
// Clip trailing consecutive occurances of c from the string.
const int len = wcslen(peek());
int index = len;
while (index > 0 && getCharAt(index - 1) == c)
{
--index;
}

if (index < len)
{
// Clip trailing white space from the string.
int len = wcslen(peek());
for (int index = len-1; index >= 0; index--)
truncateTo(index);
}
}
validate();
}

// -----------------------------------------------------
void UnicodeString::removeLastChar()
{
truncateBy(1);
}

// -----------------------------------------------------
void UnicodeString::truncateBy(const Int charCount)
{
validate();
if (m_data && charCount > 0)
{
const size_t len = wcslen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
size_t count = charCount;
if (charCount > len)
{
if (iswspace(getCharAt(index)))
{
removeLastChar();
}
else
{
break;
}
count = len;
}
peek()[len - count] = 0;
}
}
validate();
}

// -----------------------------------------------------
void UnicodeString::removeLastChar()
void UnicodeString::truncateTo(const Int maxLength)
{
validate();
if (m_data)
{
int len = wcslen(peek());
if (len > 0)
const size_t len = wcslen(peek());
if (len > maxLength)
{
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
peek()[len - 1] = 0;
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
peek()[maxLength] = 0;
}
}
validate();
Expand Down
11 changes: 2 additions & 9 deletions Generals/Code/GameEngine/Source/Common/UserPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,7 @@ Bool LadderPreferences::loadProfile( Int profileID )
continue;

p.port = atoi( ptr + 1 );
Int i=0;
for (; i<strlen(ptr); ++i)
{
ladName.removeLastChar();
}
ladName.truncateBy(strlen(ptr));
p.address = QuotedPrintableToAsciiString(ladName);

ptr = ladData.reverseFind(':');
Expand All @@ -815,10 +811,7 @@ Bool LadderPreferences::loadProfile( Int profileID )
continue;

p.lastPlayDate = atoi( ptr + 1 );
for (i=0; i<strlen(ptr); ++i)
{
ladData.removeLastChar();
}
ladData.truncateBy(strlen(ptr));
p.name = QuotedPrintableToUnicodeString(ladData);

m_ladders[p.lastPlayDate] = p;
Expand Down
Loading
Loading