Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1163491
  • 博文数量: 146
  • 博客积分: 6619
  • 博客等级: 准将
  • 技术积分: 1621
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-29 14:06
文章分类

全部博文(146)

文章存档

2020年(1)

2019年(4)

2018年(3)

2017年(5)

2015年(5)

2014年(7)

2013年(5)

2012年(11)

2011年(15)

2010年(13)

2009年(14)

2008年(63)

分类: C/C++

2008-04-07 15:14:11

memcmp ( ) /* -- C语言库函数源代码 - */  
/*
     Compares count bytes of memory starting at buffer1 and buffer2 and find if equal or which one is first in lexical order.
     比较内存区域buffer1和buffer2的前count个字节。当buffer1 < buffer2时,返回值 < 0;当buffer1 = buffer2时,返回值 0;当buffer1 > buffer2时,返回值 > 0。
*/
int my_memcmp(const void *buffer1,const void *buffer2,int count)
{
     if (!count)
        return(0);
     while ( --count && *(char *)buffer1 == *(char *)buffer2)
     {
        buffer1 = (char *)buffer1 + 1;
          buffer2 = (char *)buffer2 + 1;
     }
     return( *((unsigned char *)buffer1) - *((unsigned char *)buffer2) );
}
void Print(char * str1,char *str2,int t)
{
     if(t > 0)
        printf("\n%s Upper Than %s\n",str1,str2);
     else if(t < 0)
        printf("\n%s Lower Than %s\n",str1,str2);
     else
        printf("\n%s Equal %s\n",str1,str2);
      
}
int main()
{
     char *str1= "ammana";
     char *str2 = "babi";

     Print(str1,str2,my_memcmp(str1,str2,3));
     Print(str2,str1,my_memcmp(str2,str1,3));
     Print(str2,str2,my_memcmp(str2,str2,3));
   
     system("pause");
     return 0;
}
memchr ( )/* -- C语言库函数源代码 - */  
#include
/*
     Searches at bufferfor the given character, stopping when characteris first found or cnt bytes have been searched through.
     从buffer所指内存区域的前count个字节查找字符ch,当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的指针;否则返回NULL。
*/


void * my_memchr(const void * buffer,int ch,int count)
{
     while ( count && (*(unsigned char *)buffer != (unsigned char)ch) )
     {
        buffer = (unsigned char *)buffer + 1;
          count--;
     }
     return(count ? (void *)buffer : NULL);
}
int main()
{
     char *str = "ammana_babi";
     char * p;
     char ch;
   
     ch = '9';
     p = (char *)my_memchr(str,ch,strlen(str)+1);
     if(p == NULL)
        printf("Can't find the character %c !\n",ch);
     else
        printf("Find the character %c !\n",*p);
   
     ch = 'b';
     p = (char *)my_memchr(str,ch,strlen(str)+1);
     if(p == NULL)
        printf("Can't find the character %c !\n",ch);
     else
        printf("Find the character %c !\n",*p);

     system("pause");
     return 0;
}
memccpy ( ) /* -- C语言库函数源代码 - */  
#include
/*
     Copies bytes from src to dest until count bytes have been    copied,or up to and including the character c, whichever comes first.
     如果src前n个字节中存在’c’,返回指向字符’c’后的第一个字符的指针;否则返回NULL,src被复制。
*/

