]> pd.if.org Git - pdclib/blob - functions/string/memchr.c
Added test driver.
[pdclib] / functions / string / memchr.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* memchr( const void *, int, size_t )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <string.h>
12
13 void * memchr( const void * s, int c, size_t n )
14 {
15     const unsigned char * p = (const unsigned char *) s;
16     while ( n-- )
17     {
18         if ( *p == (unsigned char) c )
19         {
20             return (void *) p;
21         }
22         ++p;
23     }
24     return NULL;
25 }
26
27 #ifdef TEST
28 #include <_PDCLIB_test.h>
29
30 int main()
31 {
32     BEGIN_TESTS;
33     TESTCASE( memchr( abcde, 'c', 5 ) == &abcde[2] );
34     TESTCASE( memchr( abcde, 'a', 1 ) == &abcde[0] );
35     TESTCASE( memchr( abcde, 'a', 0 ) == NULL );
36     TESTCASE( memchr( abcde, '\0', 5 ) == NULL );
37     TESTCASE( memchr( abcde, '\0', 6 ) == &abcde[5] );
38     return TEST_RESULTS;
39 }
40 #endif