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:
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:
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;
}toDefineConsoleFunction( 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;
}
Torque Owner Nathan Martin
TRON 2001 Network
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.