3 /* 7.21 String handling <string.h>
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
9 #ifndef _PDCLIB_STRING_H
10 #define _PDCLIB_STRING_H _PDCLIB_STRING_H
11 #include <_PDCLIB_int.h>
12 _PDCLIB_BEGIN_EXTERN_C
14 #ifndef _PDCLIB_SIZE_T_DEFINED
15 #define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED
16 typedef _PDCLIB_size_t size_t;
19 #ifndef _PDCLIB_NULL_DEFINED
20 #define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED
21 #define NULL _PDCLIB_NULL
24 /* String function conventions */
27 In any of the following functions taking a size_t n to specify the length of
28 an array or size of a memory region, n may be 0, but the pointer arguments to
29 the call shall still be valid unless otherwise stated.
32 /* Copying functions */
34 /* Copy a number of n characters from the memory area pointed to by s2 to the
35 area pointed to by s1. If the two areas overlap, behaviour is undefined.
36 Returns the value of s1.
38 void * memcpy( void * _PDCLIB_restrict s1, const void * _PDCLIB_restrict s2, size_t n );
40 /* Copy a number of n characters from the memory area pointed to by s2 to the
41 area pointed to by s1. The two areas may overlap.
42 Returns the value of s1.
44 void * memmove( void * _PDCLIB_restrict s1, const void * _PDCLIB_restrict s2, size_t n );
46 /* Copy the character array s2 (including terminating '\0' byte) into the
48 Returns the value of s1.
50 char * strcpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 );
52 /* Copy a maximum of n characters from the character array s2 into the character
53 array s1. If s2 is shorter than n characters, '\0' bytes will be appended to
54 the copy in s1 until n characters have been written. If s2 is longer than n
55 characters, NO terminating '\0' will be written to s1. If the arrays overlap,
56 behaviour is undefined.
57 Returns the value of s1.
59 char * strncpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n );
61 /* Concatenation functions */
63 /* Append the contents of the character array s2 (including terminating '\0') to
64 the character array s1 (first character of s2 overwriting the '\0' of s1). If
65 the arrays overlap, behaviour is undefined.
66 Returns the value of s1.
68 char * strcat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 );
70 /* Append a maximum of n characters from the character array s1 to the character
71 array s1 (first character of s2 overwriting the '\0' of s1). A terminating
72 '\0' is ALWAYS appended, even if the full n characters have already been
73 written. If the arrays overlap, behaviour is undefined.
74 Returns the value of s1.
76 char * strncat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n );
78 /* Comparison functions */
80 /* Compare the first n characters of the memory areas pointed to by s1 and s2.
81 Returns 0 if s1 == s2, a negative number if s1 < s2, and a positive number if
84 int memcmp( const void * s1, const void * s2, size_t n );
86 /* Compare the character arrays s1 and s2.
87 Returns 0 if s1 == s2, a negative number if s1 < s2, and a positive number if
90 int strcmp( const char * s1, const char * s2 );
92 /* Compare the character arrays s1 and s2, interpreted as specified by the
93 LC_COLLATE category of the current locale.
94 Returns 0 if s1 == s2, a negative number if s1 < s2, and a positive number if
96 TODO: Currently a dummy wrapper for strcmp() as PDCLib does not yet support
99 int strcoll( const char * s1, const char * s2 );
101 /* Compare no more than the first n characters of the character arrays s1 and
103 Returns 0 if s1 == s2, a negative number if s1 < s2, and a positive number if
106 int strncmp( const char * s1, const char * s2, size_t n );
108 /* Transform the character array s2 as appropriate for the LC_COLLATE setting of
109 the current locale. If length of resulting string is less than n, store it in
110 the character array pointed to by s1. Return the length of the resulting
113 size_t strxfrm( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n );
115 /* Search functions */
117 /* Search the first n characters in the memory area pointed to by s for the
118 character c (interpreted as unsigned char).
119 Returns a pointer to the first instance found, or NULL.
121 void * memchr( const void * s, int c, size_t n );
123 /* Search the character array s (including terminating '\0') for the character c
124 (interpreted as char).
125 Returns a pointer to the first instance found, or NULL.
127 char * strchr( const char * s, int c );
129 /* Determine the length of the initial substring of character array s1 which
130 consists only of characters not from the character array s2.
131 Returns the length of that substring.
133 size_t strcspn( const char * s1, const char * s2 );
135 /* Search the character array s1 for any character from the character array s2.
136 Returns a pointer to the first occurrence, or NULL.
138 char * strpbrk( const char * s1, const char * s2 );
140 /* Search the character array s (including terminating '\0') for the character c
141 (interpreted as char).
142 Returns a pointer to the last instance found, or NULL.
144 char * strrchr( const char * s, int c );
146 /* Determine the length of the initial substring of character array s1 which
147 consists only of characters from the character array s2.
148 Returns the length of that substring.
150 size_t strspn( const char * s1, const char * s2 );
152 /* Search the character array s1 for the substring in character array s2.
153 Returns a pointer to that sbstring, or NULL. If s2 is of length zero,
156 char * strstr( const char * s1, const char * s2 );
158 /* In a series of subsequent calls, parse a C string into tokens.
159 On the first call to strtok(), the first argument is a pointer to the to-be-
160 parsed C string. On subsequent calls, the first argument is NULL unless you
161 want to start parsing a new string. s2 holds an array of seperator characters
162 which can differ from call to call. Leading seperators are skipped, the first
163 trailing seperator overwritten with '\0'.
164 Returns a pointer to the next token.
165 WARNING: This function uses static storage, and as such is not reentrant.
167 char * strtok( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 );
169 /* Miscellaneous functions */
171 /* Write the character c (interpreted as unsigned char) to the first n
172 characters of the memory area pointed to by s.
175 void * memset( void * s, int c, size_t n );
177 /* Map an error number to a (locale-specific) error message string. Error
178 numbers are typically errno values, but any number is mapped to a message.
179 TODO: PDCLib does not yet support locales.
181 char * strerror( int errnum );
183 /* Returns the length of the string s (excluding terminating '\0').
185 size_t strlen( const char * s );
187 #if _PDCLIB_POSIX_MIN(2008098L) || _PDCLIB_XOPEN_MIN(0)
188 char * strdup( const char* src );
189 char * strndup( const char* src, size_t n );