免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1318 | 回复: 0
打印 上一主题 下一主题

glibc笔记——strlen [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-08 22:12 |只看该作者 |倒序浏览

glib2.0中strlen函数的定义如下:
#include string.h>
size_t
strlen (const char *str)
{
  int cnt;
  asm("cld\n"            /* Search forward. 清方向位 */
      /* Some old versions of gas need `repne' instead of `repnz'.  */
          //ECX!=0且ZF=0则继续循环
      "repnz\n"            /* Look for a zero byte.  */
          //AL-(ES:[EDI])
      "scasb" /* %0, %1, %3 */ :
      //输出cnt,初值为-1(==ECX);EAX=0;EDI=str
      "=c" (cnt) : "D" (str), "0" (-1), "a" (0));
      
  return -2 - cnt;
}
其中size_t的定义位于stddef.h:
typedef unsigned long size_t;
注意点:
(1)该函数返回无符号整数
if(strlen(x)>=strlen(y))...与
if(strlen(x)-strlen(y))...的含义并不相同,无符号数绝不可能为负数.
(2)一个自定义的实现:
#include
size_t strlen(const char *str)
{
    int len=0;
    while(*str++!='\0')
        len++;
    return len;
}
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/86768/showart_2180364.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP