C-Runtime library: string trim函数
以前,一直以为c runtime库中是没有string trim之类的函数的(我的前任留下的代码中,大佬们总是自己写一个:),其实,c runtime中是有string trim的函数的,那就是strspn!
用法:
p = string1 + strspn(string1, " /t");
printf("%s", p);
char * string2 = "abcdefghijklmn<...>abcdefghijklmn";
printf("%s", strpbrk(string2, "<>"));
好简单!
strcspn, strpbrk, strtok都是非常好用的函数,可惜以前没有好好地读过C函数参考手册,于是今天自己写了个与strspn功能完全相同的函数,还洋洋得意其中用到的技巧呢,汗!
另外,C++ STL::string支持以上的几个函数,只是名称不同。但是,C99、MS均不支持strrstr之类的函数,只能自己写一个啦!
顺便说一下,sscanf这个函数也是非常地好用!
今天研究了一下strspn, strpbrk的实现,其中的算法还真有意思,模仿其中的代码,写下了如下两个对我的项目来说非常方便的函数,分享一下:
char * xstrpspn(const char *string, const char * end_ptr, const char *control)
{
const unsigned char *tail = (const unsigned char *)end_ptr;
const unsigned char *str = (const unsigned char *)string;
const unsigned char *ctrl = (const unsigned char *)control;
unsigned char map[32];
int count;
int backward;
// Clear out bit map
for (count = 0; count < 32; count++) {
map[count] = 0;
}
// Set bits in control map
while (*ctrl) {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
ctrl++;
}
// 1st char NOT in control map stops search
if (*str) {
backward = (str <= tail || NULL == tail) ? 0 : 1;
count = 0;
while (str != tail && (map[*str >> 3] & (1 << (*str & 7))) ) {
if(backward){ str--; }else{ str++; }
}
}
return((char*)str);
}
char * xstrpbrk(const char *string, const char *end_ptr, const char *control)
{
const unsigned char *tail = (const unsigned char *)end_ptr;
const unsigned char *str = (const unsigned char *)string;
const unsigned char *ctrl = (const unsigned char *)control;
unsigned char map[32];
int count;
int backward;
// Clear out bit map
for (count = 0; count < 32; count++) {
map[count] = 0;
}
// Set bits in control map
while (*ctrl) {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
ctrl++;
}
// 1st char in control map stops search
backward = (str <= tail || NULL == tail) ? 0 : 1;
while (*str && str != tail)
{
if ( (map[*str >> 3] & (1 << (*str & 7))) ){
return((char *)str);
}
if(backward){ str--; }else{ str++; }
}
return NULL;
}
以前,一直以为c runtime库中是没有string trim之类的函数的(我的前任留下的代码中,大佬们总是自己写一个:),其实,c runtime中是有string trim的函数的,那就是strspn!
用法:
p = string1 + strspn(string1, " /t");
printf("%s", p);
char * string2 = "abcdefghijklmn<...>abcdefghijklmn";
printf("%s", strpbrk(string2, "<>"));
好简单!
strcspn, strpbrk, strtok都是非常好用的函数,可惜以前没有好好地读过C函数参考手册,于是今天自己写了个与strspn功能完全相同的函数,还洋洋得意其中用到的技巧呢,汗!
另外,C++ STL::string支持以上的几个函数,只是名称不同。但是,C99、MS均不支持strrstr之类的函数,只能自己写一个啦!
顺便说一下,sscanf这个函数也是非常地好用!
今天研究了一下strspn, strpbrk的实现,其中的算法还真有意思,模仿其中的代码,写下了如下两个对我的项目来说非常方便的函数,分享一下:
char * xstrpspn(const char *string, const char * end_ptr, const char *control)
{
const unsigned char *tail = (const unsigned char *)end_ptr;
const unsigned char *str = (const unsigned char *)string;
const unsigned char *ctrl = (const unsigned char *)control;
unsigned char map[32];
int count;
int backward;
// Clear out bit map
for (count = 0; count < 32; count++) {
map[count] = 0;
}
// Set bits in control map
while (*ctrl) {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
ctrl++;
}
// 1st char NOT in control map stops search
if (*str) {
backward = (str <= tail || NULL == tail) ? 0 : 1;
count = 0;
while (str != tail && (map[*str >> 3] & (1 << (*str & 7))) ) {
if(backward){ str--; }else{ str++; }
}
}
return((char*)str);
}
char * xstrpbrk(const char *string, const char *end_ptr, const char *control)
{
const unsigned char *tail = (const unsigned char *)end_ptr;
const unsigned char *str = (const unsigned char *)string;
const unsigned char *ctrl = (const unsigned char *)control;
unsigned char map[32];
int count;
int backward;
// Clear out bit map
for (count = 0; count < 32; count++) {
map[count] = 0;
}
// Set bits in control map
while (*ctrl) {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
ctrl++;
}
// 1st char in control map stops search
backward = (str <= tail || NULL == tail) ? 0 : 1;
while (*str && str != tail)
{
if ( (map[*str >> 3] & (1 << (*str & 7))) ){
return((char *)str);
}
if(backward){ str--; }else{ str++; }
}
return NULL;
}