void * my_memccpy(void *dest,const void *src,int c,int count)
{
        while ( count && (*((char *)(dest = (char *)dest + 1) - 1) =
     *((char *)(src = (char *)src + 1) - 1)) != (char)c )
          count--;
     return(count ? dest : NULL);
}
/*这个函数的while条件判断写的比较长,看的眼疼,等价与以下写法:*/
void * my_memccpy01(void *dst,const void *src,int c,int count)
{
     while (count)
     {
        *(char *)dst = *(char *)src;
        dst = (char *)dst + 1;
        if(*(char *)src == (char) c)
          break;
        src = (char *)src + 1;
        count--;
     }
     return(count ? dst : NULL);
}
int main()
{
     char a[12];
     char * p;
     char * str ="ammana_babi";
     char ch;
   
     ch =    '9';
     p = (char *)my_memccpy01(a,str,ch,strlen(str)+1);
     if(p == NULL)
        printf("\nCan't not find character. \n");
     else
     {
        printf("\nFind the character! \n");
        *p= '\0';
     }
     printf("\nThe String which has been copied is:\t");
     puts(a);
   
     printf("************************************");

     ch =    'b';
     p = (char *)my_memccpy01(a,str,ch,strlen(str)+1);
     if(p == NULL)
        printf("\nCan't not find character. \n");
     else
     {
        printf("\nFind the character! \n");
        *p = '\0';
     }
     printf("\nThe String which has been copied is:\t");
     puts(a);
   
     system("pause");
     return 0;
}
memmove ( ) /* -- C语言库函数源代码 - */  
/*
     memmove() copies a source memory buffer to a destination memory buffer.This routine recognize overlapping buffers to avoid propogation.For cases where propagation is not a problem, memcpy() can be used.
     memmove()由src所指定的内存区域赋值count个字符到dst所指定的内存区域。src和dst所指内存区域可以重叠,但复制后src的内容会被更改。函数返回指向dst的指针。
*/

void * my_memmove(void * dst,const void * src,int count)
{
     void * ret = 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(ret);
}
int main()
{
     char a[12];
     puts((char *)my_memmove(a,"ammana_babi",16));
     system("pause");
     return 0;
}
memcpy ( ) /* -- C语言库函数源代码 - */  
/*
     memcpy() copies a source memory buffer to a destination memory buffer. This routine does NOT recognize overlapping buffers, and thus can lead to propogation.For cases where propagation must be avoided, memmove() must be used.
     memcpy()由src指定内存区域拷贝count个字符到dst所指定的内存区域。src和dst内存区域不能重叠,函数返回指向dst的指针。
*/

void * my_memcpy(void *dst,const void *src,int count)
{
     void * ret = dst;
     while (count--)
     {
        *(char *)dst = *(char *)src;
        dst = (char *)dst + 1;
        src = (char *)src + 1;
     }
     return(ret);
}
int main()
{
     char a[12];
     puts((char *)my_memcpy(a,"ammana_babi",16));
     system("pause");
     return 0;
}
strnicmp ( ) /* -- C语言库函数源代码 - */  
/*
     Compare the two strings for lexical order.    Stops the comparison when the
     following occurs: (1) strings differ, (2) the end of the strings is reached,
     or (3) count characters have been compared. For the purposes of the comparison,
     upper case characters are converted to lower case.
     字符串比较函数,比较字符串src和dst的前count个字符,但是不区分大小写,大写字母
     会被转换为小写字母来进行比较。如:"abc_" < "ABCD" ,因为 "_" < "d"。当源字符串
     大于目标字符串的时候,返回>0;当源字符串等于目标字符串的时候,返回=0。当源字符串
     小于目标字符串的时候,返回<0;
*/

int my_strnicmp(const char *dst,const char *src,int count)
{
     int ch1, ch2;
     do
     {
        if ( ((ch1 = (unsigned char)(*(dst++))) >= 'A') &&(ch1 <= 'Z') )
          ch1 += 0x20;
        if ( ((ch2 = (unsigned char)(*(src++))) >= 'A') &&(ch2 <= 'Z') )
          ch2 += 0x20;
     } while ( --count && ch1 && (ch1 == ch2) );
     return (ch1 - ch2);
}
void Print(char * str1,char *str2,int t,int n)
{
     char *p;
     p = str1;
     while(*p && (p-str1) < n) printf("%c",*p),p++;
     if(t > 0)
          printf("\tUpper Than\t");
      
     else if(t < 0)
        printf("\tLower Than\t");
     else
        printf("\tEqual\t\t");
     p = str2;
     while(*p && (p-str2) < n) printf("%c",*p),p++;
     printf("\n");
}
#define nn 4  
int main()
{
     char *str1= "ammana";
     char *str2 = "babi";
     char *str3 = "AMMANA";
     char *str4 = "bab_";

     Print(str1,str2,my_strnicmp(str1,str2,nn),nn);
     Print(str3,str1,my_strnicmp(str3,str1,nn),nn);
     Print(str4,str2,my_strnicmp(str3,str2,nn),nn);
   
     system("pause");
     return 0;
}

