Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1184562
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 14:46:27


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <string.h>

  5. #define SZ_SZIE 128

  6. typedef char STRING[SZ_SZIE];

  7. char * mystrcpy(char * dest, char * src);
  8. int StrIsEmpty(char * str);
  9. int mystrcmp(char * s1, char * s2);
  10. int mystrlen(char * str);
  11. int clearstr(char * str);
  12. char * mystrcat(char * dest, char * src);
  13. char * substring(char * sub, char * src, int pos, int len);
  14. int Index(char * src, char * substr, int pos);
  15. int Replace(char * src, char * oldstr, char * newstr);
  16. int StrInsert(char * src, int pos, char * newstr);
  17. int StrDelete(char * src, int pos, int len);
  18. char * reverse1(char * str);
  19. char * reverse2(char * str);
  20. char * reverse3(char * str);

  21. /*
  22. 函数功能:编程查找2个字符串的最大公共子串
  23. 比如:有2个字符串 A="aoabcp", B="gfabcm", 则返回输出:"abc"
  24. */

  25. int main(int argc, char ** argv)
  26. {
  27.     freopen("string.txt", "w", stdout);
  28.     printf("串的顺序存储!\n");
  29.     STRING string;
  30.     memset(string, 0, sizeof(STRING));
  31.     char * str = mystrcpy(string, "abcdefg");
  32.     printf("字符串复制函数,得到string = [%s]\n", string);
  33.     printf("字符串复制函数,返回得到str = [%s]\n", str);
  34.     
  35.     //memset(string, 0, sizeof(STRING));
  36.     if(StrIsEmpty(string) == 1)
  37.         printf("空串!\n");
  38.     else
  39.         printf("非空串!\n");
  40.         
  41.     int flag = mystrcmp(string, "abcdef");
  42.     printf("字符串比较=[%d]\n", flag);
  43.     
  44.     //memset(string, 0, sizeof(STRING));
  45.     //char * p = NULL;
  46.     int len = mystrlen(string);
  47.     printf("串长=[%d]\n", len);
  48.     
  49.     //clearstr(string);
  50.     printf("清空串string=[%s]\n", string);
  51.     
  52.     char * str1 = mystrcat(string, "ABCabcdefgABCabcdefg");
  53.     printf("串联接string=[%s]\n", string);
  54.     printf("串联接str1=[%s]\n", str1);
  55.     
  56.     STRING substr;
  57.     memset(substr, 0, sizeof(substr));
  58.     char * str2 = substring(substr, string, 7, 3);
  59.     printf("子串substr = [%s]\n", substr);
  60.     printf("子串str2 = [%s]\n", str2);
  61.     
  62.     int index = Index(string, substr, 17);
  63.     printf("[%s]串定位=[%d]\n", substr, index);
  64.     
  65.     STRING oldstr;
  66.     memset(oldstr, 0, sizeof(oldstr));
  67.     mystrcpy(oldstr, "ABC");
  68.     STRING newstr;
  69.     memset(newstr, 0, sizeof(newstr));
  70.     mystrcpy(newstr, "linux");
  71.     printf("替换前:string=[%s]\n", string);
  72.     Replace(string, oldstr, newstr);
  73.     printf("将串中[%s]替换成[%s]\n替换后:string=[%s]\n", oldstr, newstr, string);
  74.     
  75.     memset(oldstr, 0, sizeof(oldstr));
  76.     mystrcpy(oldstr, "linux");
  77.     memset(newstr, 0, sizeof(newstr));
  78.     mystrcpy(newstr, "");
  79.     //mystrcpy(newstr, "UNIX");
  80.     Replace(string, oldstr, newstr);
  81.     printf("将串中[%s]替换成[%s]\n替换后:string=[%s]\n", oldstr, newstr, string);
  82.     
  83.     STRING insertstr;
  84.     memset(insertstr, 0, sizeof(insertstr));
  85.     mystrcpy(insertstr, "SYSTEM");
  86.     StrInsert(string, 3, insertstr);
  87.     printf("串插入之后:string=[%s]\n", string);
  88.     
  89.     StrDelete(string, 10, 3);
  90.     printf("串删除之后:string=[%s]\n", string);
  91.     
  92.     memset(string, 0, sizeof(STRING));
  93.     strcpy(string, "0123456789");
  94.     printf("字符串=[%s]", string);
  95.     reverse1(string);
  96.     printf(" 反转之后=[%s]\n", string);

  97.     return 0;
  98. }

  99. /*功能:串插入*/
  100. int StrInsert(char * src, int pos, char * newstr)
  101. {
  102.     assert((src != NULL)&&(newstr != NULL)&&(pos >= 0)&&(pos < mystrlen(src)));
  103.     
  104.     int movelen = 0; /*移动的距离*/
  105.     int movenum = 0; /*移动的字符数*/
  106.     int movebeg = 0; /*起始移动位置*/
  107.     
  108.     movelen = mystrlen(newstr);
  109.     movenum = mystrlen(src) - pos;
  110.     movebeg = mystrlen(src) - 1;
  111.     
  112.     while(movenum-- > 0)
  113.     {
  114.         *(src+movebeg+movelen) = *(src+movebeg); /*右移*/
  115.         movebeg--;
  116.     }
  117.     
  118.     while(*newstr != '\0') /*空出来的地方赋值新串*/
  119.     {
  120.         *(src+pos) = *newstr;
  121.         src++;
  122.         newstr++;
  123.     }
  124.     
  125.     return 0;
  126. }

  127. /*功能:串删除
  128. 也可以使用memcpy(),strcpy(), strcat()函数来简化操作
  129. */
  130. int StrDelete(char * src, int pos, int len)
  131. {
  132.     assert((src != NULL)&&(len >= 0)&&(len <= mystrlen(src))&&(pos >= 0)&&(pos < mystrlen(src)-len));
  133.     
  134.     int movelen = 0; /*移动的距离*/
  135.     int movenum = 0; /*移动的字符数*/
  136.     int movebeg = 0; /*起始移动位置*/
  137.     
  138.     movelen = len;
  139.     movenum = mystrlen(src) - pos - len;
  140.     movebeg = pos + len;
  141.     
  142.     while(movenum-- > 0)
  143.     {
  144.         *(src+movebeg-movelen) = *(src+movebeg); /*左移*/
  145.         movebeg++;
  146.     }
  147.     
  148.     return 0;
  149. }

  150. /*功能:串置换
  151. 也可以使用memcpy(),strcpy(), strcat()函数来简化操作
  152. */
  153. int Replace(char * src, char * oldstr, char * newstr)
  154. {
  155.     assert((src != NULL)&&(oldstr != NULL)&&(newstr != NULL));
  156.     int i = 0; /*主串下标*/
  157.     int j = 0; /*子串下标*/
  158.     int movenum = 0; /*移动的字符数*/
  159.     int movelen = 0; /*移动的距离*/
  160.     int movebeg = 0; /*起始移动位置*/
  161.         
  162.     if(mystrlen(oldstr) == mystrlen(newstr)) /*置换的字符长度不变,则不需要移动串中的元素*/
  163.     {
  164.         i = 0;
  165.         while(i >= 0) /*i<0时:主串中没有oldstr字符串*/
  166.         {
  167.             i = Index(src, oldstr, i); /*在主串中定位oldstr字符串*/
  168.             if(i < 0) /*主串中已经没有oldstr串了*/
  169.                     break;
  170.             j = 0;
  171.             while(*(newstr+j) != '\0') /*置换*/
  172.             {
  173.                 *(src+i) = *(newstr+j);
  174.                 i++;
  175.                 j++;
  176.             }
  177.         }
  178.     }
  179.     else if(mystrlen(oldstr) < mystrlen(newstr)) /*主串中的后面的元素需要后移*/
  180.     {
  181.         i = 0;
  182.         while(i >= 0)
  183.         {
  184.             i = Index(src, oldstr, i); /*在主串中定位oldstr字符串*/
  185.             if(i < 0) /*主串中已经没有oldstr字符串了*/
  186.                     break;
  187.             movelen = mystrlen(newstr) - mystrlen(oldstr); /*移动距离*/
  188.             movenum = mystrlen(src) - i - mystrlen(oldstr); /*后移的字符数*/
  189.             movebeg = mystrlen(src) - 1; /*起始移动位置:是最后一个字符,'\0'就不需要移动了*/
  190.     
  191.             while(movenum-- > 0) /*需要移动完所有的字符*/
  192.             {
  193.                 *(src+movebeg+movelen) = *(src+movebeg);
  194.                 movebeg--; /*从最后一个字符开始,向后移动*/
  195.             }
  196.             
  197.             j = 0;
  198.             while(*(newstr+j) != '\0') /*置换*/
  199.             {
  200.                 *(src+i) = *(newstr+j);
  201.                 i++;
  202.                 j++;
  203.             }
  204.         }
  205.     }
  206.     else if(mystrlen(oldstr) > mystrlen(newstr)) /*主串中的后面的元素需要前移*/
  207.     {
  208.         i = 0;
  209.         while(i >= 0)
  210.         {
  211.             i = Index(src, oldstr, i); /*在主串中定位oldstr字符串*/
  212.             if(i < 0) /*主串中已经没有oldstr字符串了*/
  213.                     break;
  214.             movelen = mystrlen(oldstr) - mystrlen(newstr); /*移动距离*/
  215.             /*movenum:前移的字符数:应该把后面的清'\0',所以多移动几个字符*/
  216.             movenum = mystrlen(src) - i - mystrlen(oldstr) + movelen;
  217.             movebeg = i + mystrlen(oldstr); /*起始移动位置:是第1个字符*/
  218.     
  219.             while(movenum-- > 0) /*需要移动完所有的字符*/
  220.             {
  221.                 *(src+movebeg-movelen) = *(src+movebeg);
  222.                 movebeg++; /*从第一个字符开始,向前移动*/
  223.             }
  224.             
  225.             j = 0;
  226.             while(*(newstr+j) != '\0') /*置换*/
  227.             {
  228.                 *(src+i) = *(newstr+j);
  229.                 i++;
  230.                 j++;
  231.             }
  232.         }
  233.     }
  234.     return 0;
  235. }

  236. /*功能:串定位:*/
  237. int Index(char * src, char * substr, int pos)
  238. {
  239.     assert((src != NULL)&&(pos >= 0)&&(pos < mystrlen(src)));
  240.     int i = 0; /*主串下标*/
  241.     int j = 0; /*子串下标*/
  242.     while((i < mystrlen(src)-pos-1)&&(j < mystrlen(substr)))
  243.     {
  244.         if(*(src+pos+i) == *(substr+j)) /*有相等,则继续比较后继的字符*/
  245.         {
  246.             i++;
  247.             j++; /*直到比较完所有的子串*/
  248.         }
  249.         else /*i指针回退,重新开始匹配。i是主串下次匹配的起始地址,j是子串的起始地址*/
  250.         {
  251.             i = i-j+1; /*主串回到上一次比较的起始位置的下一个位置*/
  252.             j = 0;
  253.         }
  254.     }
  255.     if(j >= strlen(substr)) /*while循环结束时:j已经等于子串的长度*/
  256.         return pos + (i - strlen(substr)); /*pos下标之后:子串在主串中第一次出现的位置*/
  257.     else
  258.         return -1;
  259. }

  260. /*功能:求子串(用sub返回串src的第pos个字符起长度为len的子串)*/
  261. char * substring(char * sub, char * src, int pos, int len)
  262. {
  263.     assert((src != NULL)&&(pos >= 0)&&(pos <= mystrlen(src))&&(len > 0)&&(len < mystrlen(src)-pos));
  264.     char * addr = sub;
  265.     int i = 0;
  266.     while(i < len)
  267.     {
  268.         *(sub+i) = *(src+pos+i);
  269.         i++;
  270.     }
  271.     
  272.     return addr;
  273. }

  274. /*功能:串联接*/
  275. char * mystrcat(char * dest, char * src)
  276. {
  277.     assert((dest != NULL)&&(src != NULL));
  278.     char * addr = dest;
  279.     while(*dest != '\0')
  280.     {
  281.         dest++;
  282.     }
  283.     while((*dest++ = *src++) != '\0');
  284.     
  285.     return addr;
  286. }

  287. /*功能:清空串*/
  288. int clearstr(char * str)
  289. {
  290.     assert(str != NULL);
  291.     while(*str != '\0')
  292.     {
  293.         *str++ = '\0';
  294.     }
  295.     return 0;
  296. }

  297. /*功能:求串长*/
  298. int mystrlen(char * str)
  299. {
  300.     assert(str != NULL);
  301.     int len = 0;
  302.     while((*str++) != 0)
  303.     {
  304.         len++;
  305.     }
  306.     return len;
  307. }

  308. /*功能:串比较*/
  309. int mystrcmp(char * s1, char * s2)
  310. {
  311.     assert((s1 != NULL)&&(s2 != NULL));
  312.     int flag = 0;
  313.     while(flag == 0)
  314.     {
  315.         flag = *s1 - *s2;
  316.         if((flag == 0)&&(*s2 != '\0')) /*此处比较还相等,并且字符串还没有结束,要继续下一个比较*/
  317.         {
  318.             s1++;
  319.             s2++;
  320.             continue;
  321.         }
  322.         else if((flag == 0)&&(*s2 == '\0')) /*此处比较还相等,并且字符串已结束,说明字符串是相等*/
  323.         {
  324.             flag = 0;
  325.             break;
  326.         }
  327.         else if(flag != 0) /*此处比较不相等了,后面不需要比较了*/
  328.         {
  329.             break;
  330.         }
  331.     }
  332.     return flag;    
  333. }

  334. /*功能:判断串是否为空:1:空串,2:非空串*/
  335. int StrIsEmpty(char * str)
  336. {
  337.     assert(str != NULL);
  338.     if(*str == '\0') /*空串*/
  339.         return 1;
  340.     else /*非空串*/
  341.         return 2;
  342. }

  343. /************************************************************
  344.  *函数名:
  345.  *函数功能:字符串的复制
  346.  *函数参数:
  347.  *编程方法:
  348.  *1,
  349.     int i = 0;
  350.     while(*(src+i) != '\0')
  351.     {
  352.         *(dest+i) = *(src+i);
  353.         i++;
  354.     }
  355.  *2,
  356.     while(*src != '\0')
  357.     {
  358.         *dest = *src;
  359.         dest++;
  360.         src++;
  361.     }
  362.  *3,
  363.     while(*src != '\0')
  364.     {
  365.         *dest++ = *src++;
  366.     }
  367. *************************************************************/
  368. char * mystrcpy(char * dest, char * src)
  369. {
  370.     assert((dest != NULL)&&(src != NULL));
  371.     char * addr = dest;
  372.     while((*dest++ = *src++) != '\0');
  373.     return addr;
  374. }

  375. /*功能:将字符串反转的递归算法*/
  376. char * reverse1(char * str)
  377. {
  378.     if(str == NULL)
  379.         return NULL;
  380.     int len=strlen(str);
  381.     if(len > 1)
  382.     {
  383.         char ctemp=str[0];
  384.         str[0]=str[len-1];
  385.         str[len-1]='\0';
  386.         
  387.         reverse1(str+1);
  388.         str[len-1]=ctemp;
  389.     }
  390.     return str;
  391. }

  392. /*功能:将字符串反转的另一种递归算法*/
  393. char * reverse2(char * str)
  394. {
  395.     if(str == NULL)
  396.         return NULL;
  397.     int len = strlen(str);
  398.     if(len > 1)
  399.     {
  400.         char temp = str[0];
  401.         str[0] = str[len-1];
  402.         str[len-1] = temp;

  403.         char newstr[len+1];
  404.         memset(newstr, 0, sizeof(newstr));
  405.         int newlen = strlen(str) - sizeof(char)*2;
  406.         if(newlen > 1)
  407.         {
  408.             memcpy(newstr, str+sizeof(char)*1, sizeof(char)*newlen);
  409.             reverse2(newstr);
  410.             memcpy(str+sizeof(char)*1, newstr, sizeof(char)*newlen);
  411.         }
  412.     }
  413.     return str;
  414. }

  415. /*功能:非递归实现字符串的反转*/
  416. char * reverse3(char * str)
  417. {
  418.     if(str == NULL)
  419.         return NULL;
  420.     char * head = str;
  421.     char * tail = str + strlen(str) -1;
  422.     char temp;
  423.     while(head < str + strlen(str)/2)
  424.     {
  425.         temp = *head;
  426.         *head++ = *tail;
  427.         *tail-- = temp;
  428.     }
  429.     return str;
  430. }






























  431. /*
  432.  * linux/lib/string.c
  433.  *
  434.  * Copyright (C) 1991, 1992 Linus Torvalds
  435.  */

  436. extern void putc(unsigned char c);
  437. int puts(const char *s)
  438. {
  439.     int i = 0;
  440.     while (*s) {
  441.         putc(*s);
  442.         s++;
  443.         i++;
  444.     }
  445.     putc('\n');
  446.     putc('\r');
  447.     return i+2;
  448. }

  449. int putchar(int c)
  450. {
  451.     putc((unsigned char)c);
  452.     return c;
  453. }

  454. /**
  455.  * strnicmp - Case insensitive, length-limited string comparison
  456.  * @s1: One string
  457.  * @s2: The other string
  458.  * @len: the maximum number of characters to compare
  459.  */
  460. int strnicmp(const char *s1, const char *s2, size_t len)
  461. {
  462.     /* Yes, Virginia, it had better be unsigned */
  463.     unsigned char c1, c2;

  464.     c1 = 0;    c2 = 0;
  465.     if (len) {
  466.         do {
  467.             c1 = *s1; c2 = *s2;
  468.             s1++; s2++;
  469.             if (!c1)
  470.                 break;
  471.             if (!c2)
  472.                 break;
  473.             if (c1 == c2)
  474.                 continue;
  475.             c1 = tolower(c1);
  476.             c2 = tolower(c2);
  477.             if (c1 != c2)
  478.                 break;
  479.         } while (--len);
  480.     }
  481.     return (int)c1 - (int)c2;
  482. }

  483. char * ___strtok;

  484. /**
  485.  * strcpy - Copy a %NUL terminated string
  486.  * @dest: Where to copy the string to
  487.  * @src: Where to copy the string from
  488.  */
  489. char * strcpy(char * dest,const char *src)
  490. {
  491.     char *tmp = dest;

  492.     while ((*dest++ = *src++) != '\0')
  493.         /* nothing */;
  494.     return tmp;
  495. }

  496. /**
  497.  * strncpy - Copy a length-limited, %NUL-terminated string
  498.  * @dest: Where to copy the string to
  499.  * @src: Where to copy the string from
  500.  * @count: The maximum number of bytes to copy
  501.  *
  502.  * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
  503.  * However, the result is not %NUL-terminated if the source exceeds
  504.  * @count bytes.
  505.  */
  506. char * strncpy(char * dest,const char *src,size_t count)
  507. {
  508.     char *tmp = dest;

  509.     while (count-- && (*dest++ = *src++) != '\0')
  510.         /* nothing */;

  511.     return tmp;
  512. }

  513. /**
  514.  * strcat - Append one %NUL-terminated string to another
  515.  * @dest: The string to be appended to
  516.  * @src: The string to append to it
  517.  */
  518. char * strcat(char * dest, const char * src)
  519. {
  520.     char *tmp = dest;

  521.     while (*dest)
  522.         dest++;
  523.     while ((*dest++ = *src++) != '\0')
  524.         ;

  525.     return tmp;
  526. }

  527. /**
  528.  * strncat - Append a length-limited, %NUL-terminated string to another
  529.  * @dest: The string to be appended to
  530.  * @src: The string to append to it
  531.  * @count: The maximum numbers of bytes to copy
  532.  *
  533.  * Note that in contrast to strncpy, strncat ensures the result is
  534.  * terminated.
  535.  */
  536. char * strncat(char *dest, const char *src, size_t count)
  537. {
  538.     char *tmp = dest;

  539.     if (count) {
  540.         while (*dest)
  541.             dest++;
  542.         while ((*dest++ = *src++)) {
  543.             if (--count == 0) {
  544.                 *dest = '\0';
  545.                 break;
  546.             }
  547.         }
  548.     }

  549.     return tmp;
  550. }

  551. /**
  552.  * strcmp - Compare two strings
  553.  * @cs: One string
  554.  * @ct: Another string
  555.  */
  556. int strcmp(const char * cs,const char * ct)
  557. {
  558.     register signed char __res;

  559.     while (1) {
  560.         if ((__res = *cs - *ct++) != 0 || !*cs++)
  561.             break;
  562.     }

  563.     return __res;
  564. }

  565. /**
  566.  * strncmp - Compare two length-limited strings
  567.  * @cs: One string
  568.  * @ct: Another string
  569.  * @count: The maximum number of bytes to compare
  570.  */
  571. int strncmp(const char * cs,const char * ct,size_t count)
  572. {
  573.     register signed char __res = 0;

  574.     while (count) {
  575.         if ((__res = *cs - *ct++) != 0 || !*cs++)
  576.             break;
  577.         count--;
  578.     }

  579.     return __res;
  580. }

  581. /**
  582.  * strchr - Find the first occurrence of a character in a string
  583.  * @s: The string to be searched
  584.  * @c: The character to search for
  585.  */
  586. char * strchr(const char * s, int c)
  587. {
  588.     for(; *s != (char) c; ++s)
  589.         if (*s == '\0')
  590.             return NULL;
  591.     return (char *) s;
  592. }

  593. /**
  594.  * strrchr - Find the last occurrence of a character in a string
  595.  * @s: The string to be searched
  596.  * @c: The character to search for
  597.  */
  598. char * strrchr(const char * s, int c)
  599. {
  600.        const char *p = s + strlen(s);
  601.        do {
  602.            if (*p == (char)c)
  603.                return (char *)p;
  604.        } while (--p >= s);
  605.        return NULL;
  606. }

  607. /**
  608.  * strlen - Find the length of a string
  609.  * @s: The string to be sized
  610.  */
  611. size_t strlen(const char * s)
  612. {
  613.     const char *sc;

  614.     for (sc = s; *sc != '\0'; ++sc)
  615.         /* nothing */;
  616.     return sc - s;
  617. }

  618. /**
  619.  * strnlen - Find the length of a length-limited string
  620.  * @s: The string to be sized
  621.  * @count: The maximum number of bytes to search
  622.  */
  623. size_t strnlen(const char * s, size_t count)
  624. {
  625.     const char *sc;

  626.     for (sc = s; count-- && *sc != '\0'; ++sc)
  627.         /* nothing */;
  628.     return sc - s;
  629. }

  630. /**
  631.  * strspn - Calculate the length of the initial substring of @s which only
  632.  *     contain letters in @accept
  633.  * @s: The string to be searched
  634.  * @accept: The string to search for
  635.  */
  636. size_t strspn(const char *s, const char *accept)
  637. {
  638.     const char *p;
  639.     const char *a;
  640.     size_t count = 0;

  641.     for (p = s; *p != '\0'; ++p) {
  642.         for (a = accept; *a != '\0'; ++a) {
  643.             if (*p == *a)
  644.                 break;
  645.         }
  646.         if (*a == '\0')
  647.             return count;
  648.         ++count;
  649.     }

  650.     return count;
  651. }

  652. /**
  653.  * strpbrk - Find the first occurrence of a set of characters
  654.  * @cs: The string to be searched
  655.  * @ct: The characters to search for
  656.  */
  657. char * strpbrk(const char * cs,const char * ct)
  658. {
  659.     const char *sc1,*sc2;

  660.     for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  661.         for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  662.             if (*sc1 == *sc2)
  663.                 return (char *) sc1;
  664.         }
  665.     }
  666.     return NULL;
  667. }

  668. /**
  669.  * strtok - Split a string into tokens
  670.  * @s: The string to be searched
  671.  * @ct: The characters to search for
  672.  *
  673.  * WARNING: strtok is deprecated, use strsep instead.
  674.  */
  675. char * strtok(char * s,const char * ct)
  676. {
  677.     char *sbegin, *send;

  678.     sbegin = s ? s : ___strtok;
  679.     if (!sbegin) {
  680.         return NULL;
  681.     }
  682.     sbegin += strspn(sbegin,ct);
  683.     if (*sbegin == '\0') {
  684.         ___strtok = NULL;
  685.         return( NULL );
  686.     }
  687.     send = strpbrk( sbegin, ct);
  688.     if (send && *send != '\0')
  689.         *send++ = '\0';
  690.     ___strtok = send;
  691.     return (sbegin);
  692. }

  693. /**
  694.  * strsep - Split a string into tokens
  695.  * @s: The string to be searched
  696.  * @ct: The characters to search for
  697.  *
  698.  * strsep() updates @s to point after the token, ready for the next call.
  699.  *
  700.  * It returns empty tokens, too, behaving exactly like the libc function
  701.  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  702.  * Same semantics, slimmer shape. ;)
  703.  */
  704. char * strsep(char **s, const char *ct)
  705. {
  706.     char *sbegin = *s, *end;

  707.     if (sbegin == NULL)
  708.         return NULL;

  709.     end = strpbrk(sbegin, ct);
  710.     if (end)
  711.         *end++ = '\0';
  712.     *s = end;

  713.     return sbegin;
  714. }

  715. /**
  716.  * memset - Fill a region of memory with the given value
  717.  * @s: Pointer to the start of the area.
  718.  * @c: The byte to fill the area with
  719.  * @count: The size of the area.
  720.  *
  721.  * Do not use memset() to access IO space, use memset_io() instead.
  722.  */
  723. void * memset(void * s,int c,size_t count)
  724. {
  725.     char *xs = (char *) s;

  726.     while (count--)
  727.         *xs++ = c;

  728.     return s;
  729. }

  730. /**
  731.  * bcopy - Copy one area of memory to another
  732.  * @src: Where to copy from
  733.  * @dest: Where to copy to
  734.  * @count: The size of the area.
  735.  *
  736.  * Note that this is the same as memcpy(), with the arguments reversed.
  737.  * memcpy() is the standard, bcopy() is a legacy BSD function.
  738.  *
  739.  * You should not use this function to access IO space, use memcpy_toio()
  740.  * or memcpy_fromio() instead.
  741.  */
  742. void bcopy(const void *src, void *dest, size_t count)
  743. {
  744.     char *destTmp = (char *)dest;
  745.     char *srcTmp = (char *)src;

  746.     while (count--)
  747.         *destTmp++ = *srcTmp++;
  748. }

  749. /**
  750.  * memcpy - Copy one area of memory to another
  751.  * @dest: Where to copy to
  752.  * @src: Where to copy from
  753.  * @count: The size of the area.
  754.  *
  755.  * You should not use this function to access IO space, use memcpy_toio()
  756.  * or memcpy_fromio() instead.
  757.  */
  758. void * memcpy(void * dest,const void *src,size_t count)
  759. {
  760.     char *tmp = (char *) dest, *s = (char *) src;

  761.     while (count--)
  762.         *tmp++ = *s++;

  763.     return dest;
  764. }

  765. /**
  766.  * memmove - Copy one area of memory to another
  767.  * @dest: Where to copy to
  768.  * @src: Where to copy from
  769.  * @count: The size of the area.
  770.  *
  771.  * Unlike memcpy(), memmove() copes with overlapping areas.
  772.  */
  773. void * memmove(void * dest,const void *src,size_t count)
  774. {
  775.     char *tmp, *s;

  776.     if (dest <= src) {
  777.         tmp = (char *) dest;
  778.         s = (char *) src;
  779.         while (count--)
  780.             *tmp++ = *s++;
  781.         }
  782.     else {
  783.         tmp = (char *) dest + count;
  784.         s = (char *) src + count;
  785.         while (count--)
  786.             *--tmp = *--s;
  787.         }

  788.     return dest;
  789. }

  790. /**
  791.  * memcmp - Compare two areas of memory
  792.  * @cs: One area of memory
  793.  * @ct: Another area of memory
  794.  * @count: The size of the area.
  795.  */
  796. int memcmp(const void * cs,const void * ct,size_t count)
  797. {
  798.     const unsigned char *su1, *su2;
  799.     int res = 0;

  800.     for( su1 = (const unsigned char *)cs, su2 = (const unsigned char *)ct; 0 < count; ++su1, ++su2, count--)
  801.         if ((res = *su1 - *su2) != 0)
  802.             break;
  803.     return res;
  804. }

  805. /**
  806.  * memscan - Find a character in an area of memory.
  807.  * @addr: The memory area
  808.  * @c: The byte to search for
  809.  * @size: The size of the area.
  810.  *
  811.  * returns the address of the first occurrence of @c, or 1 byte past
  812.  * the area if @c is not found
  813.  */
  814. void * memscan(void * addr, int c, size_t size)
  815. {
  816.     unsigned char * p = (unsigned char *) addr;

  817.     while (size) {
  818.         if (*p == c)
  819.             return (void *) p;
  820.         p++;
  821.         size--;
  822.     }
  823.       return (void *) p;
  824. }
  825. #endif

  826. /**
  827.  * strstr - Find the first substring in a %NUL terminated string
  828.  * @s1: The string to be searched
  829.  * @s2: The string to search for
  830.  */
  831. char * strstr(const char * s1,const char * s2)
  832. {
  833.     int l1, l2;

  834.     l2 = strlen(s2);
  835.     if (!l2)
  836.         return (char *) s1;
  837.     l1 = strlen(s1);
  838.     while (l1 >= l2) {
  839.         l1--;
  840.         if (!memcmp(s1,s2,l2))
  841.             return (char *) s1;
  842.         s1++;
  843.     }
  844.     return NULL;
  845. }

  846. /**
  847.  * memchr - Find a character in an area of memory.
  848.  * @s: The memory area
  849.  * @c: The byte to search for
  850.  * @n: The size of the area.
  851.  *
  852.  * returns the address of the first occurrence of @c, or %NULL
  853.  * if @c is not found
  854.  */
  855. void *memchr(const void *s, int c, size_t n)
  856. {
  857.     const unsigned char *p = (const unsigned char *)s;
  858.     while (n-- != 0) {
  859.             if ((unsigned char)c == *p++) {
  860.             return (void *)(p-1);
  861.         }
  862.     }
  863.     return NULL;
  864. }

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

上一篇:SPI裸机驱动

下一篇:链式队列

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