向前辈致敬 strspn

把8位的CHAR型数据分解为:前5位和后3位,这样2^5 = 32个CHAR型数+值就可表示所有的CHAR型数据

这样做的好处:在给出子串后,不用比较256次,最多比较32次即可判断出是否一个数在子串中

/***

*int strspn(string, control) - find init substring of control chars

*

*Purpose:

*       Finds the index of the first character in string that does belong

*       to the set of characters specified by control.  This is

*       equivalent to the length of the initial substring of string that

*       consists entirely of characters from control.  The '\0' character

*       that terminates control is not considered in the matching process.

*

*Entry:

*       char *string - string to search

*       char *control - string containing characters not to search for

*

*Exit:

*       returns index of first char in string not in control

*

*Exceptions:

*

*******************************************************************************/

int strspn(const char * string, const char * control)

{

    unsigned char map[32] = {0};

    size_t count;



    printf("lx_strspn %s control %s\r\n", string, control);

    while(*control != 0)

    {

        map[*control >> 3] |= (1 << (*control & 7));

        control++;

    }



    count = 0;

    while(map[*string >>3] & (1 << (*string & 7)))

    {

        printf("%d %d %d \r\n", *string >>3, *string & 7, map[*string >>3]);

        count++;

        string++;

    }



    return count;

}



你可能感兴趣的:(tr)