Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5202642
  • 博文数量: 553
  • 博客积分: 13864
  • 博客等级: 上将
  • 技术积分: 11041
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-28 21:25
个人简介

个人Blog: hhktony.com

文章分类

全部博文(553)

文章存档

2015年(1)

2014年(2)

2013年(12)

2012年(384)

2011年(154)

分类: LINUX

2012-02-14 01:53:46

标准strcpy函数

功能:复制字符串

参数:目的字符串指针,源字符串指针(输入参数)

返回:目的字符串指针

char * strcpy( char *strDest, const char *strSrc )  //将源字符串加const,表明其为输入参数
{
 assert( (strDest != NULL) && (strSrc != NULL) ); //对源地址和目的地址加非0断言

    if ( strDest == strSrc)
         return strDest ;
 char *address = strDest;
 while( (*strDest++ = * strSrc++) != ‘/0’ );
  return address;  //为了实现链式操作,将目的地址返回
}

标准strlen函数

功能:计算字符串长度,结束符/0不计算在内

参数:字符串指针(输入参数)

返回:字符串长度 int

int strlen( const char *str )

{
 assert( strt != NULL ); //断言字符串地址非0
 int len;
 while( (*str++) != '/0' )
 {
  len++;
 }
 return len;
}

 

char *strncpy(char *a, const char *b, size_t n)
{
/* is is quite likely that memcpy(a, b, MIN(n, strlen(b))
   is now faster than this first bit: */
   
    char *t = a;
    char *t_end = a+n;

    if  (n > 0)
    {
 while (t < t_end && ((*t++ = *b++)))
 {
     ;
 }

 /*  pad with NULs if fewer than N characters */

 while (t < t_end)
 {
     *t++ = '/0';
 }
    }

    return a;
}


char *strcat(char *a, const char *b)
{
    char *t = a;

    while (*t)
    {
        t++;
    }

    while ((*t++ = *b++))
    {
        ;
    }

   return a;
}

char *strncat(char *a, const char *b, size_t n)
{
    char *t = a;
    char *t_end;

    while (*t)
    {
        t++;
    }

    /* append at most 'n' characters */
    t_end = t+n;

    while (t < t_end && (*t++ = *b++))
    {
        ;
    }

    /*  always NUL terminate, even if strlen(b) > n chars */
    *t = '/0';

    return a;
}

int strcmp(const char *a, const char *b)
{
    while (*a)
    {
        if (*a != *b)
        {
            return (int)(*a) - (int)(*b);
        }
        else
        {
            a++;
            b++;
        }
    }

    return (*b ? -1 : 0);
}

int strncmp(const char *a, const char *b, size_t n)
{
    while (n-- > 0)
    {
        if  (*a != *b)
        {
            return (int)(*a) - (int)(*b);
        }
        else if (*a)
        {
            a++;
            b++;
        }
        else
        {
            return 0;
        }
    }

    return 0;
}

#endif /* OS_HOSTED */

int strncasecmp(const char *a, const char *b, size_t n)
{
    while (n-- > 0)
    {
        if  (toupper(*a) != toupper(*b))
        {
            return (int)(*a) - (int)(*b);
        }
        else if (*a)
        {
            a++;
            b++;
        }
        else
        {
            return 0;
        }
    }
    return 0;
}

int strnicmp(const char *a, const char *b, size_t n)
{
   return strncasecmp(a, b, n);
}

int strcasecmp(const char *a, const char *b)
{
    while (*a)
    {
        if (toupper(*a) != toupper(*b))
        {
            return (int)(*a) - (int)(*b);
        }
        else
        {
            a++;
            b++;
        }
    }

    return (*b ? -1 : 0);
}

 

标准空字符判断函数

/**

 * 功能:空字符判断 

 *  @c     char.
 *  @return      positive if c is space char 
 *                      negative and negative non-zero if not

**/

int isspace(char c)
{
    return (c == ' ') || (c == '/t') || (c == '/n');
}

 

