Game Development Community

NextToken() skips empty tokens

by Orion Elenzil · in Torque Game Engine · 11/03/2006 (2:30 pm) · 1 replies

Pretty sure this qualifies as a bug and not a feature:


good, i would expect this:
echo(NextToken("foo:x:bar",0, ":"));
x:bar

bad!
echo(NextToken("foo::bar",0, ":"));
bar


this means if you can't have empty tokens.

i think i'll submit a fix for this.

#1
11/03/2006 (4:14 pm)
// just like nextToken except doesn't collapse delimiter runs.
ConsoleFunction(GoodNextToken,const char *,4,4,"GoodNextToken(str,token,delim)")
{
   argc;

   char *str = (char *) argv[1];
   const char *token = argv[2];
   const char *delim = argv[3];

   if (str)
   {
      // skip over any characters that are NOT a member of delim
      const char *tmp = str;

      while (*str && !isInSet(*str, delim))
         str++;

      // terminate the token
      if (*str)
         *str++ = 0;

      // set local variable if inside a function
      if (gEvalState.stack.size())
         Con::setLocalVariable(token,tmp);
      else
         Con::setVariable(token,tmp);
   }

   return str;
}