Index: memcached.c =================================================================== --- memcached.c (revision 499) +++ memcached.c (working copy) @@ -770,46 +770,36 @@ typedef struct token_s { * command = tokens[ix].value; * } */ -static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { - char *cp; - char *value = NULL; - size_t length = 0; +static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) { + char *s, *e; size_t ntokens = 0; - assert(command != NULL && tokens != NULL && max_tokens > 1); - - cp = command; - while(*cp != '\0' && ntokens < max_tokens - 1) { - if(*cp == ' ') { - // If we've accumulated a token, this is the end of it. - if(length > 0) { - tokens[ntokens].value = value; - tokens[ntokens].length = length; + for (s = e = command; ntokens < max_tokens - 1; ++e) { + if (*e == ' ') { + if (s != e) { + tokens[ntokens].value = s; + tokens[ntokens].length = e - s; ntokens++; - length = 0; - value = NULL; - } - *cp = '\0'; - } else { - if(length == 0) { - value = cp; + *e = '\0'; } - length++; + s = e + 1; } - cp++; - } + else if (*e == '\0') { + if (s != e) { + tokens[ntokens].value = s; + tokens[ntokens].length = e - s; + ntokens++; + } - if(ntokens < max_tokens - 1 && length > 0) { - tokens[ntokens].value = value; - tokens[ntokens].length = length; - ntokens++; + break; /* string end */ + } } /* * If we scanned the whole string, the terminal value pointer is null, * otherwise it is the first unprocessed character. */ - tokens[ntokens].value = *cp == '\0' ? NULL : cp; + tokens[ntokens].value = *e == '\0' ? NULL : e; tokens[ntokens].length = 0; ntokens++;