這是如何工作的?C Tokenizer - 它是如何工作的?
我知道使用它,你在傳:
- 開始:字符串(如 「第1項,第2項,第3項」)
- DELIM:分隔字符串(如 「」)
- TOK:參照一個字符串,將持有令牌
- nextpos(可選):參照在原始字符串的位置的下一個標記開始
- sdelim(可選):指針,其將保存的字符開始toke的分隔符ñ
- edelim(可選):指向將持有令牌
代碼的結束分隔符字符:
#include <stdlib.h>
#include <string.h>
int token(char* start, char* delim, char** tok, char** nextpos, char* sdelim, char* edelim) {
// Find beginning:
int len = 0;
char *scanner;
int dictionary[8];
int ptr;
for(ptr = 0; ptr < 8; ptr++) {
dictionary[ptr] = 0;
}
for(; *delim; delim++) {
dictionary[*delim/32] |= 1 << *delim % 32;
}
if(sdelim) {
*sdelim = 0;
}
for(; *start; start++) {
if(!(dictionary[*start/32] & 1 << *start % 32)) {
break;
}
if(sdelim) {
*sdelim = *start;
}
}
if(*start == 0) {
if(nextpos != NULL) {
*nextpos = start;
}
*tok = NULL;
return 0;
}
for(scanner = start; *scanner; scanner++) {
if(dictionary[*scanner/32] & 1 << *scanner % 32) {
break;
}
len++;
}
if(edelim) {
*edelim = *scanner;
}
if(nextpos != NULL) {
*nextpos = scanner;
}
*tok = (char*)malloc(sizeof(char) * (len + 1));
if(*tok == NULL) {
return 0;
}
memcpy(*tok, start, len);
*(*tok + len) = 0;
return len + 1;
}
我得到的大部分,除了:
dictionary[*delim/32] |= 1 << *delim % 32;
and
dictionary[*start/32] & 1 << *start % 32
它是神奇的嗎?
你爲什麼覺得它的魔力? – ChaosPandion 2010-07-30 14:39:51
我知道這兩行做一些事情來找到分隔符,但它沒有通過分隔符字符串循環? – 2010-07-30 14:42:34