| 1 | //===-- Implementation of wcpncpy -----------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/wchar/wcpncpy.h" |
| 10 | |
| 11 | #include "hdr/types/size_t.h" |
| 12 | #include "hdr/types/wchar_t.h" |
| 13 | #include "src/__support/common.h" |
| 14 | #include "src/__support/macros/config.h" |
| 15 | #include "src/__support/macros/null_check.h" |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | LLVM_LIBC_FUNCTION(wchar_t *, wcpncpy, |
| 20 | (wchar_t *__restrict s1, const wchar_t *__restrict s2, |
| 21 | size_t n)) { |
| 22 | if (n) { |
| 23 | LIBC_CRASH_ON_NULLPTR(s1); |
| 24 | LIBC_CRASH_ON_NULLPTR(s2); |
| 25 | } |
| 26 | size_t i; |
| 27 | // Copy up until \0 is found. |
| 28 | for (i = 0; i < n && s2[i] != '\0'; ++i) |
| 29 | s1[i] = s2[i]; |
| 30 | // When n>strlen(src), n-strlen(src) \0 are appended. |
| 31 | for (size_t j = i; j < n; ++j) |
| 32 | s1[j] = L'\0'; |
| 33 | // ...but our result points to the first \0 (if any). |
| 34 | return s1 + i; |
| 35 | } |
| 36 | |
| 37 | } // namespace LIBC_NAMESPACE_DECL |
| 38 | |