2 * AcessOS Basic C Library
12 * \fn EXPORT int strcmp(const char *s1, const char *s2)
13 * \brief Compare two strings
15 EXPORT int strcmp(const char *s1, const char *s2)
17 while(*s1 && *s1 == *s2) {
20 return (int)*s1 - (int)*s2;
24 * \fn EXPORT int strncmp(const char *s1, const char *s2)
25 * \brief Compare two strings
27 EXPORT int strncmp(const char *s1, const char *s2, size_t n)
29 while(n && *s1 && *s1 == *s2)
37 return (int)*s1 - (int)*s2;
40 EXPORT int strcasecmp(const char *s1, const char *s2)
43 while( (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0' ) {
49 EXPORT int strncasecmp(const char *s1, const char *s2, size_t n)
52 if( n == 0 ) return 0;
53 while(n -- && (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0') {
60 * \fn EXPORT char *strcpy(char *dst, const char *src)
61 * \brief Copy a string to another
63 EXPORT char *strcpy(char *dst, const char *src)
75 * \fn EXPORT char *strncpy(char *dst, const char *src)
76 * \brief Copy at most \a num characters from \a src to \a dst
79 EXPORT char *strncpy(char *dst, const char *src, size_t num)
82 while(*src && num--) *to++ = *src++;
88 * \fn EXPORT char *strcat(char *dst, const char *src)
89 * \brief Append a string onto another
91 EXPORT char *strcat(char *dst, const char *src)
97 while(*src) *to++ = *src++;
103 EXPORT char *strncat(char *dst, const char *src, size_t n)
109 while(*src && n--) *to++ = *src++;
116 * \brief Get the length of a string
118 EXPORT size_t strlen(const char *str)
121 for(retval = 0; *str != '\0'; str++, retval++);
126 * \brief Get the length of a string, with a maximum of \a maxlen
128 * Gets the length of a string (excluding the terminating \0 byte)
130 EXPORT size_t strnlen(const char *str, size_t maxlen)
133 for( len = 0; maxlen -- && *str; str ++, len ++ );
138 * \fn EXPORT char *strdup(const char *str)
139 * \brief Duplicate a string using heap memory
140 * \note Defined in POSIX Spec, not C spec
142 EXPORT char *strdup(const char *str)
144 size_t len = strlen(str);
145 char *ret = malloc(len+1);
146 if(ret == NULL) return NULL;
152 * \fn EXPORT char *strndup(const char *str, size_t maxlen)
153 * \brief Duplicate a string into the heap with a maximum length
154 * \param str Input string buffer
155 * \param maxlen Maximum valid size of the \a str buffer
156 * \return Heap string with the same value of \a str
158 EXPORT char *strndup(const char *str, size_t maxlen)
162 for( len = 0; len < maxlen && str[len]; len ++) ;
163 ret = malloc( len + 1);
164 memcpy( ret, str, len );
170 * \fn EXPORT char *strchr(char *str, int character)
171 * \brief Locate a character in a string
173 EXPORT char *strchr(const char *str, int character)
177 if(*str == character)
184 * \fn EXPORT char *strrchr(char *str, int character)
185 * \brief Locate the last occurance of a character in a string
187 EXPORT char *strrchr(const char *str, int character)
193 if(str[i] == character)
194 return (void*)&str[i];
200 * \fn EXPORT char *strstr(char *str1, const char *str2)
201 * \brief Search a \a str1 for the first occurance of \a str2
203 EXPORT char *strstr(const char *str1, const char *str2)
205 const char *test = str2;
209 if(*test == '\0') return (char*)str1;
210 if(*str1 == *test) test++;
218 * \fn EXPORT void *memset(void *dest, int val, size_t num)
219 * \brief Clear memory with the specified value
221 EXPORT void *memset(void *dest, int val, size_t num)
223 unsigned char *p = dest;
224 while(num--) *p++ = val;
229 * \fn EXPORT void *memcpy(void *dest, const void *src, size_t count)
230 * \brief Copy one memory area to another
232 EXPORT void *memcpy(void *__dest, const void *__src, size_t count)
234 const int wordmask = sizeof(void*)-1;
235 uintptr_t src = (uintptr_t)__src;
236 uintptr_t dst = (uintptr_t)__dest;
238 if( count < sizeof(void*)*2 || (dst & wordmask) != (src & wordmask) )
241 const char *sp = __src;
242 while(count--) *dp++ = *sp ++;
244 // TODO: Bulk aligned copies
246 else if(count > 128 && (dst & 15) == (src & 15) )
249 for( ; dst & 15; count -- )
250 *(char*)dst++ = *(char*)src++;
251 memcpy_16byte(dst, src, count / 16);
256 *(char*)dst++ = *(char*)src++;
262 for( ; count && (dst & wordmask) != 0; count -- )
263 *(char*)dst++ = *(char*)src++;
265 dp = (void*)dst; sp = (void*)src;
266 while( count >= sizeof(void*) )
269 count -= sizeof(void*);
271 dst = (uintptr_t)dp; src = (uintptr_t)sp;
272 for( ; count; count -- )
273 *(char*)dst++ = *(char*)src++;
280 * \fn EXPORT void *memmove(void *dest, const void *src, size_t count)
281 * \brief Copy data in memory, avoiding overlap problems
283 EXPORT void *memmove(void *dest, const void *src, size_t count)
285 const char *sp = (const char *)src;
286 char *dp = (char *)dest;
287 // Check if the areas overlap
289 memcpy(dest, src, count);
290 else if( dp >= sp+count )
291 memcpy(dest, src, count);
295 dp[count] = sp[count];
306 * \fn EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
307 * \brief Compare two regions of memory
308 * \param mem1 Region 1
309 * \param mem2 Region 2
310 * \param count Number of bytes to check
312 EXPORT int memcmp(const void *mem1, const void *mem2, size_t count)
314 const unsigned char *p1 = mem1, *p2 = mem2;
326 * \fn EXPORT void *memchr(void *ptr, int value, size_t num)
327 * \brief Locates the first occurence of \a value starting at \a ptr
328 * \param ptr Starting memory location
329 * \param value Value to find
330 * \param num Size of memory area to check
332 EXPORT void *memchr(const void *ptr, int value, size_t num)
336 if( *(const unsigned char*)ptr == (unsigned char)value )
343 EXPORT size_t strcspn(const char *haystack, const char *reject)
349 for( i = 0; reject[i] && reject[i] == *haystack; i ++ );
351 if( reject[i] ) return ret;
357 EXPORT size_t strspn(const char *haystack, const char *accept)
363 for( i = 0; accept[i] && accept[i] == *haystack; i ++ );
365 if( !accept[i] ) return ret;