strchrpos() and strrchrpos()
by Orion Elenzil · 02/06/2008 (8:26 am) · 1 comments
torquescript's strchr() function is the script-exposure of the standard C function strchr(), which returns a pointer to the first occurrence of a given character in a given string. in C, it's easy to get the index of that character via pointer arithmetic, but in torquescript it's slightly convoluted. so, strchrpos() returns the index of the the first occurrence of a given character in a given string, or -1 if it doesn't occur. strrchrpos() does the same, but use the word "last" instead of "first".
trivial, but might save someone a couple minutes someday.
should be applicable to all flavours of TG*.
it has literally taken me three times as long to write this description than to write the functions.
consoleFunctions.cc
after the console function ConsoleFunction(strrchr,const char *,3,3,"(string,char)"),
add these console functions:
trivial, but might save someone a couple minutes someday.
should be applicable to all flavours of TG*.
it has literally taken me three times as long to write this description than to write the functions.
consoleFunctions.cc
after the console function ConsoleFunction(strrchr,const char *,3,3,"(string,char)"),
add these console functions:
ConsoleFunction(strchrpos,S32,3,3,"(string,char)")
{
argc;
const char *ret = dStrchr(argv[1], argv[2][0]);
return ret ? ret - argv[1] : -1;
}
ConsoleFunction(strrchrpos,S32,3,3,"(string,char)")
{
argc;
const char *ret = dStrrchr(argv[1], argv[2][0]);
return ret ? ret - argv[1] : -1;
}About the author

Torque Owner Andrea Fraboni
C4DGames