X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fwchar%2Fwcstok.c;fp=functions%2Fwchar%2Fwcstok.c;h=62f8dd8764d47af8fe36a5a8317e984b609914f1;hb=14b5b78e19a79f590233caee7848e7cb19084088;hp=0000000000000000000000000000000000000000;hpb=41777e3d5dc655f59a8c3bcd071639e6878e853a;p=pdclib diff --git a/functions/wchar/wcstok.c b/functions/wchar/wcstok.c new file mode 100644 index 0000000..62f8dd8 --- /dev/null +++ b/functions/wchar/wcstok.c @@ -0,0 +1,109 @@ +/* wcstok( wchar_t *, const wchar_t * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +wchar_t * wcstok( wchar_t * _PDCLIB_restrict s1, + const wchar_t * _PDCLIB_restrict s2, + wchar_t ** _PDCLIB_restrict ptr ) +{ + const wchar_t * p = s2; + + if ( s1 != NULL ) + { + /* new string */ + *ptr = s1; + } + else + { + /* old string continued */ + if ( *ptr == NULL ) + { + /* No old string, no new string, nothing to do */ + return NULL; + } + s1 = *ptr; + } + + /* skipping leading s2 characters */ + while ( *p && *s1 ) + { + if ( *s1 == *p ) + { + /* found seperator; skip and start over */ + ++s1; + p = s2; + continue; + } + ++p; + } + + if ( ! *s1 ) + { + /* no more to parse */ + return ( *ptr = NULL ); + } + + /* skipping non-s2 characters */ + *ptr = s1; + while ( **ptr ) + { + p = s2; + while ( *p ) + { + if ( **ptr == *p++ ) + { + /* found seperator; overwrite with '\0', position *ptr, return */ + *(*ptr)++ = L'\0'; + return s1; + } + } + ++(*ptr); + } + + /* parsed to end of string */ + *ptr = NULL; + return s1; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + wchar_t s[] = L"_a_bc__d_"; + wchar_t* state = NULL; + wchar_t* tokres; + + TESTCASE( ( tokres = wcstok( s, L"_", &state ) ) == &s[1] ); + TESTCASE( s[1] == L'a' ); + TESTCASE( s[2] == L'\0' ); + TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == &s[3] ); + TESTCASE( s[3] == L'b' ); + TESTCASE( s[4] == L'c' ); + TESTCASE( s[5] == L'\0' ); + TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == &s[7] ); + TESTCASE( s[6] == L'_' ); + TESTCASE( s[7] == L'd' ); + TESTCASE( s[8] == L'\0' ); + TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == NULL ); + wcscpy( s, L"ab_cd" ); + TESTCASE( ( tokres = wcstok( s, L"_", &state ) ) == &s[0] ); + TESTCASE( s[0] == L'a' ); + TESTCASE( s[1] == L'b' ); + TESTCASE( s[2] == L'\0' ); + TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == &s[3] ); + TESTCASE( s[3] == L'c' ); + TESTCASE( s[4] == L'd' ); + TESTCASE( s[5] == L'\0' ); + TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == NULL ); + return TEST_RESULTS; +} +#endif