现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

strcpy()、memcpy()、memmove()、memset()的实现

2012-07-27 22:48 工业·编程 ⁄ 共 3169字 ⁄ 字号 评论 1 条

strcpy(), 字符串拷贝.
char *strcpy(char *strDest, const char *strSrc)
{
    assert((strDest!=NULL) && (strSrc !=NULL));
    char *address = strDest;    
    while( (*strDest++ = * strSrc++) != '/0')
       NULL ;
    return address ;      
}


memcpy, 拷贝不重叠的内存块
void *memcpy(void* pvTo, void* pvFrom, int size)

{
        assert(pvTo != NULL && pvFrom != NULL);
        void* pbTo = (byte*)pvTo;
        void* pbFrom = (byte*)pvFrom;
        /* 内存块重叠吗?如果重叠,就使用memmove */
        assert(pbTo>=pbFrom+size || pbFrom>=pbTo+size);
        while(size-->0)
            *pbTo++ == *pbFrom++;
        return pvTo;
}


void *MemCopy(void *dest,const void *src,int count)
{
    char *pDest=static_cast<char *>(dest);
    const char *pSrc=static_cast<const char *>(src);
    if( pDest>pSrc && pDest<pSrc+count )    //判断临界区域,无重复则正向拷贝,重复则逆向拷贝
    {
        for(int i=count-1; i<=0; i--)
        {
            pDest[i]=pSrc[i];
        }
    }
    else
    {
        for(int i=0; i<count; i++)
        {
             pDest[i]=pSrc[i];
        }
    }
    return pDest;
}


void *Memmove(void *Dst, const void*Src,int count)
{
    assert(Dst && Src);
    void* pDst = Dst;
    if (Dst<Src && (char*)Dst > (char*)Src + count) 
    {
        while(count--)
        {
               *(char*)Dst = *(char*)Src;
               Dst = (char*)Dst + 1;
               Src = (char*)Src + 1;
        }
    }
    else
    {
           Dst = (char*)Dst + count - 1;
           Src = (char*)Src + count - 1;
           while(count--)
           {
                  *(char*)Dst = *(char*)Src;
                  Dst = (char*)Dst -1 ;
                  Src = (char*)Src -1 ;
           }
    }
    return pDst;
}


void* memmove(void *dest, const void *src,int n)
{
    if (n == 0) return 0;
    if (dest == NULL) return 0;
    if (src == NULL)    return 0;
    char *psrc = (char*)src;
    char *pdest = (char*)dest;
    if((dest <= psrc) || (pdest >= psrc + n)) /*检查是否有重叠问题 */ 
    {
         for(int i=0; i < n; i++) /*正向拷贝*/ 
         { 
               *pdest++ = *psrc++; 
          } 
    } 
    else /*反向拷贝*/ 
    {
          psrc += n;
          pdest += n;
          for(int i=0;i<n;i++) 
          { 
                *pdest-- = *psrc--;
           } 
    } 
    return dest;
}

memset:把buffer所指内存区域的前count个字节设置成字符c

void * Memset(void* buffer, int c, int count)
{
        char* pvTo=(char*)buffer;
        assert(buffer != NULL);
        while(count-->0)
        *pvTo++=(char)c;
        return buffer;
}

-----------------------------------------------------------------------------------------------strcat
void mycat(char *s1, char *s2)
{
      while(*s1++);
      s1--;
      while(*s1++= *s2++);
}
------------------------------------------------------------------------------------------------------------------------strcpy
void mycpy(char *s1, char *s2)
{
      while(*s1++= *s2++);
}
-----------------------------------------------------------------strcmp
int mycmp(char *s1,char *s2)
{
       for(; *s == *t;s++,t++)
       if(*s== 0)

             return0;

       return *s - *t;

}
----------------------------------------------------------------------------------------strlen
int mylen(char *s1)
{
      char*p = s1;
      while(*p++);
             returnp - s -1;
}
-------------------------------------------------------------------------------------strlwr
char* strlwr(char *s)
{
        unsignedchar AL;
        registerchar *DX,*SI;
        DX=SI=s;
        while((AL=*SI++)!='/0')
        {
                  AL-='A';
                  if(AL>'Z'-'A')

                          continue;

                  SI[-1]+='a'-'A';
         }
         return DX;

}

目前有 1 条留言    访客:0 条, 博主:0 条 ,引用: 1 条

    外部的引用: 1 条

    • 新手必看:你知道strcpy和memcpy的区别吗 | 求索阁

    给我留言

    留言无头像?