]> pd.if.org Git - pdclib/blob - functions/wchar/wcstok.c
dos2unix
[pdclib] / functions / wchar / wcstok.c
1 /* wcstok( wchar_t *, const wchar_t * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <wchar.h>
8
9 #ifndef REGTEST
10
11 wchar_t * wcstok( wchar_t * _PDCLIB_restrict s1, 
12                   const wchar_t * _PDCLIB_restrict s2,
13                   wchar_t ** _PDCLIB_restrict ptr )
14 {
15     const wchar_t * p = s2;
16
17     if ( s1 != NULL )
18     {
19         /* new string */
20         *ptr = s1;
21     }
22     else
23     {
24         /* old string continued */
25         if ( *ptr == NULL )
26         {
27             /* No old string, no new string, nothing to do */
28             return NULL;
29         }
30         s1 = *ptr;
31     }
32
33     /* skipping leading s2 characters */
34     while ( *p && *s1 )
35     {
36         if ( *s1 == *p )
37         {
38             /* found seperator; skip and start over */
39             ++s1;
40             p = s2;
41             continue;
42         }
43         ++p;
44     }
45
46     if ( ! *s1 )
47     {
48         /* no more to parse */
49         return ( *ptr = NULL );
50     }
51
52     /* skipping non-s2 characters */
53     *ptr = s1;
54     while ( **ptr )
55     {
56         p = s2;
57         while ( *p )
58         {
59             if ( **ptr == *p++ )
60             {
61                 /* found seperator; overwrite with '\0', position *ptr, return */
62                 *(*ptr)++ = L'\0';
63                 return s1;
64             }
65         }
66         ++(*ptr);
67     }
68
69     /* parsed to end of string */
70     *ptr = NULL;
71     return s1;
72 }
73
74 #endif
75
76 #ifdef TEST
77 #include "_PDCLIB_test.h"
78
79 int main( void )
80 {
81     // MinGW at least has a very nonconforming (different signature!) variety
82     // of wcstok
83 #ifndef REGTEST
84     wchar_t s[] = L"_a_bc__d_";
85     wchar_t* state  = NULL;
86     wchar_t* tokres;
87
88     TESTCASE( ( tokres = wcstok( s, L"_", &state ) ) == &s[1] );
89     TESTCASE( s[1] == L'a' );
90     TESTCASE( s[2] == L'\0' );
91     TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == &s[3] );
92     TESTCASE( s[3] == L'b' );
93     TESTCASE( s[4] == L'c' );
94     TESTCASE( s[5] == L'\0' );
95     TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == &s[7] );
96     TESTCASE( s[6] == L'_' );
97     TESTCASE( s[7] == L'd' );
98     TESTCASE( s[8] == L'\0' );
99     TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == NULL );
100     wcscpy( s, L"ab_cd" );
101     TESTCASE( ( tokres = wcstok( s, L"_", &state ) ) == &s[0] );
102     TESTCASE( s[0] == L'a' );
103     TESTCASE( s[1] == L'b' );
104     TESTCASE( s[2] == L'\0' );
105     TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == &s[3] );
106     TESTCASE( s[3] == L'c' );
107     TESTCASE( s[4] == L'd' );
108     TESTCASE( s[5] == L'\0' );
109     TESTCASE( ( tokres = wcstok( NULL, L"_", &state ) ) == NULL );
110 #endif
111     return TEST_RESULTS;
112 }
113 #endif