memcached 之token_t

来源:互联网 发布:什么软件看股票最好 编辑:程序博客网 时间:2024/05/29 16:16
typedef struct token_s {    char *value;    size_t length;} token_t;
static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens) {    char *s, *e;    size_t ntokens = 0;    size_t len = strlen(command);    unsigned int i = 0;    assert(command != NULL && tokens != NULL && max_tokens > 1);    s = e = command;    for (i = 0; i < len; i++) {        if (*e == ' ') {            if (s != e) {                tokens[ntokens].value = s;                tokens[ntokens].length = e - s;                ntokens++;                *e = '\0';                if (ntokens == max_tokens - 1) {                    e++;                    s = e; /* so we don't add an extra token */                    break;                }            }            s = e + 1;        }        e++;    }    if (s != e) {
        //tokens指向下一个command字符串。        tokens[ntokens].value = s;        tokens[ntokens].length = e - s;        ntokens++;    }    /*     * If we scanned the whole string, the terminal value pointer is null,     * otherwise it is the first unprocessed character.     */    tokens[ntokens].value =  *e == '\0' ? NULL : e;    tokens[ntokens].length = 0;    ntokens++;    return ntokens;} 

value代表command中的字符串,length代表该字符串的长度。

example:

get foo ecy
 

value:get length:3

value:foo length:3

value:ecy length:3

value:NULL length:0

理由:

tokens[ntokens].value =  *e == '\0' ? NULL : e;

 

 

 

 

原创粉丝点击