#define CHAR_BIT 8
#define CHAR_MAX 127
#define CHAR_MIN (-128)

#define INT_MAX (0x7fffffff)
#define INT_MIN (-INT_MAX - 1)

#define LONG_MAX (0x7fffffff)
#define LONG_MIN (0x80000000U)

#define SCHAR_MAX 127
#define SCHAR_MIN (-128)

#define SHRT_MAX (0x7fff)
#define SHRT_MIN (0xffff8000)

#define UCHAR_MAX 255
#define UINT_MAX (0xffffffff)
#define ULONG_MAX (0xffffffffU)
#define USHRT_MAX (0xffff)
#define U_SHRT_MAX USHRT_MAX

long strtol(const char *s, char **endp, int base)
{
    unsigned char *p = (unsigned char *)s;
    long rv = 0;
    int sig = 1;

    /*  skip leading spaces  */

    while (isspace(*p))
    {
        p++;
    }

    /*  check for sign  */

    if  (*p == '-')
    {
        sig = -1;
        p++;
    }

    /*  zero base => look at first digit  */

    if  (base == 0)
    {
        if  (*p == '0')
        {
            p++;
            if  (*p == 'x' || *p == 'X')
            {
  p++;
                base = 16;
            }
            else
            {
                base = 8;
            }
        }
        else
        {
            base = 10;
        }
    }
    else
    {
 /*  base 16 is allowed an initial 0x  */
 
 if  (base == 16)
 {
     if  (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
     {
  p += 2;
     }
 }
    }

    /*  and we're off  */

    while (1==1)
    {
        int cv = 256;

        if  (isdigit(*p))
        {
            cv = *p - '0';
        }
        else if (islower(*p))
        {
            cv = *p - 'a' + 10;
        }
        else if (isupper(*p))
        {
            cv = *p - 'A' + 10;
        }
       
        if  (cv >= base)
        {
            if  (endp)
            {
                *endp = p;
            }
            return rv*sig;
        }
        else
        {
            p++;

            rv = rv * base + cv;

            if  ((BITS)rv & 0x80000000U)
            {
                if (endp)
                {
                    *endp = p;
                }
                ATMOS_SETERR(ERANGE);
                return (sig == 1 ? LONG_MAX : LONG_MIN);
            }
        }
    }
}

unsigned long strtoul(const char *s, char **endp, int base)
{
    unsigned char *p = (unsigned char *)s;
    unsigned long rv = 0;

    /*  skip leading spaces  */

    while (isspace(*p))
    {
        p++;
    }

    /*  zero base => look at first digit  */

    if  (base == 0)
    {
        if  (*p == '0')
        {
            p++;
            if  (*p == 'x' || *p == 'X')
            {
  p++;
                base = 16;
            }
            else
            {
                base = 8;
            }
        }
        else
        {
            base = 10;
        }
    }
    else
    {
 /*  base 16 is allowed an initial 0x  */
 
 if  (base == 16)
 {
     if  (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
     {
  p += 2;
     }
 }
    }

    /*  and we're off  */

    while (1==1)
    {
        int cv = 256;

        if  (isdigit(*p))
        {
            cv = *p - '0';
        }
        else if (islower(*p))
        {
            cv = *p - 'a' + 10;
        }
        else if (isupper(*p))
        {
            cv = *p - 'A' + 10;
        }
       
        if  (cv >= base)
        {
            if  (endp)
            {
                *endp = p;
            }
            return rv;
        }
        else
        {
            p++;

            if  (rv & 0x80000000U)
            {
                if (endp)
                {
                    *endp = p;
                }
                ATMOS_SETERR(ERANGE);
                return ULONG_MAX;
            }

            rv = rv * base + cv;
        }
    }
}

阅读(1074) | 评论(0) | 转发(0) |
0

上一篇:vim插件使用 -- c.vim

下一篇:extern

给主人留下些什么吧!~~