Game Development Community

T3D 1.1 Final - trim Console Command doesn't work correctly - RESOLVED (THREED-2513)

by Nathan Bowhay - ESAL · in Torque 3D Professional · 08/31/2011 (5:02 pm) · 3 replies

Build: 1.1 Final (possibly others)
Platform: Windows Vista
Target: trim Console Function

Issues: when you call trim on a string with a space before and after it, the string isn't completely trimmed (space after still there). This is a bug because it should trim both the beginning and ending spaces as the description is: Remove leading and trailing whitespace from the string. and the example is: trim( " string " ); // Returns "string"., which doesn't actually work as explained.

Steps to Repeat:
1. Launch Torque
2. Open the console by pressing Tilda or ~
3. In the console type:
%test = trim(" string ");echo(%test @ "|");
4. You will notice it prints "string |". This same result happens when doing it any place else in script as well.

Suggested Fix:
Looks to be a copy/paste error from ltrim & rtrim.
In consoleFunctions.cpp around line 476 in the console function trim change the str in the second while loop to ptr. So change:
DefineConsoleFunction( trim, const char*, ( const char* str ),,
   "Remove leading and trailing whitespace from the string.n"
   "@param str A string.n"
   "@return A string that is the same as @a str but with any leading (i.e. leftmost) and trailing (i.e. rightmost) whitespace removed.nn"
   "@tsexamplen"
   "trim( "   string  " ); // Returns "string".n"
   "@endtsexamplen"
   "@ingroup Strings" )
{
   const char *ptr = str;
   while(*ptr == ' ' || *ptr == 'n' || *ptr == 't')
      ptr++;
   S32 firstWhitespace = 0;
   S32 pos = 0;
   while(str[pos])
   {
      if(str[pos] != ' ' && str[pos] != 'n' && str[pos] != 't')
         firstWhitespace = pos + 1;
      pos++;
   }
   char *ret = Con::getReturnBuffer(firstWhitespace + 1);
   dStrncpy(ret, ptr, firstWhitespace);
   ret[firstWhitespace] = 0;
   return ret;
}
to
DefineConsoleFunction( trim, const char*, ( const char* str ),,
   "Remove leading and trailing whitespace from the string.n"
   "@param str A string.n"
   "@return A string that is the same as @a str but with any leading (i.e. leftmost) and trailing (i.e. rightmost) whitespace removed.nn"
   "@tsexamplen"
   "trim( "   string  " ); // Returns "string".n"
   "@endtsexamplen"
   "@ingroup Strings" )
{
   const char *ptr = str;
   while(*ptr == ' ' || *ptr == 'n' || *ptr == 't')
      ptr++;
   S32 firstWhitespace = 0;
   S32 pos = 0;
   while(ptr[pos])
   {
      if(ptr[pos] != ' ' && ptr[pos] != 'n' && ptr[pos] != 't')
         firstWhitespace = pos + 1;
      pos++;
   }
   char *ret = Con::getReturnBuffer(firstWhitespace + 1);
   dStrncpy(ret, ptr, firstWhitespace);
   ret[firstWhitespace] = 0;
   return ret;
}

#1
08/31/2011 (6:35 pm)
The fix for this problem is to change the following in the function
DefineConsoleFunction( trim, const char*, ( const char* str ),,
of source file Engine/source/console/consoleFunctions.cpp
and on line 331 (assuming Torque 3D 1.1 Final) from:
// ignore this comment
   while(str[pos])
   {
      if(str[pos] != ' ' && str[pos] != '\n' && str[pos] != '\t')

to this:
// ignore this comment
   while(ptr[pos])
   {
      if(ptr[pos] != ' ' && ptr[pos] != '\n' && ptr[pos] != '\t')

I added the // ignore this comment bit to prevent the code tags from removing the indentation.

Proof of fix:
==>echo("\"" @ trim("   Hello   World    ") @ "\"" );
"Hello   World"
==>echo("\"" @ trim("Hello   World    ") @ "\"" );
"Hello   World"
==>echo("\"" @ trim("   Hello   World") @ "\"" );
"Hello   World"
==>echo("\"" @ trim(" Hello   World ") @ "\"" );
"Hello   World"

Edited: Forgot to mention what source file to edit. 2nd put backslashes back into my code above.
#2
09/01/2011 (9:14 am)
Thanks Nathan and Nathan. I've gone ahead and logged the bug and fix under THREED-2513.
#3
10/11/2011 (10:17 am)
Fixed in 1.2