stricmp ( ) /* -- C语言库函数源代码 - */  
/*
_stricmp/_strcmpi perform a case-insensitive string comparision.
For differences, upper case letters are mapped to lower case.
Thus, "abc_" < "ABCD" since "_" < "d".
     字符串比较函数,比较字符串src和dst,但是不区分大小写,
     大写字母会被转换为小写字母来进行比较。如:"abc_" < "ABCD" ,因为 "_" < "d"。
     当源字符串大于目标字符串的时候,返回>0;当源字符串等于目标字符串的时候,返回=0。
     当源字符串小于目标字符串的时候,返回<0;
*/

int my_stricmp(const char *dst, const char *src)
{
     int ch1, ch2;
     do
     {
        if ( ((ch1 = (unsigned char)(*(dst++))) >= 'A') &&(ch1 <= 'Z') )
          ch1 += 0x20;
        if ( ((ch2 = (unsigned char)(*(src++))) >= 'A') &&(ch2 <= 'Z') )
          ch2 += 0x20;
     } while ( ch1 && (ch1 == ch2) );
     return(ch1 - ch2);
}
void Print(char * str1,char *str2,int t)
{
     if(t > 0)
        printf("\n%s Upper Than %s\n",str1,str2);
     else if(t < 0)
        printf("\n%s Lower Than %s\n",str1,str2);
     else
        printf("\n%s Equal %s\n",str1,str2);
}
int main()
{
     char *str1= "ammana";
     char *str2 = "babi";
     char *str3 = "AMMANA";
     char *str4 = "bab_";

     Print(str1,str2,my_stricmp(str1,str2));
     Print(str3,str1,my_stricmp(str3,str1));
     Print(str4,str2,my_stricmp(str4,str2));
   
     system("pause");
     return 0;
}

strncmp ( ) /* -- C语言库函数源代码 - */  
/*
     Compares two strings for lexical order.  
     The comparison stops after: (1) a difference between the strings is found, (2) the end of the strings is reached, or (3) count characters have been compared.
     比较字符串first和last的前n个字符。first > last ,>0;first = last,=0;first < last, <0 。
*/

int my_strncmp(const char *first,const char *last,int count)
{
     if (!count)
        return(0);
     while (--count && *first && *first == *last)first++,last++;
     return( *(unsigned char *)first - *(unsigned char *)last );
}
void Print(char * str1,char *str2,int t,int n)
{
     char *p;
     p = str1;
     while(*p && (p-str1) < n) printf("%c",*p),p++;
     if(t > 0)
          printf("\tUpper Than\t");
      
     else if(t < 0)
        printf("\tLower Than\t");
     else
        printf("\tEqual\t\t");
     p = str2;
     while(*p && (p-str2) < n) printf("%c",*p),p++;
     printf("\n");
}
#define nn 3  
int main()
{
     char *str1= "ammana";
     char *str2 = "babi";

     Print(str1,str2,my_strncmp(str1,str2,nn),nn);
     Print(str2,str1,my_strncmp(str2,str1,nn),nn);
     Print(str2,str2,my_strncmp(str2,str2,nn),nn);
   
     system("pause");
     return 0;
}

strcmp ( ) /* -- C语言库函数源代码 - */  
/*
     STRCMP compares two strings and returns an integer
     to indicate whether the first is less than the second,
     the two are equal, or whether the first is greater than the second.
     Comparison is done byte by byte on an UNSIGNED basis,
     which is to say that Null (0) is less than any other character (1-255).
     字符串比较函数,比较字符串source和dest。当源字符串大于目标字符串的时候,返回1;
     当源字符串等于目标字符串的时候,返回0。当源字符串小于目标字符串的时候,返回-1;
*/
int my_strcmp(const char *source,const char *dest)
{
     int ret = 0 ;
     while( ! (ret = *( unsigned char *)source - *(unsigned char *)dest) && *dest)
        source++, dest++;
     if ( ret < 0 )
          ret = -1 ;
     else if ( ret > 0 )
          ret = 1 ;
     return(ret);
}
void Print(char * str1,char *str2,int t)
{
     if(t == 0)
        printf("\n%s Equal %s\n",str1,str2);
     else if(t == 1)
        printf("\n%s Upper Than %s\n",str1,str2);
     else if(t == -1)
        printf("\n%s Lower Than %s\n",str1,str2);
}
int main()
{
     char *str1= "ammana";
     char *str2 = "babi";

     Print(str1,str2,my_strcmp(str1,str2));
     Print(str2,str1,my_strcmp(str2,str1));
     Print(str2,str2,my_strcmp(str2,str2));
   
     system("pause");
     return 0;
}

