X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstring%2Fstrtok.c;h=69b45a73cb8aeb0753395907425beed9643ce13f;hb=7832fa07b60f2176efd9e9e4746246717d3e8949;hp=1e588e84bb1e52d9390c83bab8945eec82ca5b44;hpb=34893ecc2200dc7017c36a54cb6c5f4c2378b5ec;p=pdclib diff --git a/functions/string/strtok.c b/functions/string/strtok.c index 1e588e8..69b45a7 100644 --- a/functions/string/strtok.c +++ b/functions/string/strtok.c @@ -5,4 +5,35 @@ // This code is Public Domain. Use, modify, and redistribute at will. // ---------------------------------------------------------------------------- -char * strtok( char * restrict s1, const char * restrict s2 ) { /* TODO */ }; +char * strtok( char * restrict src, const char * restrict seperators ) +{ + static char * store = NULL; + size_t token_length; + + if ( src != NULL ) + { + // new string + store = src; + } + if ( store == NULL ) + { + // no old string, no new string, nothing to do + return NULL; + } + src += strspn( src, seperators ); // skipping leading seperators + if ( strlen( src ) == 0 ) + { + // no more to parse + return ( store = NULL ); + } + token_length = strcspn( src, seperators ); + if ( src[ token_length ] == '\0' ) + { + // parsed to end of string + store = NULL; + return src; + } + src[ token_length ] = '\0'; + store = src + token_length + 1; + return src; +}