读书人

项目开发中有用的算法(1)

发布时间: 2012-09-05 15:19:34 作者: rapoo

项目开发中有用的算法(一)
[/color][color=green]//////////////////////////////////////////////////////////////////////////////////////////
//
//功能: 接照指定分隔符拆分字符串.
//
//Parameters:
//char *str => 待拆分字符中
//char *sep => 分隔符,即拆分的条件
//int max_strs => 返回的最大子串数目
//int *toks => place to store the number of tokens found in str
//char meta => the "escape metacharacter", treat the character
//after this character as a literal and "escape" a
//seperator
//
//Returns:
//2D char array with one token per "row" of the returnedarray.
//
//////////////////////////////////////////////////////////////////////////////////////////
char **mSplit(char *str, char *sep, int max_strs, int *toks, char meta)
{
char **retstr; // 2D array which is returned to caller
char *idx; // index pointer into str
char *end; // ptr to end of str
char *sep_end; // ptr to end of seperator string
char *sep_idx; // index ptr into seperator string
int len = 0; // length of current token string
int curr_str = 0; // current index into the 2D return array
char last_char = (char) 0xFF;

//对toks、str做点合法性检查
if(!toks) return NULL;

*toks = 0;

if (!str) return NULL;

//
//find the ends of the respective passed strings so our while() loops
//know where to stop
//
sep_end = sep + strlen(sep);//设置分隔符的结尾位置
end = str + strlen(str);//设置待分隔字符串的结尾位置

// remove trailing whitespace
while(isspace((int) *(end - 1)) && ((end - 1) >= str))//去除字符串末尾空格
*(--end) = '\0'; // -1 because of NULL

// set our indexing pointers
sep_idx = sep;//设置分隔符字符串索引
idx = str;//设置字符串索引指针

//
//alloc space for the return string, this is where the pointers to the
// tokens will be stored
//
if((retstr = (char **) malloc((sizeof(char **) * max_strs))) == NULL)//为返回的二维字符串指针数组分配内存空间
return NULL;

max_strs--;

// loop thru each letter in the string being tokenized
while(idx < end)//遍历字符串中的每一个字符
{
// loop thru each seperator string char
while(sep_idx < sep_end)//当分隔符字符串指针末到该字符串结尾时循环
{
//
// if the current string-indexed char matches the current
// seperator char...
//
if((*idx == *sep_idx) && (last_char != meta))//当前字符串中的字符匹配当前分隔符
{
// if there's something to store...
if(len > 0)//如果非分隔符的字符数目大于0
{
if(curr_str <= max_strs)//如果当前经过分隔处理后所得的字符串数目不大于指定的最大返回数目,实际上是最大返回数-1
{
// allocate space for the new token
if((retstr[curr_str] = (char *)//为当前二维字符串指针数组元素分配空间
malloc((sizeof(char) * len) + 1)) == NULL)
{
return NULL;
}

// copy the token into the return string array
memcpy(retstr[curr_str], (idx - len), len);//将对应长度的子字符串拷贝到返回的二维字符串数组中
retstr[curr_str][len] = 0;

// twiddle the necessary pointers and vars
len = 0;//重新设置相应的变量值和指针。返回的标识字符串数目加1
curr_str++;

last_char = *idx;
idx++;
}

//
// if we've gotten all the tokens requested, return the
// list
//
if(curr_str >= max_strs)//如果当前经过分隔处理后所得的字符串数目不小于指定的最大返回数目,即我们已经获得了所需的所有标识字符串
{
while(isspace((int) *idx))//当前字符串指针跳过空格符
idx++;

len = (int)(end - idx);//设置拷贝长度值

if((retstr[curr_str] = (char *)malloc((sizeof(char) * len) + 1)) == NULL)//为返回二维字符串指针数组的当前元素分配对应的内存空间
return NULL;

memcpy(retstr[curr_str], idx, len);//拷贝对应长度的子字符串到返回的二维字符串数组中
retstr[curr_str][len] = 0;

*toks = curr_str + 1;//设置返回的标识字符串数目值

return retstr;//返回二维字符串数组指针
}
}
else
//
// otherwise, the previous char was a seperator as well,
// and we should just continue
//
{
last_char = *idx;
idx++;
// make sure to reset this so we test all the sep. chars
sep_idx = sep;
len = 0;
}
}
else
{
// go to the next seperator
sep_idx++;
}
}

sep_idx = sep;//重新设置分隔符字符串指针的指向的起始位置
len++;//递增非分隔符的字符数目
last_char = *idx;
idx++;//递增字符串指针
}

// put the last string into the list

if(len > 0)
{
if((retstr[curr_str] = (char *)malloc((sizeof(char) * len) + 1)) == NULL)//分配空间
return NULL;

memcpy(retstr[curr_str], (idx - len), len);//拷贝
retstr[curr_str][len] = 0;

*toks = curr_str + 1;//设置返回的标识字符串数目值
}

// return the token list
return retstr;
}

读书人网 >编程

热点推荐