Re: Does strtok require a non-null token?
- From: Clever Monkey <clvrmnky.invalid@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 13 Oct 2006 09:50:32 -0400
William Hughes wrote:
Default User wrote:Perhaps. About the only thing surprising to me was that the argument you pass it is affected.William Hughes wrote:The point is not that the function's behaviour is not sometimes
ryampolsky@xxxxxxxxx wrote:Unless that's the behavior you want. Example, breaking lines into wordsI'm using strtok to break apart a colon-delimited string. ItYes. Just one more reason to avoid strtok().
basically works, but it looks like strtok skips over empty
sections. In other words, if the string has 2 colons in a row, it
doesn't treat that as a null token, it just treats the 2 colons as
a single delimiter.
Is that the intended behavior?
with white space. You don't want a bunch of "null" words.
what you want. The point is
-the default behaviour is surprising
-the default behaviour is not evenSo far I've only ever needed the default behaviour with respect to collapsing adjacent tokens. In fact, I *expected* this! That is, for the majority of the reasons I need to tokenized a string, this default behaviour is exactly what I want.
usually what you want
-the default behaviour throws information awayNot sure what you mean here, but I assume you are referring to how it munges its argument. I guess I just never care about this because we always store strings in a struct that is passed around, or make copies of things we tokenized and care about.
-if you don't like the default behaviour, seeI assume figure 1 is a picture of your own implementation that has non-default requirements :)
figure 1.
Personally I'm with the Linux man pages on this one. Under BugsWell, I'll ignore this advice. For the trivial case of needing tokenized a string to store in my own array of buffers, it works just fine.
is the advice "Never use this function".
For those requirements that strtok() does not fit we have our own internal tokenizing routines. If all I need is to parse out (say) a bunch of email addresses passed as a list and store them in a char** [which was the last time I used strtok()] then it fits perfectly. In this case I don't even care if the calling code screwed up the list. I either get one or more valid strings or I don't. I return success or failure and let them howl!
Of course, if I'd been bitten by the function in the past, I'd be arguing differently.
Many of the str_ routines in the Standard have some legacy use that explains design decisions [e.g., strncpy() and database column width]. I wonder if strtok() also has history that explains why the defaults cause so much consternation?
.
- Follow-Ups:
- Re: Does strtok require a non-null token?
- From: William Hughes
- Re: Does strtok require a non-null token?
- References:
- Does strtok require a non-null token?
- From: ryampolsky
- Re: Does strtok require a non-null token?
- From: William Hughes
- Re: Does strtok require a non-null token?
- From: Default User
- Re: Does strtok require a non-null token?
- From: William Hughes
- Does strtok require a non-null token?
- Prev by Date: Re: distant compilation and float literals
- Next by Date: Re: _JoyDef and _Control
- Previous by thread: Re: Does strtok require a non-null token?
- Next by thread: Re: Does strtok require a non-null token?
- Index(es):
Relevant Pages
|