Game Development Community

new console function: collapseWhiteSpace()

by Orion Elenzil · 03/04/2007 (7:42 am) · 0 comments

collapseWhiteSpace() reduces runs of whitespace to their first character.

note collapseWhiteSpace() does not trim leading or trailing spaces, it just collapsed them.

some examples:

(dry is the input,
exp is the expected output)

%dry = " ";
%exp = " ";

%dry = " x x xx ";
%exp = " x x xx ";

%dry = "";
%exp = "";

%dry = "x \t x";
%exp = "x x";

%dry = "x\t\t\t x";
%exp = "x\tx";

%dry = " x";
%exp = " x";

%dry = "xxx";
%exp = "xxx";


orion

consoleFunctions.cc
ConsoleFunction(collapseWhiteSpace, const char *,2,2,"(string)")
{
   char*       ret         = Con::getReturnBuffer(dStrlen(argv[1]) + 1);

   const char* p           = argv[1];
   char*       q           = ret;
   bool        prevIsSpace = false;
   bool        currIsSpace;

   while(*p != '[[4f3959260f489]]')
   {
      currIsSpace = (*p == ' ' || *p == '\n' || *p == '\t' || *p == '\r');
      if (!currIsSpace || !prevIsSpace)
      {
         *q = *p;
         q++;
      }

      prevIsSpace = currIsSpace;
      p++;
   }

   *q = *p;

   return ret;
}