Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Performance-wise, I'd benchmark this vs if(memchr(src,'\0',n)==src+n) memcpy(dst, src, n); because it isn't obvious at least to me if that's faster or slower than your custom function. Regarding...
Answer
#1: Initial revision
- Performance-wise, I'd benchmark this vs `if(memchr(src,'\0',n)==src+n) memcpy(dst, src, n);` because it isn't obvious at least to me if that's faster or slower than your custom function. - Regarding where `end` is pointing, I think that's the right call since it's convenient to have a pointer to the null terminator for many reasons, such as when determining the string length or indeed some manner of "chaining". - Bug: no const correctness. Okay so it isn't an actual bug, but it's something with overwhelming consensus of being good practice. In this case it means that you should do `const char* src`. - Minor: No restrict correctness. As written,`dst`/`end` and `src` may _not_ overlap (and you need to document this). Therefore, `src` can be `const char* restrict`. You can't restrict `dst` and `end` though since if used correctly, they point at the same buffer. - Aligned word copy improvements are possible. If you truly wish to write a library-quality function, it would do the copying on word-basis. This means handling misalignment at the beginning, then making an assumption of how far word copying can be done without reading out-of-bounds. Not exactly trivial - you can look at the Github project for glibc implementation of `strcpy` for inspiration. Note that glibc might rely on non-standard extensions however. - Style remark: `if (!*dst)` looks a bit weird and there's no obvious reason why you wouldn't write `if (*dst != '\0')`.