Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #83839

Re: strtok() implementation

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: strtok() implementation
Date 2016-03-14 15:26 +1300
Message-ID <dkmlqfF53ugU1@mid.individual.net> (permalink)
References <56e59164$0$27830$426a74cc@news.free.fr> <56e5c7fe$0$9212$426a74cc@news.free.fr> <nc4i81$qr9$1@dont-email.me> <56e5e0dc$0$9225$426a74cc@news.free.fr> <56e5e88f$0$3047$426a74cc@news.free.fr>

Show all headers | View raw


On 03/14/16 11:25, boon wrote:
> On 03/13/2016 10:52 PM, boon wrote:
>> On 03/13/2016 09:26 PM, Eric Sosman wrote:
>>> On 3/13/2016 4:06 PM, boon wrote:
>>>> On 03/13/2016 05:13 PM, boon wrote:
>
> [...]
>
>>
>> In fact both of them may be used to implement strtok(). Here is my new
>> version :
>>
>> char *my_strtok(char *str, const char *delim)
>> {
>>       char *ret;
>>       static char *saveptr;
>>
>>       if (!str && !saveptr)
>>           return NULL;
>>
>>       if (str)
>>           saveptr = str;
>>
>>       saveptr += strspn(saveptr, delim);
>>       ret = saveptr;
>>       saveptr += strcspn(saveptr, delim);
>>
>>       if (*saveptr == '\0') {
>>           saveptr = NULL;
>>           return ret;
>>       }
>>
>>       *saveptr++ = '\0';
>>
>>       return ret;
>> }
>>
>> Sounds correct to me. Note that the string parsing is stopped only when
>> both str and saveptr are NULL.
>>
>> saveptr value is reset when its last character is null character.
>
> Ooops! I meant, 'saveptr' is reset to NULL value when it points to a
> null string (made of a unique null character).

This fails two of my tests:

{
   char data[] = "  ";
   const char* delim = " ";

   CPPUNIT_ASSERT( !my_strtok( data, delim ) );
}

and

{
   char data[] = "A B ";
   const char* delim = " ";

   my_strtok( data, delim );
   my_strtok( NULL, delim );

   char* p = my_strtok( NULL, delim );

   CPPUNIT_ASSERT( !p );
}

-- 
Ian Collins

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

strtok() implementation boon <root@localhost> - 2016-03-13 17:13 +0100
  Re: strtok() implementation Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-03-13 10:04 -0700
    Re: strtok() implementation boon <root@localhost> - 2016-03-13 18:51 +0100
  Re: strtok() implementation Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-13 13:38 -0400
    Re: strtok() implementation boon <root@localhost> - 2016-03-13 19:05 +0100
      Re: strtok() implementation Keith Thompson <kst-u@mib.org> - 2016-03-13 13:50 -0700
        Re: strtok() implementation boon <root@localhost> - 2016-03-13 23:10 +0100
  Re: strtok() implementation boon <root@localhost> - 2016-03-13 21:06 +0100
    Re: strtok() implementation Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-13 16:26 -0400
      Re: strtok() implementation boon <root@localhost> - 2016-03-13 22:52 +0100
        Re: strtok() implementation boon <root@localhost> - 2016-03-13 23:25 +0100
          Re: strtok() implementation Ian Collins <ian-news@hotmail.com> - 2016-03-14 15:26 +1300
            Re: strtok() implementation boon <root@localhost.localdomain> - 2016-03-14 12:44 +0100
              Re: strtok() implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-03-17 08:23 -0700
                Re: strtok() implementation boon <root@localhost> - 2016-03-18 21:09 +0100
                Re: strtok() implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-03-19 14:21 -0700
                Re: strtok() implementation Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2016-03-19 16:25 -0500
                Re: strtok() implementation boon <fred900rbc@gmail.com> - 2016-03-24 13:05 -0700
                Re: strtok() implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-03-30 09:13 -0700
                Re: strtok() implementation Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2016-03-30 14:44 -0500
                Re: strtok() implementation boon <root@127.10.10.1> - 2016-03-31 10:24 +0200
                Re: strtok() implementation Tim Rentsch <txr@alumni.caltech.edu> - 2016-04-05 12:23 -0700
  Re: strtok() implementation Ian Collins <ian-news@hotmail.com> - 2016-03-14 15:31 +1300
    Re: strtok() implementation boon <root@localhost> - 2016-03-14 20:13 +0100
      Re: strtok() implementation Ian Collins <ian-news@hotmail.com> - 2016-03-15 09:48 +1300
        Re: strtok() implementation Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-03-14 14:05 -0700
          Re: strtok() implementation Ian Collins <ian-news@hotmail.com> - 2016-03-15 10:09 +1300
            Re: strtok() implementation Richard Heathfield <rjh@cpax.org.uk> - 2016-03-14 22:02 +0000
              Re: strtok() implementation Gareth Owen <gwowen@gmail.com> - 2016-03-14 22:16 +0000
          Re: strtok() implementation Keith Thompson <kst-u@mib.org> - 2016-03-14 14:50 -0700
          Re: strtok() implementation raltbos@xs4all.nl (Richard Bos) - 2016-03-14 22:06 +0000
            Re: strtok() implementation boon <root@localhost> - 2016-03-15 22:14 +0100
              Re: strtok() implementation BartC <bc@freeuk.com> - 2016-03-15 21:23 +0000
              Re: strtok() implementation raltbos@xs4all.nl (Richard Bos) - 2016-03-17 12:27 +0000
        Re: strtok() implementation boon <root@localhost> - 2016-03-15 22:04 +0100
          Re: strtok() implementation Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-15 18:18 -0400
            Re: strtok() implementation boon <root@localhost> - 2016-03-18 21:19 +0100
        Re: strtok() implementation boon <root@localhost> - 2016-03-15 22:08 +0100
  Re: strtok() implementation boon <root@localhost> - 2016-03-15 22:32 +0100

csiph-web