Main: libc/string.h: Add func "memcmp" (closes #37)

This commit is contained in:
Alex Kotov 2022-06-07 09:05:27 +03:00
parent 040197dacb
commit 982b36c6b7
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 14 additions and 3 deletions

View File

@ -113,9 +113,11 @@ AS_IF([test "$with_libc" = yes], [with_libc=yes], [with_libc
#############
AS_IF([test "$enable_tests" = yes -a "$host_cpu" != "$build_cpu" ], AC_MSG_ERROR([can not build cross-platform tests]))
AS_IF([test "$enable_tests" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests]))
AS_IF([test "$enable_tests_python" = yes -a "$host_cpu" != "$build_cpu" ], AC_MSG_ERROR([can not build cross-platform tests]))
AS_IF([test "$enable_tests" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests]))
AS_IF([test "$enable_tests_python" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests]))
AS_IF([test "$enable_tests" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with tests]))
AS_IF([test "$enable_tests_python" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with tests]))
AS_IF([test "$with_printf" = yes -a "$with_ntoa" = no], AC_MSG_ERROR([package `printf' requires package `ntoa']))
AS_IF([test "$with_printf" = yes -a "$with_printf_fmt" = no], AC_MSG_ERROR([package `printf' requires package `printf-fmt']))

View File

@ -7,6 +7,7 @@ extern "C" {
#include <stddef.h>
int memcmp(const void *s1, const void *s2, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);

View File

@ -29,10 +29,18 @@ int isspace(const int c)
return c == ' ' || (unsigned)c - '\t' < 5;
}
int memcmp(const void *s1, const void *s2, size_t n)
{
for (const unsigned char *p1 = s1, *p2 = s2; n--; ++p1, ++p2) {
if (*p1 != *p2) return *p1 - *p2;
}
return 0;
}
void *memcpy(void *dest, const void *src, size_t n)
{
char *dest_cell = dest;
char *src_cell = src;
char *src_cell = (char*)src;
while (n--) *dest_cell++ = *src_cell++;
return dest;
}
@ -40,7 +48,7 @@ void *memcpy(void *dest, const void *src, size_t n)
void *memmove(void *dest, const void *src, size_t n)
{
char *dest_cell = dest;
char *src_cell = src;
char *src_cell = (char*)src;
if (dest_cell <= src_cell) {
while (n--) *dest_cell++ = *src_cell++;
} else {