X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibc.so_src%2Fstring.c;h=0757c24ad3ee46432d3cc7f1232017dfe06cb968;hb=c9d40bdcf6d791c235b7d8e69646925d01e06506;hp=eaddd181d0b499d33f17617b167a620d0be1013f;hpb=2a05bcd81312a2885f824dac79e82c01a6e60c6c;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libc.so_src/string.c b/Usermode/Libraries/libc.so_src/string.c index eaddd181..0757c24a 100644 --- a/Usermode/Libraries/libc.so_src/string.c +++ b/Usermode/Libraries/libc.so_src/string.c @@ -5,7 +5,9 @@ #include #include #include +#include #include "lib.h" +#include /** * \fn EXPORT int strcmp(const char *s1, const char *s2) @@ -13,7 +15,7 @@ */ EXPORT int strcmp(const char *s1, const char *s2) { - while(*s1 == *s2 && *s1 != '\0' && *s2 != '\0') { + while(*s1 && *s1 == *s2) { s1++; s2++; } return (int)*s1 - (int)*s2; @@ -25,11 +27,34 @@ EXPORT int strcmp(const char *s1, const char *s2) */ EXPORT int strncmp(const char *s1, const char *s2, size_t n) { + while(n && *s1 && *s1 == *s2) + { + s1++; s2++; + n --; + } + if( n == 0 ) + return 0; + else + return (int)*s1 - (int)*s2; +} + +EXPORT int strcasecmp(const char *s1, const char *s2) +{ + int rv; + while( (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0' ) { + s1++; s2++; + } + return rv; +} + +EXPORT int strncasecmp(const char *s1, const char *s2, size_t n) +{ + int rv = 0; if( n == 0 ) return 0; - while(n -- && *s1 == *s2 && *s1 != '\0' && *s2 != '\0') { + while(n -- && (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0') { s1++; s2++; } - return (int)*s1 - (int)*s2; + return rv; } /** @@ -76,18 +101,40 @@ EXPORT char *strcat(char *dst, const char *src) return dst; } +EXPORT char *strncat(char *dst, const char *src, size_t n) +{ + char *to = dst; + // Find the end + while(*to) to++; + // Copy + while(*src && n--) *to++ = *src++; + // End string + *to = '\0'; + return dst; +} + /** - * \fn EXPORT int strlen(const char *str) * \brief Get the length of a string */ -EXPORT int strlen(const char *str) +EXPORT size_t strlen(const char *str) { - int retval; - for(retval = 0; *str != '\0'; str++) - retval++; + size_t retval; + for(retval = 0; *str != '\0'; str++, retval++); return retval; } +/** + * \brief Get the length of a string, with a maximum of \a maxlen + * + * Gets the length of a string (excluding the terminating \0 byte) + */ +EXPORT size_t strnlen(const char *str, size_t maxlen) +{ + size_t len; + for( len = 0; maxlen -- && *str; str ++, len ++ ); + return len; +} + /** * \fn EXPORT char *strdup(const char *str) * \brief Duplicate a string using heap memory @@ -124,11 +171,12 @@ EXPORT char *strndup(const char *str, size_t maxlen) * \fn EXPORT char *strchr(char *str, int character) * \brief Locate a character in a string */ -EXPORT char *strchr(char *str, int character) +EXPORT char *strchr(const char *str, int character) { - while(*str) + for(;*str;str++) { - if(*str == character) return str; + if(*str == character) + return (char*)str; } return NULL; } @@ -137,13 +185,14 @@ EXPORT char *strchr(char *str, int character) * \fn EXPORT char *strrchr(char *str, int character) * \brief Locate the last occurance of a character in a string */ -EXPORT char *strrchr(char *str, int character) +EXPORT char *strrchr(const char *str, int character) { int i; i = strlen(str)-1; while(i--) { - if(str[i] == character) return &str[i]; + if(str[i] == character) + return (void*)&str[i]; } return NULL; } @@ -152,16 +201,15 @@ EXPORT char *strrchr(char *str, int character) * \fn EXPORT char *strstr(char *str1, const char *str2) * \brief Search a \a str1 for the first occurance of \a str2 */ -EXPORT char *strstr(char *str1, const char *str2) +EXPORT char *strstr(const char *str1, const char *str2) { const char *test = str2; - while(*str1) + for(;*str1;str1++) { - if(*test == '\0') return str1; + if(*test == '\0') return (char*)str1; if(*str1 == *test) test++; else test = str2; - str1 ++; } return NULL; } @@ -182,12 +230,51 @@ EXPORT void *memset(void *dest, int val, size_t num) * \fn EXPORT void *memcpy(void *dest, const void *src, size_t count) * \brief Copy one memory area to another */ -EXPORT void *memcpy(void *dest, const void *src, size_t count) +EXPORT void *memcpy(void *__dest, const void *__src, size_t count) { - char *sp = (char *)src; - char *dp = (char *)dest; - for(;count--;) *dp++ = *sp++; - return dest; + const int wordmask = sizeof(void*)-1; + uintptr_t src = (uintptr_t)__src; + uintptr_t dst = (uintptr_t)__dest; + + if( count < sizeof(void*)*2 || (dst & wordmask) != (src & wordmask) ) + { + char *dp = __dest; + const char *sp = __src; + while(count--) *dp++ = *sp ++; + } + // TODO: Bulk aligned copies + #if 0 + else if(count > 128 && (dst & 15) == (src & 15) ) + { + // SSE/bulk copy + for( ; dst & 15; count -- ) + *(char*)dst++ = *(char*)src++; + memcpy_16byte(dst, src, count / 16); + dst += count & ~15; + src += count & ~15; + count &= 15; + while(count --) + *(char*)dst++ = *(char*)src++; + } + #endif + else + { + void **dp, **sp; + for( ; count && (dst & wordmask) != 0; count -- ) + *(char*)dst++ = *(char*)src++; + + dp = (void*)dst; sp = (void*)src; + while( count >= sizeof(void*) ) + { + *dp++ = *sp++; + count -= sizeof(void*); + } + dst = (uintptr_t)dp; src = (uintptr_t)sp; + for( ; count; count -- ) + *(char*)dst++ = *(char*)src++; + } + + return __dest; } /** @@ -196,14 +283,24 @@ EXPORT void *memcpy(void *dest, const void *src, size_t count) */ EXPORT void *memmove(void *dest, const void *src, size_t count) { - char *sp = (char *)src; - char *dp = (char *)dest; - // Check if corruption will happen - if( (unsigned int)dest > (unsigned int)src && (unsigned int)dest < (unsigned int)src+count ) - for(;count--;) dp[count] = sp[count]; - else - for(;count--;) *dp++ = *sp++; - return dest; + const char *sp = (const char *)src; + char *dp = (char *)dest; + // Check if the areas overlap + if( sp >= dp+count ) + memcpy(dest, src, count); + else if( dp >= sp+count ) + memcpy(dest, src, count); + else { + if( sp < dp ) { + while(count--) + dp[count] = sp[count]; + } + else { + while(count--) + *dp++ = *sp++; + } + } + return dest; } /** @@ -215,12 +312,13 @@ EXPORT void *memmove(void *dest, const void *src, size_t count) */ EXPORT int memcmp(const void *mem1, const void *mem2, size_t count) { + const unsigned char *p1 = mem1, *p2 = mem2; while(count--) { - if( *(unsigned char*)mem1 != *(unsigned char*)mem2 ) - return *(unsigned char*)mem1 - *(unsigned char*)mem2; - mem1 ++; - mem2 ++; + if( *p1 != *p2 ) + return *p1 - *p2; + p1 ++; + p2 ++; } return 0; } @@ -232,13 +330,86 @@ EXPORT int memcmp(const void *mem1, const void *mem2, size_t count) * \param value Value to find * \param num Size of memory area to check */ -EXPORT void *memchr(void *ptr, int value, size_t num) +EXPORT void *memchr(const void *ptr, int value, size_t num) { while(num--) { - if( *(unsigned char*)ptr == (unsigned char)value ) - return ptr; + if( *(const unsigned char*)ptr == (unsigned char)value ) + return (void*)ptr; ptr ++; } return NULL; } + +EXPORT size_t strcspn(const char *haystack, const char *reject) +{ + size_t ret = 0; + int i; + while( *haystack ) + { + for( i = 0; reject[i] && reject[i] == *haystack; i ++ ); + + if( reject[i] ) return ret; + ret ++; + } + return ret; +} + +EXPORT size_t strspn(const char *haystack, const char *accept) +{ + size_t ret = 0; + int i; + while( *haystack ) + { + for( i = 0; accept[i] && accept[i] == *haystack; i ++ ); + + if( !accept[i] ) return ret; + ret ++; + } + return ret; +} + +EXPORT char *strpbrk(const char *haystack, const char *accept) +{ + while( *haystack ) + { + for( int i = 0; accept[i]; i ++ ) + { + if( accept[i] == *haystack ) + return (char*)haystack; + } + } + return NULL; +} + +char *strtok(char *str, const char *delim) +{ + static char *__saveptr; + return strtok_r(str, delim, &__saveptr); +} +char *strtok_r(char *str, const char *delim, char **saveptr) +{ + char *pos = (str ? str : *saveptr); + + while( strchr(delim, *pos) ) + pos ++; + + if( *pos == '\0' ) + return NULL; + + char *ret = pos; + while( !strchr(delim, *pos) ) + pos ++; + + // Cap the returned string + // - If we're at the end of the original string, don't shift pos + if( *pos != '\0' ) { + *pos = '\0'; + pos ++; + } + + *saveptr = pos; + + return ret; +} +