X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstring%2Fmemmove.c;h=7c50deeb6ead1e8d0c1cac5a3e2e3e793d7b6931;hb=2f57d79a5a24856fdea69b2119d67e5fb5029b3e;hp=f7ed6c1656b38d43e3f0a5afb8478a8cf895efe2;hpb=1d9d92ba957a0b8307c9a65c35867fde68e6533b;p=pdclib diff --git a/functions/string/memmove.c b/functions/string/memmove.c index f7ed6c1..7c50dee 100644 --- a/functions/string/memmove.c +++ b/functions/string/memmove.c @@ -1,31 +1,51 @@ -/* ---------------------------------------------------------------------------- - * $Id$ - * ---------------------------------------------------------------------------- - * Public Domain C Library - http://pdclib.sourceforge.net - * This code is Public Domain. Use, modify, and redistribute at will. - * --------------------------------------------------------------------------*/ +/* $Id$ */ -#include <__size_t.h> +/* Release $Name$ */ -void * memmove( void * dest, const void * src, size_t n ) +/* memmove( void *, const void *, size_t ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +void * memmove( void * s1, const void * s2, size_t n ) { - const char * src_p = (const char *) src; - char * dest_p = (char *) dest; - if ( dest_p < src_p ) + char * dest = (char *) s1; + const char * src = (const char *) s2; + if ( dest <= src ) { while ( n-- ) { - *dest_p++ = *src_p++; + *dest++ = *src++; } } else { - src_p += n; - dest_p += n; + src += n; + dest += n; while ( n-- ) { - *dest_p-- = *src_p--; + *--dest = *--src; } } - return dest; + return s1; +} + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main() +{ + char s[] = "xxxxabcde"; + BEGIN_TESTS; + TESTCASE( memmove( s, s + 4, 5 ) == s ); + TESTCASE( s[0] == 'a' ); + TESTCASE( s[4] == 'e' ); + TESTCASE( s[5] == 'b' ); + TESTCASE( memmove( s + 4, s, 5 ) == s + 4 ); + TESTCASE( s[4] == 'a' ); + return TEST_RESULTS; } +#endif