鱼C论坛

 找回密码
 立即注册
查看: 2444|回复: 3

关于strok函数的传参问题

[复制链接]
发表于 2018-4-30 13:22:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么第二次以后strokk函数的第一个参数要传NULL呢?这样不就没法分割指定字符串了吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-30 19:44:50 | 显示全部楼层
  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.

  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.

  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10. Library General Public License for more details.

  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB.  If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA.  */

  15. #include <ansidecl.h>
  16. #include <errno.h>
  17. #include <string.h>


  18. static char *olds = NULL;

  19. /* Parse S into tokens separated by characters in DELIM.
  20.    If S is NULL, the last string strtok() was called with is
  21.    used.  For example:
  22.         char s[] = "-abc=-def";
  23.         x = strtok(s, "-");                // x = "abc"
  24.         x = strtok(NULL, "=-");                // x = "def"
  25.         x = strtok(NULL, "=");                // x = NULL
  26.                 // s = "abc\0-def\0"
  27. */
  28. char *
  29. DEFUN(strtok, (s, delim),
  30.       register char *s AND register CONST char *delim)
  31. {
  32.   char *token;

  33.   if (s == NULL)
  34.     {
  35.       if (olds == NULL)
  36.         {
  37.           errno = EINVAL;
  38.           return NULL;
  39.         }
  40.       else
  41.         s = olds;
  42.     }

  43.   /* Scan leading delimiters.  */
  44.   s += strspn(s, delim);
  45.   if (*s == '\0')
  46.     {
  47.       olds = NULL;
  48.       return NULL;
  49.     }

  50.   /* Find the end of the token.  */
  51.   token = s;
  52.   s = strpbrk(token, delim);
  53.   if (s == NULL)
  54.     /* This token finishes the string.  */
  55.     olds = NULL;
  56.   else
  57.     {
  58.       /* Terminate the token and make OLDS point past it.  */
  59.       *s = '\0';
  60.       olds = s + 1;
  61.     }
  62.   return token;
  63. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-30 20:33:31 | 显示全部楼层
  1. #include <errno.h>


  2. /* Return the length of the maximum initial segment
  3. of S which contains only characters in ACCEPT.  */
  4. size_t strspn(char *s, const char *accept)
  5. {
  6.         register const char *p;
  7.         register const char *a;
  8.         register size_t count = 0;

  9.         for(p = s; *p != '\0'; ++p)
  10.         {
  11.                 for(a = accept; *a != '\0'; ++a)
  12.                         if(*p == *a)
  13.                                 break;
  14.                 if(*a == '\0')
  15.                         return count;
  16.                 else
  17.                         ++count;
  18.         }

  19.         return count;
  20. }


  21. void __cdecl exit(int _Code);
  22. /* Cause an abnormal program termination with core-dump.  */
  23. void abort(void)
  24. {
  25.         exit(127);
  26. }


  27. /* Find the first ocurrence of C in S.  */
  28. char *strchr(const char *s, int c)
  29. {
  30.         const unsigned char *char_ptr;
  31.         const unsigned long int *longword_ptr;
  32.         unsigned long int longword, magic_bits, charmask;

  33.         c = (unsigned char)c;

  34.         /* Handle the first few characters by reading one character at a time.
  35.         Do this until CHAR_PTR is aligned on a longword boundary.  */
  36.         for(char_ptr = s; ((unsigned long int) char_ptr
  37.                 & (sizeof(longword) - 1)) != 0;
  38.                 ++char_ptr)
  39.                 if(*char_ptr == c)
  40.                         return (void *)char_ptr;
  41.                 else if(*char_ptr == '\0')
  42.                         return NULL;

  43.         /* All these elucidatory comments refer to 4-byte longwords,
  44.         but the theory applies equally well to 8-byte longwords.  */

  45.         longword_ptr = (unsigned long int *) char_ptr;

  46.         /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
  47.         the "holes."  Note that there is a hole just to the left of
  48.         each byte, with an extra at the end:

  49.         bits:  01111110 11111110 11111110 11111111
  50.         bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

  51.         The 1-bits make sure that carries propagate to the next 0-bit.
  52.         The 0-bits provide holes for carries to fall into.  */
  53.         switch(sizeof(longword))
  54.         {
  55.         case 4: magic_bits = 0x7efefeffL; break;
  56.         case 8: magic_bits = (0x7efefefeL << 32) | 0xfefefeffL; break;
  57.         default:
  58.                 abort();
  59.         }

  60.         /* Set up a longword, each of whose bytes is C.  */
  61.         charmask = c | (c << 8);
  62.         charmask |= charmask << 16;
  63.         if(sizeof(longword) > 4)
  64.                 charmask |= charmask << 32;
  65.         if(sizeof(longword) > 8)
  66.                 abort();

  67.         /* Instead of the traditional loop which tests each character,
  68.         we will test a longword at a time.  The tricky part is testing
  69.         if *any of the four* bytes in the longword in question are zero.  */
  70.         for(;;)
  71.         {
  72.                 /* We tentatively exit the loop if adding MAGIC_BITS to
  73.                 LONGWORD fails to change any of the hole bits of LONGWORD.

  74.                 1) Is this safe?  Will it catch all the zero bytes?
  75.                 Suppose there is a byte with all zeros.  Any carry bits
  76.                 propagating from its left will fall into the hole at its
  77.                 least significant bit and stop.  Since there will be no
  78.                 carry from its most significant bit, the LSB of the
  79.                 byte to the left will be unchanged, and the zero will be
  80.                 detected.

  81.                 2) Is this worthwhile?  Will it ignore everything except
  82.                 zero bytes?  Suppose every byte of LONGWORD has a bit set
  83.                 somewhere.  There will be a carry into bit 8.  If bit 8
  84.                 is set, this will carry into bit 16.  If bit 8 is clear,
  85.                 one of bits 9-15 must be set, so there will be a carry
  86.                 into bit 16.  Similarly, there will be a carry into bit
  87.                 24.  If one of bits 24-30 is set, there will be a carry
  88.                 into bit 31, so all of the hole bits will be changed.

  89.                 The one misfire occurs when bits 24-30 are clear and bit
  90.                 31 is set; in this case, the hole at bit 31 is not
  91.                 changed.  If we had access to the processor carry flag,
  92.                 we could close this loophole by putting the fourth hole
  93.                 at bit 32!

  94.                 So it ignores everything except 128's, when they're aligned
  95.                 properly.

  96.                 3) But wait!  Aren't we looking for C as well as zero?
  97.                 Good point.  So what we do is XOR LONGWORD with a longword,
  98.                 each of whose bytes is C.  This turns each byte that is C
  99.                 into a zero.  */

  100.                 longword = *longword_ptr++;

  101.                 /* Add MAGIC_BITS to LONGWORD.  */
  102.                 if((((longword + magic_bits)

  103.                         /* Set those bits that were unchanged by the addition.  */
  104.                         ^ ~longword)

  105.                         /* Look at only the hole bits.  If any of the hole bits
  106.                         are unchanged, most likely one of the bytes was a
  107.                         zero.  */
  108.                         & ~magic_bits) != 0 ||

  109.                         /* That caught zeroes.  Now test for C.  */
  110.                         ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
  111.                                 & ~magic_bits) != 0)
  112.                 {
  113.                         /* Which of the bytes was C or zero?
  114.                         If none of them were, it was a misfire; continue the search.  */

  115.                         const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);

  116.                         if(*cp == c)
  117.                                 return (char *)cp;
  118.                         else if(*cp == '\0')
  119.                                 return NULL;
  120.                         if(*++cp == c)
  121.                                 return (char *)cp;
  122.                         else if(*cp == '\0')
  123.                                 return NULL;
  124.                         if(*++cp == c)
  125.                                 return (char *)cp;
  126.                         else if(*cp == '\0')
  127.                                 return NULL;
  128.                         if(*++cp == c)
  129.                                 return (char *)cp;
  130.                         else if(*cp == '\0')
  131.                                 return NULL;
  132.                         if(sizeof(longword) > 4)
  133.                         {
  134.                                 if(*++cp == c)
  135.                                         return (char *)cp;
  136.                                 else if(*cp == '\0')
  137.                                         return NULL;
  138.                                 if(*++cp == c)
  139.                                         return (char *)cp;
  140.                                 else if(*cp == '\0')
  141.                                         return NULL;
  142.                                 if(*++cp == c)
  143.                                         return (char *)cp;
  144.                                 else if(*cp == '\0')
  145.                                         return NULL;
  146.                                 if(*++cp == c)
  147.                                         return (char *)cp;
  148.                                 else if(*cp == '\0')
  149.                                         return NULL;
  150.                         }
  151.                 }
  152.         }

  153.         return NULL;
  154. }



  155. /* Find the first ocurrence in S of any character in ACCEPT.  */
  156. char *strpbrk(register const char *s, register const char *accept)
  157. {
  158.         while(*s != '\0')
  159.                 if(strchr(accept, *s) == NULL)
  160.                         ++s;
  161.                 else
  162.                         return (char *)s;

  163.         return NULL;
  164. }




  165. static char *olds = NULL;

  166. /* Parse S into tokens separated by characters in DELIM.
  167. If S is NULL, the last string strtok() was called with is
  168. used.  For example:
  169. char s[] = "-abc=-def";
  170. x = strtok(s, "-");                // x = "abc"
  171. x = strtok(NULL, "=-");                // x = "def"
  172. x = strtok(NULL, "=");                // x = NULL
  173. // s = "abc\0-def\0"
  174. */

  175. char *strtok(register char *s, register const char *delim)
  176. {
  177.         char *token;

  178.         if(s == NULL)
  179.         {
  180.                 if(olds == NULL)
  181.                 {
  182.                         errno = EINVAL;
  183.                         return NULL;
  184.                 }
  185.                 else
  186.                         s = olds;
  187.         }

  188.         /* Scan leading delimiters.  */
  189.         s += strspn(s, delim);
  190.         if(*s == '\0')
  191.         {
  192.                 olds = NULL;
  193.                 return NULL;
  194.         }

  195.         /* Find the end of the token.  */
  196.         token = s;
  197.         s = strpbrk(token, delim);
  198.         if(s == NULL)
  199.                 /* This token finishes the string.  */
  200.                 olds = NULL;
  201.         else
  202.         {
  203.                 /* Terminate the token and make OLDS point past it.  */
  204.                 *s = '\0';
  205.                 olds = s + 1;
  206.         }
  207.         return token;
  208. }

  209. #include <stdio.h>

  210. int main(void)
  211. {
  212.         char buf[] = "hello@boy@this@is@heima";
  213.         char *temp = strtok(buf, "@");

  214.         while(temp)
  215.         {
  216.                 printf("%s ", temp);
  217.                 temp = strtok(NULL, "@");
  218.         }

  219.         return 0;
  220. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-30 20:34:22 | 显示全部楼层
  1. hello boy this is heima 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 03:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表