Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using strtok to mark delimiters as tokens

Reply
Thread Tools

using strtok to mark delimiters as tokens

 
 
jaysome
Guest
Posts: n/a
 
      03-13-2008
On Wed, 12 Mar 2008 10:42:28 +0000, Richard Bos wrote:

> Falcon Kirtaran <(E-Mail Removed)> wrote:
>
>> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> > i wanted to know whether we can use strtok command to mark delimiters
>> > as tokens as well.In Java,we have a command:

>
>> I don't think there is. This is because the function is generally
>> implemented by replacing the delimiter with a null.

>
> s/generally/required to be/.
>
>> It'd be really easy to write your own, however, that preserves the
>> delimiting character. For instance, it might look something like:
>>
>> char * strtok_d(char * str, const char * tokens, char * delim) {

>
> Don't call it that, though. Names starting in str and a letter are
> reserved for the implementation's use.


Strictly speaking, this is not true.

The standard states that function names that begin with str and a
*lowercase* letter may be reserved. Thus, for example, a
function name such as stricmp is reserved, whereas a function name such
as strIcmp is not reserved--the user is free to define such a function.

In my opinion, if you want to both define a function name that begins
with "str" and at the same time avoid using a reserved identifier, then
use a name that begins with "str_", i.e., str_icmp.

If this seems overly pedantic, then there's a good chance I'd agree with
you.

For example, the function name strip_leading_white_space will never, ever
conflict with a function name defined by an implementation (the
implementation would in all probability, if it in fact defined such a
function, use something such as stripleadingwhitespace or striplws or
strlws or some other name).

The probability that an identifier you define conflicts with an
identifier defined by the implementation decreases exponentially,
multiplied by some constant, with the number of underscores used in your
identifier.

In my experience, a single underscore is all that is needed, even if your
identifier does begin with "str" and (followed by) a lowercase letter.

--
jay
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      03-13-2008
Falcon Kirtaran wrote:
> Mark Bluemel wrote:
>> Falcon Kirtaran wrote:
>>> (E-Mail Removed) wrote:
>>>> hi,
>>>> i wanted to know whether we can use strtok command to mark delimiters
>>>> as tokens as well.In Java,we have a command:

>>
>> [snip]
>>
>>> It'd be really easy to write your own, however, that preserves the
>>> delimiting character. For instance, it might look something like:
>>>
>>> char * strtok_d(char * str, const char * tokens, char * delim) {

>>
>> [snip]
>>
>>> };
>>>
>>> The only thing you'd need to take care of is that both parameters
>>> must be null-terminated strings. Also, you'd have to check if the
>>> third parameter when dereferenced is not 0. Obviously, the third
>>> parameter is just a single char (by reference), not a string.
>>>
>>> [snip] Calls to this can't be mixed interchangeably with calls to
>>> strtok().

>>
>> And due to the (very dubious) design decision to hold state in a static
>> variable, it is not thread-safe or even capable of being used on more
>> than one string at a time, for example if you wanted to further parse
>> a field extracted from a string before moving on to the next.
>>
>> There are better solutions out there, I'm sure.

>
> strtok() itself is not thread-safe, however, and also can't be used on
> more than one string at a time.


Yeah, so if you're writing a replacement, you might as well write a
better one...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To Mark Fitzpatrick, Juan Libre, Mark Rae, Steve Orr, Cowboy and other MVPs Cirene ASP .Net 5 05-17-2008 07:17 PM
strtok behavior with multiple consecutive delimiters Geometer C Programming 34 05-09-2006 02:32 PM
strtok behavior with multiple consecutive delimiters Geometer C++ 33 05-09-2006 02:32 PM
Problems with strtok() returning one too many tokens... Adam Balgach C++ 2 11-28-2004 01:12 AM



Advertisments