strtok() considered evil

at 2005-01-18 in General by kiesel (0 comments)

While experimenting with tokenizing of strings today, Christian Lang and me came to one point where code using strtok() silently failed: reason to this was the nested use of strtok() in different places on different strings.

strtok() has an internal pointer that stores the string the operation takes place on and the current position on that string. To initialize these values, a first call to the function is issued with the tokens to search and the string as arguments. Following commands only need to specify the tokens.

The issue with this is that the internal pointers are no stack - you cannot "push" one string over another and pop it off when you're finished. Unfortunately, the original string is just overwritten by the later one.

In the case of classes, you can never know, whether any caller on another level is currently tokenizing a string - so keep your hands off this function if you don't want to break code.

The XP framework offers a nice class text.StringTokenzier that can take strtok's part with glance.

(Note: this issue has also been documented in the XP framework documentation on the use of native method function which can be found here.)



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases

Related

Find related articles by a search for «strtok()».