Skip to content

Commit d11ecb9

Browse files
authored
[GEN][ZH] Simplify repeated calls to removeLastChar with new truncate and trim methods (#1257)
1 parent 68576e7 commit d11ecb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+512
-236
lines changed

Generals/Code/GameEngine/Include/Common/AsciiString.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ class AsciiString
228228
*/
229229
void trim( void );
230230

231+
/**
232+
Remove trailing whitespace from the string.
233+
*/
234+
void trimEnd(void);
235+
236+
/**
237+
Remove all consecutive occurances of c from the end of the string.
238+
*/
239+
void trimEnd(const char c);
240+
231241
/**
232242
Make the string lowercase
233243
*/
@@ -240,6 +250,18 @@ class AsciiString
240250
*/
241251
void removeLastChar();
242252

253+
/**
254+
Remove the final charCount characters in the string. If the string is empty,
255+
do nothing.
256+
*/
257+
void truncateBy(const Int charCount);
258+
259+
/**
260+
Truncate the string to a length of maxLength characters, not including null termination,
261+
by removing from the end. If the string is empty or shorter than maxLength, do nothing.
262+
*/
263+
void truncateTo(const Int maxLength);
264+
243265
/**
244266
Analogous to sprintf() -- this formats a string according to the
245267
given sprintf-style format string (and the variable argument list)

Generals/Code/GameEngine/Include/Common/UnicodeString.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,35 @@ class UnicodeString
228228
*/
229229
void trim( void );
230230

231+
/**
232+
Remove trailing whitespace from the string.
233+
*/
234+
void trimEnd(void);
235+
236+
/**
237+
Remove all consecutive occurances of c from the end of the string.
238+
*/
239+
void trimEnd(const WideChar c);
240+
231241
/**
232242
Remove the final character in the string. If the string is empty,
233243
do nothing. (This is a rather dorky method, but used a lot in
234244
text editing, thus its presence here.)
235245
*/
236246
void removeLastChar();
237247

248+
/**
249+
Remove the final charCount characters in the string. If the string is empty,
250+
do nothing.
251+
*/
252+
void truncateBy(const Int charCount);
253+
254+
/**
255+
Truncate the string to a length of maxLength characters, not including null termination,
256+
by removing from the end. If the string is empty or shorter than maxLength, do nothing.
257+
*/
258+
void truncateTo(const Int maxLength);
259+
238260
/**
239261
Analogous to sprintf() -- this formats a string according to the
240262
given sprintf-style format string (and the variable argument list)

Generals/Code/GameEngine/Include/GameClient/DisplayString.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class DisplayString : public MemoryPoolObject
100100
virtual void setClipRegion( IRegion2D *region ); ///< clip text in this region
101101

102102
virtual void removeLastChar( void ); ///< remove the last character
103+
virtual void truncateBy(const Int charCount); ///< remove the last charCount characters
104+
virtual void truncateTo(const Int maxLength); ///< remove characters, if needed, until the string is maxLength long excluding null terminator
105+
103106
virtual void appendChar( WideChar c ); ///< append character to end
104107

105108
DisplayString *next( void ); ///< return next string

Generals/Code/GameEngine/Source/Common/CommandLine.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ static void ConvertShortMapPathToLongMapPath(AsciiString &mapName)
9595
DEBUG_CRASH(("Invalid map name %s", mapName.str()));
9696
}
9797
// remove the .map from the end.
98-
token.removeLastChar();
99-
token.removeLastChar();
100-
token.removeLastChar();
101-
token.removeLastChar();
98+
token.truncateBy(4);
10299

103100
actualpath.concat(token);
104101
actualpath.concat('\\');

Generals/Code/GameEngine/Source/Common/GameEngine.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,9 +897,7 @@ void updateTGAtoDDS()
897897
}
898898

899899
// replace tga with dds
900-
filenameDDS.removeLastChar(); // a
901-
filenameDDS.removeLastChar(); // g
902-
filenameDDS.removeLastChar(); // t
900+
filenameDDS.truncateBy(3); // "tga"
903901
filenameDDS.concat("dds");
904902

905903
Bool needsToBeUpdated = FALSE;

Generals/Code/GameEngine/Source/Common/Recorder.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ void RecorderClass::cleanUpReplayFile( void )
297297
return;
298298

299299
AsciiString debugFname = fname;
300-
debugFname.removeLastChar();
301-
debugFname.removeLastChar();
302-
debugFname.removeLastChar();
300+
debugFname.truncateBy(3);
303301
debugFname.concat("txt");
304302
UnsignedInt fileSize = 0;
305303
FILE *fp = fopen(logFileName, "rb");

Generals/Code/GameEngine/Source/Common/StatsCollector.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,7 @@ void StatsCollector::createFileName( void )
317317
const char *fname = name.reverseFind('\\');
318318
if (fname)
319319
name = fname+1;
320-
name.removeLastChar(); // p
321-
name.removeLastChar(); // a
322-
name.removeLastChar(); // m
323-
name.removeLastChar(); // .
320+
name.truncateBy(4); // ".map"
324321
m_statsFileName.clear();
325322
#if defined(RTS_DEBUG)
326323
if (TheGlobalData->m_saveStats)

Generals/Code/GameEngine/Source/Common/System/AsciiString.cpp

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,52 @@ void AsciiString::trim()
289289
set(c);
290290
}
291291

292-
if (m_data) // another check, because the previous set() could erase m_data
292+
trimEnd();
293+
}
294+
validate();
295+
}
296+
297+
// -----------------------------------------------------
298+
void AsciiString::trimEnd()
299+
{
300+
validate();
301+
302+
if (m_data)
303+
{
304+
// Clip trailing white space from the string.
305+
const int len = strlen(peek());
306+
int index = len;
307+
while (index > 0 && isspace(getCharAt(index - 1)))
293308
{
294-
// Clip trailing white space from the string.
295-
int len = strlen(peek());
296-
for (int index = len-1; index >= 0; index--)
297-
{
298-
if (isspace(getCharAt(index)))
299-
{
300-
removeLastChar();
301-
}
302-
else
303-
{
304-
break;
305-
}
306-
}
309+
--index;
310+
}
311+
312+
if (index < len)
313+
{
314+
truncateTo(index);
315+
}
316+
}
317+
validate();
318+
}
319+
320+
// -----------------------------------------------------
321+
void AsciiString::trimEnd(const char c)
322+
{
323+
validate();
324+
325+
if (m_data)
326+
{
327+
// Clip trailing consecutive occurances of c from the string.
328+
const int len = strlen(peek());
329+
int index = len;
330+
while (index > 0 && getCharAt(index - 1) == c)
331+
{
332+
--index;
333+
}
334+
335+
if (index < len)
336+
{
337+
truncateTo(index);
307338
}
308339
}
309340
validate();
@@ -331,15 +362,42 @@ void AsciiString::toLower()
331362

332363
// -----------------------------------------------------
333364
void AsciiString::removeLastChar()
365+
{
366+
truncateBy(1);
367+
}
368+
369+
// -----------------------------------------------------
370+
void AsciiString::truncateBy(const Int charCount)
334371
{
335372
validate();
336-
if (m_data)
373+
if (m_data && charCount > 0)
337374
{
338-
int len = strlen(peek());
375+
const size_t len = strlen(peek());
339376
if (len > 0)
340377
{
341-
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
342-
peek()[len - 1] = 0;
378+
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
379+
size_t count = charCount;
380+
if (charCount > len)
381+
{
382+
count = len;
383+
}
384+
peek()[len - count] = 0;
385+
}
386+
}
387+
validate();
388+
}
389+
390+
// -----------------------------------------------------
391+
void AsciiString::truncateTo(const Int maxLength)
392+
{
393+
validate();
394+
if (m_data)
395+
{
396+
const size_t len = strlen(peek());
397+
if (len > maxLength)
398+
{
399+
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
400+
peek()[maxLength] = 0;
343401
}
344402
}
345403
validate();

Generals/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,8 @@ static void findHighFileNumber( AsciiString filename, void *userData )
408408

409409
// strip off the extension at the end of the filename
410410
AsciiString nameOnly = filename;
411-
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
412-
nameOnly.removeLastChar();
413-
411+
nameOnly.truncateBy( strlen( SAVE_GAME_EXTENSION ) );
412+
414413
// convert filename (which is only numbers) to a number
415414
Int fileNumber = atoi( nameOnly.str() );
416415

Generals/Code/GameEngine/Source/Common/System/UnicodeString.cpp

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,37 +241,95 @@ void UnicodeString::trim()
241241
set(c);
242242
}
243243

244-
if (m_data) // another check, because the previous set() could erase m_data
244+
trimEnd();
245+
}
246+
validate();
247+
}
248+
249+
// -----------------------------------------------------
250+
void UnicodeString::trimEnd()
251+
{
252+
validate();
253+
254+
if (m_data)
255+
{
256+
// Clip trailing white space from the string.
257+
const int len = wcslen(peek());
258+
int index = len;
259+
while (index > 0 && iswspace(getCharAt(index - 1)))
260+
{
261+
--index;
262+
}
263+
264+
if (index < len)
265+
{
266+
truncateTo(index);
267+
}
268+
}
269+
validate();
270+
}
271+
272+
// -----------------------------------------------------
273+
void UnicodeString::trimEnd(const WideChar c)
274+
{
275+
validate();
276+
277+
if (m_data)
278+
{
279+
// Clip trailing consecutive occurances of c from the string.
280+
const int len = wcslen(peek());
281+
int index = len;
282+
while (index > 0 && getCharAt(index - 1) == c)
283+
{
284+
--index;
285+
}
286+
287+
if (index < len)
245288
{
246-
// Clip trailing white space from the string.
247-
int len = wcslen(peek());
248-
for (int index = len-1; index >= 0; index--)
289+
truncateTo(index);
290+
}
291+
}
292+
validate();
293+
}
294+
295+
// -----------------------------------------------------
296+
void UnicodeString::removeLastChar()
297+
{
298+
truncateBy(1);
299+
}
300+
301+
// -----------------------------------------------------
302+
void UnicodeString::truncateBy(const Int charCount)
303+
{
304+
validate();
305+
if (m_data && charCount > 0)
306+
{
307+
const size_t len = wcslen(peek());
308+
if (len > 0)
309+
{
310+
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
311+
size_t count = charCount;
312+
if (charCount > len)
249313
{
250-
if (iswspace(getCharAt(index)))
251-
{
252-
removeLastChar();
253-
}
254-
else
255-
{
256-
break;
257-
}
314+
count = len;
258315
}
316+
peek()[len - count] = 0;
259317
}
260318
}
261319
validate();
262320
}
263321

264322
// -----------------------------------------------------
265-
void UnicodeString::removeLastChar()
323+
void UnicodeString::truncateTo(const Int maxLength)
266324
{
267325
validate();
268326
if (m_data)
269327
{
270-
int len = wcslen(peek());
271-
if (len > 0)
328+
const size_t len = wcslen(peek());
329+
if (len > maxLength)
272330
{
273-
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
274-
peek()[len - 1] = 0;
331+
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
332+
peek()[maxLength] = 0;
275333
}
276334
}
277335
validate();

0 commit comments

Comments
 (0)