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


Groups > comp.lang.c > #15329

Re: strtok

From blp@cs.stanford.edu (Ben Pfaff)
Newsgroups comp.lang.c
Subject Re: strtok
Date 2012-01-02 10:04 -0800
Message-ID <8762gu0ymv.fsf@blp.benpfaff.org> (permalink)
References <4f00c25d$0$11612$bbae4d71@news.suddenlink.net>

Show all headers | View raw


"Bill Cunningham" <nospam@nspam.invalid> writes:

>     Why does my man page for strtok and an extension for a thread safe 
> version of this function say not to use these functions? Are they buggy or 
> deprecated? What should one use instead?

strtok() has at least these problems:

	* It merges adjacent delimiters.  If you use a comma as your
          delimiter, then "a,,b,c" will be divided into three tokens,
          not four.  This is often the wrong thing to do.  In fact, it
          is only the right thing to do, in my experience, when the
          delimiter set contains white space (for dividing a string
          into "words") or it is known in advance that there will be
          no adjacent delimiters.

	* The identity of the delimiter is lost, because it is
          changed to a null terminator.

	* It modifies the string that it tokenizes.  This is bad
          because it forces you to make a copy of the string if
          you want to use it later.  It also means that you can't
          tokenize a string literal with it; this is not
          necessarily something you'd want to do all the time but
          it is surprising.

	* It can only be used once at a time.  If a sequence of
          strtok() calls is ongoing and another one is started,
          the state of the first one is lost.  This isn't a
          problem for small programs but it is easy to lose track
          of such things in hierarchies of nested functions in
          large programs.  In other words, strtok() breaks
          encapsulation.

-- 
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

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


Thread

strtok "Bill Cunningham" <nospam@nspam.invalid> - 2012-01-01 15:30 -0500
  Re: strtok Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-01 12:41 -0800
    Re: strtok "Bill Cunningham" <nospam@nspam.invalid> - 2012-01-01 16:26 -0500
      Re: strtok John Tsiombikas <nuclear@member.fsf.org> - 2012-01-02 12:50 +0000
  Re: strtok Johann Klammer <klammerr@NOSPAM.aon.at> - 2012-01-01 21:53 +0100
  Re: strtok gordonb.o4t45@burditt.org (Gordon Burditt) - 2012-01-01 15:04 -0600
  Re: strtok "osmium" <r124c4u102@comcast.net> - 2012-01-01 15:10 -0600
    Re: strtok jacob navia <jacob@spamsink.net> - 2012-01-01 23:25 +0100
  Re: strtok Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-01 21:37 +0000
    Re: strtok "Bill Cunningham" <nospam@nspam.invalid> - 2012-01-01 16:58 -0500
  Re: strtok blp@cs.stanford.edu (Ben Pfaff) - 2012-01-02 10:04 -0800
    Re: strtok David Thompson <dave.thompson2@verizon.net> - 2012-01-19 02:48 -0500

csiph-web