strncpy ( )/* -- C语言库函数源代码 - */  
/*
     Copies count characters from the source string to the destination.
     If count is less than the length of source,NO NULL CHARACTER is put
     onto the end of the copied string.If count is greater than the length
     of sources, dest is padded with null characters to length count.
        把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中。
     如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束;
     如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。
     src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
     返回指向dest的指针。
*/
char * my_strncpy( char * dest, const char * source, int count )
{
     char *p = dest;
     while (count && (*p++ = *source++)) count--;
     while(count--)
        *p++ = '\0';
     return(dest);
}
int main()
{
     char a[20];
     puts(my_strncpy(a,"ammana_babi",15));
     system("pause");
     return 0;
}

strncat ( )/* -- C语言库函数源代码 - */  
/*
     Appends at most count characters of the string back onto the end of front, and ALWAYS terminates with a null character.If count is greater than the length of back, the length of back is used instead.(Unlike strncpy, this routine does not pad out to count characters).
     把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
     src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
     返回指向dest的指针。
*/
char * my_strncat(char *dest,const char *source,int count)
{
     char *p = dest;
     while (*p) p++;
     while (count-- && (*p++ = *source++));
     *p = '\0';
     return(dest);
}
int main()
{
     char a[20] = "ammana_";
     puts(my_strncat(a,"babi",10));
     system("pause");
     return 0;
}

strcat ( )/* -- C语言库函数源代码 - */  
/*
     Concatenates src onto the end of dest.  
     Assumes enough space in dest.
     目标指针空间必须有足够的存储空间。
*/
char *    my_strcat ( char * dst, const char * src )
{
     char * p = dst;
      while( *p )    p++;                  
      while( *p++ = *src++ ) ;
      return( dst );
}//字符串的连接

/*当然也可以用下面这种方式:*/
char * my_strcat_01(char * dst,char * src)
{
     char * p = dst + strlen(dst);
     strcpy(p,src);
     return dst;
}
int main()
{
     char str1[] = "ammana_";
     char *str2 = "babi";
     puts(my_strcat(str1,str2));
     system("pause");
     return 0;
}

strlen ( )/* -- C语言库函数源代码 - */  
/*
得到字符串长度。(不含结束符 ‘\0’)
*/
int my_strlen(const char * str )
{
     const char *p = str;
      while( *p++ ) ;
      return( (int)(p - str - 1) );
}
/*当然也可以采用下面的写法。*/
int my_strlen_01 ( const char * str )
{
     const char *p = str;
      while( *p ) p++;
      return( (int)(p - str) );
}
int main()
{
     char *str = "ammana_babi";
     printf("%d\n",my_strlen(str));
     printf("%d\n",my_strlen_01(str));
     system("pause");
}

strcpy ( )/* -- C语言库函数源代码 - */  
/*
    下面是strcpy库函数的实现,因为库函数讲究的就是精练、简洁。所以没有其他的异常处理代码。主要的异常处理还是交给了函数的使用者,在调用前请确认目的和源指针是否都存在(不能为Null),请确认目标指针空间是否大于源字符串的空间。
Copies the string src into the spot specified by dest;
    assumes enough room.
    目标指针空间必须大于源字符串空间。
*/
char * my_strcpy(char * dst, const char * src)
{
     char * cp = dst;
     while( *cp++ = *src++ )    ;         
     return( dst );
}
int main()
{
     char a[12];
     puts(my_strcpy(a,"ammana_babi"));
     system("pause");
     return 0;
}

阅读(1783) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~