X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstring%2Fmemmove.c;h=d291078feeed6dfa1dd6ee3a24dcd2ab6676a9c5;hb=cd6cfe0f578c4f744ddc9a342243aff6b42f8027;hp=ba73742ce0fda71718d004ee5d0228140b8a8be6;hpb=e5456e3c2697c4e17fc9aa3439f2e305517b4d96;p=pdclib.old diff --git a/functions/string/memmove.c b/functions/string/memmove.c index ba73742..d291078 100644 --- a/functions/string/memmove.c +++ b/functions/string/memmove.c @@ -1,38 +1,52 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- +/* $Id$ */ -void * memmove( void * s1, const void * s2, size_t n ) { /* TODO */ }; +/* memmove( void *, const void *, size_t ) -/* PDPC code - unreviewed + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +void * memmove( void * s1, const void * s2, size_t n ) { - char *p = s1; - const char *cs2 = s2; - size_t x; - - if (p <= cs2) + char * dest = (char *) s1; + const char * src = (const char *) s2; + if ( dest <= src ) { - for (x=0; x < n; x++) + while ( n-- ) { - *p = *cs2; - p++; - cs2++; + *dest++ = *src++; } } else { - if (n != 0) + src += n; + dest += n; + while ( n-- ) { - for (x=n-1; x > 0; x--) - { - *(p+x) = *(cs2+x); - } + *--dest = *--src; } - *(p+x) = *(cs2+x); } - return (s1); + return s1; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + char s[] = "xxxxabcde"; + 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; } -*/ \ No newline at end of file +#endif