Skip to content

Instantly share code, notes, and snippets.

@ecere
Created June 13, 2013 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecere/5771515 to your computer and use it in GitHub Desktop.
Save ecere/5771515 to your computer and use it in GitHub Desktop.
public int Tokenize(char * string, int maxTokens, char* tokens[], BackSlashEscaping esc)
{
int count = 0;
bool quoted = false;
byte * start = null;
bool escaped = false;
char * output = string;
for(; *string && count < maxTokens; string++, output++)
{
if(output != string)
*output = *string;
if(start)
{
if(escaped)
{
escaped = false;
output--;
// ADDED THIS HERE...
if(output != string)
*output = *string;
}
else if(*string == '\\' && (esc == true ||
(esc == forArgsPassing && (*(string+1) == '\"' || *(string+1) == ' '))))
escaped = true;
else if(*string == '\"')
{
if(quoted)
{
output--;
// *output = '\0';
quoted = false;
}
else
{
/*memmove(start + 1, start, string - (char *)start);
start++;*/
quoted = true;
output--;
}
}
else if(*string == ' ' && !quoted)
{
tokens[count++] = start;
*output = '\0';
start = null;
}
}
else if(*string != ' ')
{
if(*string == '\"')
{
quoted = true;
start = output + 1;
}
else
{
start = output;
if(*string == '\\' && (esc == true ||
(esc == forArgsPassing && (*(string+1) == '\"' || *(string+1) == ' '))))
escaped = true;
}
}
}
if(start && count < maxTokens)
{
tokens[count++] = start;
*output = '\0';
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment