Ignazio Di Napoli wrote:
Thank you to all. I simply thought standard strcat managed the
overlaps. It's a little too tricky else... I had already solved
my problem (with MemMove, which I knew handling overlaps), but
I just was wondering how a so simple thing was not contempled...

It's for efficiency reasons.  Here is an implementation of strcat()
that doesn't handle overlapping strings:

        char *strcat (char *s1, const char *s2)
        {
            char *writepos;
            const char *readpos;

            writepos = s1;
            while (*writepos) { writepos++; }

            readpos = s2;
            while (*readpos) { *writepos++ = *readpos++; }
            *writepos = 0;

            return s1;
        }

Now, what if we wanted to make this handle overlapping strings?
At a very minimum, we would need to find the length of s2 before
we started writing into s1, because writing into s1 could overwrite
the null terminator we rely on to know the length.  And that would
require an extra loop, which slows things down.

Actually, it's not so terrible cause after the first loop, we could
check if s1 <= s2 && s2 <= writepos and then handle overlapping
strings as a special case, but it still makes the implementation
slower and more complicated.

  - Logan

--
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/

Reply via email to