首页 > 其他 > 详细

strlen实现

时间:2017-04-12 04:16:27      阅读:143      评论:0      收藏:0      [点我收藏+]
size_t my_strlen(const char* str)
{
    const char* ptr = str;
    for (; ((int)ptr & 0x03) != 0; ++ptr)
    {
        if (*ptr == \0)
            return ptr - str;
    }

    unsigned int* ptr_d = (unsigned int*)ptr;
    unsigned int magic = 0x7efefeff;
    //01111110    11111110    11111110     11111111 

    while (true)
    {
        unsigned int bits32 = *ptr_d++;
        if ((((bits32 + magic) ^ (bits32 ^ -1)) & ~magic) != 0) // bits32 ^ -1 等价于 ~bits32
        {
            ptr = (const char*)(ptr_d - 1);
            if (ptr[0] == 0)
                return ptr - str;
            if (ptr[1] == 0)
                return ptr - str + 1;
            if (ptr[2] == 0)
                return ptr - str + 2;
            if (ptr[3] == 0)
                return ptr - str + 3;
        }
    }
}

 

strlen实现

原文:http://www.cnblogs.com/yifi/p/6696291.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!