mirror of
https://github.com/AsahiLinux/m1n1
synced 2024-11-12 18:37:07 +00:00
string: add memmove, strrchr, strncmp
Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
parent
b82c0072e7
commit
46a46da111
2 changed files with 56 additions and 0 deletions
53
src/string.c
53
src/string.c
|
@ -16,6 +16,27 @@ void *memcpy(void *s1, const void *s2, size_t n)
|
|||
return s1;
|
||||
}
|
||||
|
||||
void *memmove(void *s1, const void *s2, size_t n)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
const unsigned char *p1 = (const unsigned char *)s1;
|
||||
|
@ -100,6 +121,21 @@ int strcmp(const char *s1, const char *s2)
|
|||
return (*(unsigned char *)s1 - *(unsigned char *)s2);
|
||||
}
|
||||
|
||||
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 (*(unsigned char *)s1 - *(unsigned char *)s2);
|
||||
}
|
||||
}
|
||||
|
||||
size_t strlen(const char *s)
|
||||
{
|
||||
size_t rc = 0;
|
||||
|
@ -121,3 +157,20 @@ char *strchr(const char *s, int c)
|
|||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *strrchr(const char *s, int c)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
while (s[i++]) {
|
||||
/* EMPTY */
|
||||
}
|
||||
|
||||
do {
|
||||
if (s[--i] == (char)c) {
|
||||
return (char *)s + i;
|
||||
}
|
||||
} while (i);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -6,13 +6,16 @@
|
|||
#include <stddef.h>
|
||||
|
||||
void *memcpy(void *s1, const void *s2, size_t n);
|
||||
void *memmove(void *s1, const void *s2, size_t n);
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
void *memset(void *s, int c, size_t n);
|
||||
void *memchr(const void *s, int c, size_t n);
|
||||
char *strcpy(char *s1, const char *s2);
|
||||
char *strncpy(char *s1, const char *s2, size_t n);
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
int strncmp(const char *s1, const char *s2, size_t n);
|
||||
size_t strlen(const char *s);
|
||||
char *strchr(const char *s, int c);
|
||||
char *strrchr(const char *s, int c);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue