[libc-commits] [PATCH] D91399: Add strncpy implementation.

Cheng Wang via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 00:45:56 PST 2020


cheng.w added a comment.

Thanks Siva.



================
Comment at: libc/src/string/strncpy.cpp:19
+                                    const char *__restrict src, size_t n) {
+  for (size_t i = 0, j = 0; i < n; ++i) {
+    if (*(src + j) != '\0') {
----------------
sivachandra wrote:
> WDYT about a `memcpy` followed by a `memset` (for the null characters)? Both these functions in libcs are //optimized// in some manner so we will be leveraging that. We will need an additional `strlen` operation though.
By referring to `strcpy`, I used `memcpy` and `strlen` at first. 
But it seems that this method avoids the overhead of:
- `strlen` goes through `src` for the length.
- function calls.
I am not sure whick method is more appropriate. So I upload it for suggestions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91399/new/

https://reviews.llvm.org/D91399



More information about the libc-commits mailing list