部分GNU代码片 18、字符串操作


size_t strlcat(
        register char* s, register const char* t, register size_t n)
{
    const char* o = t;
   
    if (n)
    {
        while (--n && *s)
            s++;
        if (n)
            do
            {
//                if (!--n)  //这里是http://www.google.com/codesearch?hl=zh-CN&q=+strlcat+show:KiQJipp-bKc:-XhE0e2favM:qI68VRkNHVY&sa=N&cd=4&ct=rc&cs_p=http://gentoo.osuosl.org/distfiles/ast-ksh.2006-02-14.tgz&cs_f=src/lib/libast/string/strlcat.c#first给出的代码,感觉有问题,现在更正如下
                if (!n--)
                {
                    *s = 0;
                    break;
                }
            } while (*s++ = *t++);
        else
            *s = 0;
    }

//更不大明白这一段代码是搞什么的,很郁闷,他根本就没有意义
//    if (!n)
//        while (*t++)
//            ;
//    return t - o - 1;

    //下面是返回还剩余多少字符没能cat上

    if ((unsigned int )-1 == n)
    {
        o = t;
        while (*t++)
            ;
        return t-o-1;
    }
    return 0;

//    return t-o;//这个用来返回已经cat的长度
}


#define DEBUG_ME
#define  HAVE_CLOCK_GETTIME
extern double time_cost(void);
int main()
{
   
    char zA[7]="1234";
    char zB[5]="456";
   
    char zC[1024]="";
    char zD[1000]="123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    cout <<endl;
   
    cout << strlcat(zA, zB, sizeof(zA)) <<endl;
   
    cout << zA <<endl;
   
#ifdef  DEBUG_ME
                double tCost = time_cost();
#endif    
               
     for(int i=0;i<10000;i++) 
     {
         strlcat(zC, zD, sizeof(zC));
         *(zC+400) = 0;
     }
   
#ifdef  DEBUG_ME
                tCost = time_cost() - tCost;
                printf("Time: %lf seconds/n", tCost);
#endif    

}

 

Time: 0.075000 seconds这是做10000次的cat的耗时。

 time_cost函数参考前文

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值