strasp: a new string "splitter"

Erick Bourgeois erick@jeb.ca
Tue Dec 23 01:23:00 GMT 2003


Hi GCC developers,

I wrote this function (strasp()) in the hopes to address the main (in my opinion)
drawback with strtok(3) and strsep(3). That is, in both cases the string to be tokenized
is modified, and in many cases I did not want this. (Although, I could make a copy of
the string before sending it to either function, this is not what I wanted either.)
This package also includes a shell-like (main.c) program to test strasp(), after
configuring, `make all' should compile `strasp'.

DESCRIPTION

strasp() seperates (or "splits") a string according to a string of delimeters
and returns an array of pointers to *newly* allocated strings (i.e., an char **).

The original string sent to strasp() will not be modified in any way. However, the draw
 back to this is that the newly allocated array needs to be free'd(). The convenience
 function strasp_free() is supplied (defined in strasp.c) to free the array of char *'s.

	- strasp() runs (in the worst case) < O(n^^2), where n = strlen(str)
	one pass to determine the array's length, anf the second to populate it.

strasp() uses the following functions: strchr(), strlen(), malloc(), calloc(), free(),
and memcpy(); it does not call the strpbrk() function.

(strasp stands for "string array seperator")

I thought maybe that strasp() could be of use, so I packaged it up and it can be retrieved
from:
	http://jeb.ca/c/strasp/ 
The current version is 1.6

Even if you would not like to incorporate this function in anyway, it would be much
appreciated if any advice could be given on how to optimize better.

Thanks in advance,
-- 
Erick



More information about the Gcc mailing list