strcpy strcat等源码

以下程序段取自glibc,做了简单修改.
/* Copy SRC to DEST.  */
char *
strcpy (dest, src)
     char *dest;
     const char *src;
{
  char c;
  char *s = (char *)src;
  const ptrdiff_t off = dest - s - 1;

  do
    {
      c = *s++;
      s[off] = c;
    }
  while (c != '/0');

  return dest;
}
以下程序段取自VC6.0

/*

  下面是strcpy库函数的实现,因为库函数讲究的就是精练、简洁。所以没有其他的异常处理代码。主要的异常处理还是交给了函数的使用者,在调用前请确认目的和源指针是否都存在(不能为Null),请确认目标指针空间是否大于源字符串的空间。

Copies the string src into the spot specified by dest;

  assumes enough room.

  目标指针空间必须大于源字符串空间。

*/


char * __cdecl strcpy(char * dst, const char * src)
{
        char * cp = dst;

        while( *cp++ = *src++ )
                ;               /* Copy src over dst */

        return( dst );
}

glibc中取出的那段,其实我还删了一些,主要是范围检查,而取自VC的这段却是十分简洁.
这并不能说明哪个更好,看看这些源代码,使自己能提高编码水平吧.
===========================================================
Assembly code         page    , 132
        title   strcat - concatenate (append) one string to another
; ***
;
strcat.asm - contains strcat() and strcpy() routines
;
;
       Copyright (c) Microsoft Corporation. All rights reserved.
;
;
Purpose:
;
       STRCAT concatenates (appends) a copy of the source string to the
;
       end of the destination string, returning the destination string.
;
;
*******************************************************************************

        .xlist
        include cruntime.
inc
        .list


page
; ***
;
char *strcat(dst, src) - concatenate (append) one string to another
;
;
Purpose:
;
       Concatenates src onto the end of dest.  Assumes enough
;
       space in dest.
;
;
       Algorithm:
;
       char * strcat (char * dst, char * src)
;
       {
;
           char * cp = dst;
;
;
           while( *cp )
;
                   ++cp;           /* Find end of dst */
;
           while( *cp++ = *src++ )
;
                   ;               /* Copy src to end of dst */
;
           return( dst );
;
       }
;
;
Entry:
;
       char *dst - string to which "src" is to be appended
;
       const char *src - string to be appended to the end of "dst"
;
;
Exit:
;
       The address of "dst" in EAX
;
;
Uses:
;
       EAX, ECX
;
;
Exceptions:
;
;
*******************************************************************************

page
; ***
;
char *strcpy(dst, src) - copy one string over another
;
;
Purpose:
;
       Copies the string src into the spot specified by
;
       dest; assumes enough room.
;
;
       Algorithm:
;
       char * strcpy (char * dst, char * src)
;
       {
;
           char * cp = dst;
;
;
           while( *cp++ = *src++ )
;
                   ;               /* Copy src over dst */
;
           return( dst );
;
       }
;
;
Entry:
;
char * dst - string over which "src" is to be copied
;
       const char * src - string to be copied over "dst"
;
;
Exit:
;
       The address of "dst" in EAX
;
;
Uses:
;
       EAX, ECX
;
;
Exceptions:
;
*******************************************************************************
strcmp函数

int __cdecl strcmp (
        const char * src,
        const char * dst
        )
{
        int ret = 0 ;

        while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
                ++src, ++dst;  
        if ( ret < 0 )
                ret = -1 ;
        else if ( ret > 0 )
                ret = 1 ;

        return( ret );
}

 

我们要看的是while循环这个语句, ! (ret = *(unsigned char *)src - *(unsigned char *)dst)意思是拿指针变量src所指向的字符值(即*src)减去指针变量dst所指向的字符值(即*dst),差值赋给ret,再取非运算,最后与*dst进行与运算;

拿abc<abcd举例,第一次因为a=a,则执行++src, ++dst;2次自加后,ret值为负,跳出while语句,执行if语句的判断,输出为-1;

这里要注意的是:
1.unsigned char*是强制转换类型。

2.若src和dst都为空串,返回值是多少呢?因为空串不是指地址为空,还是有地址的,这样就很明确了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值