The reason it's so fast is it's a C-function. When doing string manipulation in PAWN, it's best to use the native string functions as much as possible.
Anyways, I made this just now:
GetParam
Note: I have not fully tested this - only like 80% of the possible different inputs.
Am I the only one pissed off at strtok? Not sure why but it just annoys me.
Anyways, I made this function as a replacement. It's slightly faster than strtok, and easier to use.
Usage:
pawn Код:
GetParam( string[], param_num );
string is the string you want to look for params in, and
param_num is the index of the param you want,
starting at 1.
The function will seamlessly trim the string and return it.
Example:
pawn Код:
new params[] = " I would like to \t\t\nhave\n 50 bags of cheetos.\r\n";
printf( "1: \"%s\"", GetParam( params, 1 ) ); // Prints "I"
printf( "2: \"%s\"", GetParam( params, 2 ) ); // Prints "would"
printf( "3: \"%s\"", GetParam( params, 3 ) ); // Prints "like"
printf( "4: \"%s\"", GetParam( params, 4 ) ); // Prints "to"
printf( "5: \"%s\"", GetParam( params, 5 ) ); // Prints "have"
printf( "6: \"%s\"", GetParam( params, 6 ) ); // Prints "50"
printf( "7: \"%s\"", GetParam( params, 7 ) ); // Prints "bags"
printf( "8: \"%s\"", GetParam( params, 8 ) ); // Prints "of"
printf( "9: \"%s\"", GetParam( params, 9 ) ); // Prints "cheetos."
Code:
pawn Код:
stock GetParam( const szString[ ], iParam )
{
new iSearchPos, iPos, iHoles, szReturn[ 128 ], iFoundParam = -1;
if ( szString[ 0 ] <= 1 ) // Treat \1 as a null character for ZCMD/YCMD compatibility.
return szReturn; // If the string is empty there's no point in looking for stuff.
iPos = -1;
while ( 2 <= szString[ ++iPos ] <= ' ' ) {} // Trim the beginning of the string.
if ( --iParam == 0 ) // Wether or not we're looking for the first parameter.
{
if (
( iSearchPos = strfind( szString, " ", _, iPos ) ) != -1 // Search for a space/tab
|| ( iSearchPos = strfind( szString, "\t", _, iPos ) ) != -1
)
strcat( szReturn, szString[ iPos ], iSearchPos + 1 - iPos ); // Put the found part into the return.
else
strcat( szReturn, szString[ iPos ] ); // Put the whole thing (except for the first tabs/spaces, if any) into the return.
}
else
{
while (
( iSearchPos = strfind( szString, " ", _, iPos ) ) != -1 // Search for a space/tab
|| ( iSearchPos = strfind( szString, "\t", _, iPos ) ) != -1
)
{
back_in_action:
iPos = iSearchPos;
while ( 2 <= szString[ ++iPos ] <= ' ' ) {} // Skip any whitespace.
if ( iFoundParam != -1 ) // If the previous iteration found a param..
{
strcat( szReturn, szString[ iFoundParam ], iPos - iFoundParam ); // Put it into the return string.
iFoundParam = -1;
break; // Then BREAK YOSELF.
}
if ( ++iHoles == iParam ) // If this current "hole" is in front of the param we're looking for..
iFoundParam = iPos; // ..let the script know that.
}
if ( iFoundParam != -1 ) // If a param was found in the last iteration.. (end of the string)
{
iSearchPos = strlen( szString ); // Set the next position to the end of the string.
goto back_in_action;
}
}
if ( szReturn[ 0 ] )
{
// Trim any whitespace on the right side.
for ( iPos = strlen( szReturn ) - 1; iPos >= 0 && szReturn[ iPos ] <= ' '; iPos-- )
szReturn[ iPos ] = 0;
}
return szReturn;
}