Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368992
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-05-20 14:13:00


  1. char *
  2. simple_memcpy (char *dst, const char *src, size_t n)
  3. {
  4.   char *ret = dst;
  5.   while (n--)
  6.     *dst++ = *src++;
  7.   return ret;
  8. }

  9. char *
  10. simple_memchr (const char *s, int c, size_t n)
  11. {
  12.   while (n--)
  13.     if (*s++ == (char) c)
  14.       return (char *) s - 1;
  15.   return NULL;
  16. }

  17. int
  18. simple_memcmp (const char *s1, const char *s2, size_t n)
  19. {
  20.   int ret = 0;

  21.   while (n--
  22.          && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
  23.   return ret;
  24. }

  25. void *
  26. simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
  27.                size_t needle_len)
  28. {
  29.   const char *begin;
  30.   const char *const last_possible
  31.     = (const char *) haystack + haystack_len - needle_len;

  32.   if (needle_len == 0)
  33.     return (void *) haystack;

  34.   if (haystack_len < needle_len)
  35.     return NULL;

  36.   for (begin = (const char *) haystack; begin <= last_possible; ++begin)
  37.     if (begin[0] == ((const char *) needle)[0] &&
  38.         !memcmp ((const void *) &begin[1],
  39.                  (const void *) ((const char *) needle + 1),
  40.                  needle_len - 1))
  41.       return (void *) begin;

  42.   return NULL;
  43. }

  44. char *
  45. simple_memmove (char *dst, const char *src, size_t n)
  46. {
  47.   char *ret = dst;
  48.   if (src < dst)
  49.     {
  50.       dst += n;
  51.       src += n;
  52.       while (n--)
  53.         *--dst = *--src;
  54.     }
  55.   else
  56.     while (n--)
  57.       *dst++ = *src++;
  58.   return ret;
  59. }


  60. char *
  61. simple_memset (char *s, int c, size_t n)
  62. {
  63.   char *r = s, *end = s + n;
  64.   while (r < end)
  65.     *r++ = c;
  66.   return s;
  67. }

  68. char *
  69. simple_stpcpy (char *dst, const char *src)
  70. {
  71.   while ((*dst++ = *src++) != '\0');
  72.   return dst - 1;
  73. }

  74. char *
  75. simple_stpncpy (char *dst, const char *src, size_t n)
  76. {
  77.   while (n--)
  78.     if ((*dst++ = *src++) == '\0')
  79.       {
  80.         size_t i;

  81.         for (i = 0; i < n; ++i)
  82.           dst[i] = '\0';
  83.         return dst - 1;
  84.       }
  85.   return dst;
  86. }

  87. char *
  88. simple_strcat (char *dst, const char *src)
  89. {
  90.   char *ret = dst;
  91.   while (*dst++ != '\0');
  92.   --dst;
  93.   while ((*dst++ = *src++) != '\0');
  94.   return ret;
  95. }

  96. char *
  97. simple_strchr (const char *s, int c)
  98. {
  99.   for (; *s != (char) c; ++s)
  100.     if (*s == '\0')
  101.       return NULL;
  102.   return (char *) s;
  103. }

  104. int
  105. simple_strcmp (const char *s1, const char *s2)
  106. {
  107.   int ret;

  108.   while ((ret = *(unsigned char *) s1 - *(unsigned char *) s2++) == 0
  109.          && *s1++);
  110.   return ret;
  111. }

  112. char *
  113. simple_strcpy (char *dst, const char *src)
  114. {
  115.   char *ret = dst;
  116.   while ((*dst++ = *src++) != '\0');
  117.   return ret;
  118. }

  119. size_t
  120. simple_strspn (const char *s, const char *acc)
  121. {
  122.   const char *r, *str = s;
  123.   char c;

  124.   while ((c = *s++) != '\0')
  125.     {
  126.       for (r = acc; *r != '\0'; ++r)
  127.         if (*r == c)
  128.           break;
  129.       if (*r == '\0')
  130.         return s - str - 1;
  131.     }
  132.   return s - str - 1;
  133. }

  134. size_t
  135. simple_strcspn (const char *s, const char *rej)
  136. {
  137.   const char *r, *str = s;
  138.   char c;

  139.   while ((c = *s++) != '\0')
  140.     for (r = rej; *r != '\0'; ++r)
  141.       if (*r == c)
  142.         return s - str - 1;
  143.   return s - str - 1;
  144. }

  145. size_t
  146. simple_strlen (const char *s)
  147. {
  148.   const char *p;

  149.   for (p = s; *p; ++p);
  150.   return p - s;
  151. }

  152. int
  153. simple_strncmp (const char *s1, const char *s2, size_t n)
  154. {
  155.   int ret = 0;

  156.   while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
  157.          && *s1++);
  158.   return ret;
  159. }

  160. char *
  161. simple_strncpy (char *dst, const char *src, size_t n)
  162. {
  163.   char *ret = dst;
  164.   while (n--)
  165.     if ((*dst++ = *src++) == '\0')
  166.       {
  167.         while (n--)
  168.           *dst++ = '\0';
  169.         return ret;
  170.       }
  171.   return ret;
  172. }

  173. char *
  174. simple_strpbrk (const char *s, const char *rej)
  175. {
  176.   const char *r;
  177.   char c;

  178.   while ((c = *s++) != '\0')
  179.     for (r = rej; *r != '\0'; ++r)
  180.       if (*r == c)
  181.         return (char *) s - 1;
  182.   return NULL;
  183. }

  184. char *
  185. simple_strrchr (const char *s, int c)
  186. {
  187.   const char *ret = NULL;

  188.   for (; *s != '\0'; ++s)
  189.     if (*s == (char) c)
  190.       ret = s;

  191.   return (char *) (c == '\0' ? s : ret);
  192. }

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