Permalink
Browse files

Implemented more string functions.

  • Loading branch information...
hatlesshacker committed Jun 16, 2016
1 parent 3fe91ec commit 34b3d9b2fa79898ca47d6e9fe79a04f861951d06
Showing with 66 additions and 6 deletions.
  1. 0 autogen.sh
  2. +8 −1 src/string/memcpy.c
  3. +20 −1 src/string/memmove.c
  4. +8 −1 src/string/strcat.c
  5. +4 −1 src/string/strcpy.c
  6. +15 −1 src/string/strncmp.c
  7. +11 −1 src/string/strncpy.c
View
0 autogen.sh 100755 → 100644
No changes.
View
@@ -9,12 +9,19 @@
/**
* @date 2013-05-24
* @author Arto Bendiken
+ * @auther Syed Nasim
* @see http://libc11.org/string/memcpy.html
*/
void*
memcpy(void* restrict s1,
const void* restrict s2,
size_t n) {
- return (void)s1, (void)s2, (void)n, NULL; // TODO
+ char * dest = (char *) s1;
+ const char * src = (const char *) s2;
+ while ( n-- )
+ {
+ *dest++ = *src++;
+ }
+ return s1;
}
View
@@ -9,12 +9,31 @@
/**
* @date 2013-05-24
* @author Arto Bendiken
+ * @auther Syed Nasim
* @see http://libc11.org/string/memmove.html
*/
void*
memmove(void* s1,
const void* s2,
size_t n) {
- return (void)s1, (void)s2, (void)n, NULL; // TODO
+ char * dest = (char *) s1;
+ const char * src = (const char *) s2;
+ if ( dest <= src )
+ {
+ while ( n-- )
+ {
+ *dest++ = *src++;
+ }
+ }
+ else
+ {
+ src += n;
+ dest += n;
+ while ( n-- )
+ {
+ *--dest = *--src;
+ }
+ }
+ return s1;
}
View
@@ -9,11 +9,18 @@
/**
* @date 2014-11-23
* @author Arto Bendiken
+ * @auther Syed Nasim
* @see http://libc11.org/string/strcat.html
*/
char*
strcat(char* restrict s1,
const char* restrict s2) {
- return (void)s1, (void)s2, NULL; // TODO
+ char * rc = s1;
+ if ( *s1 )
+ {
+ while ( *++s1 );
+ }
+ while ( (*s1++ = *s2++) );
+ return rc;
}
View
@@ -9,11 +9,14 @@
/**
* @date 2014-11-23
* @author Arto Bendiken
+ * @auther Syed Nasim
* @see http://libc11.org/string/strcpy.html
*/
char*
strcpy(char* restrict s1,
const char* restrict s2) {
- return (void)s1, (void)s2, NULL; // TODO
+ char * rc = s1;
+ while (( *s1++ = *s2++ ));
+ return rc;
}
View
@@ -9,12 +9,26 @@
/**
* @date 2013-05-24
* @author Arto Bendiken
+ * @auther Syed Nasim
* @see http://libc11.org/string/strncmp.html
*/
int
strncmp(const char* const s1,
const char* const s2,
const size_t n) {
- return (void)s1, (void)s2, (void)n, -1; // TODO
+ while ( *s1 && n && ( *s1 == *s2 ) )
+ {
+ ++s1;
+ ++s2;
+ --n;
+ }
+ if ( ( n == 0 ) )
+ {
+ return 0;
+ }
+ else
+ {
+ return ( *(uint8_t *)s1 - *(uint8_t *)s2 );
+ }
}
View
@@ -9,12 +9,22 @@
/**
* @date 2014-11-23
* @author Arto Bendiken
+ * @auther Syed Nasim
* @see http://libc11.org/string/strncpy.html
*/
char*
strncpy(char* restrict s1,
const char* restrict s2,
size_t n) {
- return (void)s1, (void)s2, (void)n, NULL; // TODO
+ char * rc = s1;
+ while ( ( n > 0 ) && ( *s1++ = *s2++ ) )
+ {
+ --n;
+ }
+ while ( n-- > 1 )
+ {
+ *s1++ = '\0';
+ }
+ return rc;
}

0 comments on commit 34b3d9b

Please sign in to comment.