-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
*/ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define ASSERT(cnd) printf("ASSERT: "#cnd" == %s\n", ((cnd) ? "pass" : "FAIL")) | ||
|
||
int main() | ||
{ | ||
ASSERT(strcmp("hello", "world") < 0); | ||
ASSERT(strcmp("hello", "hello") == 0); | ||
ASSERT(strcmp("wello", "hello") > 0); | ||
ASSERT(strcmp("\xff", "\1") > 0); | ||
ASSERT(strcmp("\1", "\xff") < 0); | ||
ASSERT(strcmp("Hello", "hello") < 0); | ||
|
||
ASSERT(strncmp("hello world", "hello", 5) == 0); | ||
|
||
ASSERT(strcasecmp("hello", "world") < 0); | ||
ASSERT(strcasecmp("hello", "hello") == 0); | ||
ASSERT(strcasecmp("wello", "hello") > 0); | ||
ASSERT(strcasecmp("\xff", "\1") > 0); | ||
ASSERT(strcasecmp("\1", "\xff") < 0); | ||
ASSERT(strcasecmp("Hello", "hello") == 0); | ||
ASSERT(strcasecmp("Hello", "Hello") == 0); | ||
ASSERT(strcasecmp("hellO", "Hello") == 0); | ||
|
||
|
||
char buf[13]; | ||
memset(buf, 0, 13); | ||
ASSERT(buf[0] == 0); ASSERT(buf[12] == 0); | ||
|
||
ASSERT(memchr("\xffhello", 'x', 6) == NULL); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* AcessOS Basic C Library | ||
* string.c | ||
*/ | ||
#include <acess/sys.h> | ||
//#include <acess/sys.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <ctype.h> | ||
|
@@ -13,20 +13,19 @@ | |
* \fn EXPORT int strcmp(const char *s1, const char *s2) | ||
* \brief Compare two strings | ||
*/ | ||
EXPORT int strcmp(const char *s1, const char *s2) | ||
EXPORT int strcmp(const char *_s1, const char *_s2) | ||
{ | ||
while(*s1 && *s1 == *s2) { | ||
s1++; s2++; | ||
} | ||
return (int)*s1 - (int)*s2; | ||
return strncmp(_s1, _s2, SIZE_MAX); | ||
} | ||
|
||
/** | ||
* \fn EXPORT int strncmp(const char *s1, const char *s2) | ||
* \brief Compare two strings | ||
* \fn EXPORT int strncmp(const char *s1, const char *s2, size_t n) | ||
* \brief Compare two strings, stopping after n characters | ||
*/ | ||
EXPORT int strncmp(const char *s1, const char *s2, size_t n) | ||
EXPORT int strncmp(const char *_s1, const char *_s2, size_t n) | ||
{ | ||
const unsigned char* s1 = (const unsigned char*)_s1; | ||
const unsigned char* s2 = (const unsigned char*)_s2; | ||
while(n && *s1 && *s1 == *s2) | ||
{ | ||
s1++; s2++; | ||
|
@@ -38,23 +37,31 @@ EXPORT int strncmp(const char *s1, const char *s2, size_t n) | |
return (int)*s1 - (int)*s2; | ||
} | ||
|
||
EXPORT int strcasecmp(const char *s1, const char *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; | ||
return strncasecmp(_s1, _s2, SIZE_MAX); | ||
} | ||
|
||
EXPORT int strncasecmp(const char *s1, const char *s2, size_t n) | ||
EXPORT int strncasecmp(const char *_s1, const char *_s2, size_t n) | ||
{ | ||
int rv = 0; | ||
if( n == 0 ) return 0; | ||
while(n -- && (rv = toupper(*s1) - toupper(*s2)) == 0 && *s1 != '\0' && *s2 != '\0') { | ||
s1++; s2++; | ||
const unsigned char* s1 = (const unsigned char*)_s1; | ||
const unsigned char* s2 = (const unsigned char*)_s2; | ||
while( n-- && *s1 && *s2 ) | ||
{ | ||
if( *s1 != *s2 ) | ||
{ | ||
int rv; | ||
rv = toupper(*s1) - toupper(*s2); | ||
if(rv != 0) | ||
return rv; | ||
rv = tolower(*s1) - tolower(*s2); | ||
if(rv != 0) | ||
return rv; | ||
} | ||
s1 ++; | ||
s2 ++; | ||
} | ||
return rv; | ||
return 0; | ||
} | ||
|
||
/** | ||
|
@@ -131,7 +138,8 @@ EXPORT size_t strlen(const char *str) | |
EXPORT size_t strnlen(const char *str, size_t maxlen) | ||
{ | ||
size_t len; | ||
for( len = 0; maxlen -- && *str; str ++, len ++ ); | ||
for( len = 0; maxlen -- && *str; str ++, len ++ ) | ||
This comment has been minimized.
Sorry, something went wrong.
sortie
|
||
; | ||
return len; | ||
} | ||
|
||
|
@@ -171,11 +179,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(const char *str, int character) | ||
EXPORT char *strchr(const char *_str, int character) | ||
{ | ||
const unsigned char* str = (const unsigned char*)_str; | ||
for(;*str;str++) | ||
This comment has been minimized.
Sorry, something went wrong.
sortie
|
||
{ | ||
if(*str == character) | ||
if( *str == character ) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return (char*)str; | ||
} | ||
return NULL; | ||
|
@@ -185,11 +194,10 @@ EXPORT char *strchr(const 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(const char *str, int character) | ||
EXPORT char *strrchr(const char *_str, int character) | ||
{ | ||
int i; | ||
i = strlen(str)-1; | ||
while(i--) | ||
const unsigned char* str = (const unsigned char*)_str; | ||
for( int i = strlen(_str); i--; ) | ||
This comment has been minimized.
Sorry, something went wrong.
sortie
|
||
{ | ||
if(str[i] == character) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return (void*)&str[i]; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
@@ -277,6 +285,8 @@ EXPORT void *memcpy(void *__dest, const void *__src, size_t count) | |
return __dest; | ||
} | ||
|
||
// TODO: memccpy (POSIX defined) | ||
|
||
/** | ||
* \fn EXPORT void *memmove(void *dest, const void *src, size_t count) | ||
* \brief Copy data in memory, avoiding overlap problems | ||
|
@@ -316,7 +326,7 @@ EXPORT int memcmp(const void *mem1, const void *mem2, size_t count) | |
while(count--) | ||
{ | ||
if( *p1 != *p2 ) | ||
return *p1 - *p2; | ||
return (int)*p1 - (int)*p2; | ||
p1 ++; | ||
p2 ++; | ||
} | ||
|
@@ -332,24 +342,26 @@ EXPORT int memcmp(const void *mem1, const void *mem2, size_t count) | |
*/ | ||
EXPORT void *memchr(const void *ptr, int value, size_t num) | ||
{ | ||
const unsigned char* buf = ptr; | ||
while(num--) | ||
{ | ||
if( *(const unsigned char*)ptr == (unsigned char)value ) | ||
return (void*)ptr; | ||
ptr ++; | ||
if( *buf == (unsigned char)value ) | ||
return (void*)buf; | ||
buf ++; | ||
} | ||
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; | ||
for( int i = 0; reject[i]; i ++ ) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
if( reject[i] == *haystack ) | ||
return ret; | ||
} | ||
ret ++; | ||
This comment has been minimized.
Sorry, something went wrong.
sortie
|
||
} | ||
return ret; | ||
|
@@ -358,12 +370,13 @@ EXPORT size_t strcspn(const char *haystack, const char *reject) | |
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; | ||
for( int i = 0; accept[i]; i ++ ) | ||
{ | ||
if( accept[i] != *haystack ) | ||
This comment has been minimized.
Sorry, something went wrong.
sortie
|
||
return ret; | ||
} | ||
ret ++; | ||
This comment has been minimized.
Sorry, something went wrong.
sortie
|
||
} | ||
return ret; | ||
|
@@ -378,6 +391,7 @@ EXPORT char *strpbrk(const char *haystack, const char *accept) | |
if( accept[i] == *haystack ) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return (char*)haystack; | ||
} | ||
haystack ++; | ||
} | ||
return NULL; | ||
} | ||
|
Wrong return value. fputc should return the written byte on success or EOF on failure.