Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

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

75%
+4 −0
Code Reviews stpecpy(): Design a better string copy function that truncates

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...

posted 2y ago by Lundin‭

Answer
#1: Initial revision by user avatar Lundin‭ · 2022-02-13T16:26:41Z (over 2 years ago)
- 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')`.