| 1 | //===-- Implementation of wcslcat -----------------------------------------===// |
| 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/wcslcat.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/string/string_utils.h" |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | LLVM_LIBC_FUNCTION(size_t, wcslcat, |
| 20 | (wchar_t *__restrict dst, const wchar_t *__restrict src, |
| 21 | size_t dstsize)) { |
| 22 | const size_t dstlen = internal::string_length(dst); |
| 23 | const size_t srclen = internal::string_length(src); |
| 24 | int limit = static_cast<int>(dstsize - dstlen - 1); |
| 25 | size_t returnval = (dstsize < dstlen ? dstsize : dstlen) + srclen; |
| 26 | if (limit < 0) |
| 27 | return returnval; |
| 28 | int i = 0; |
| 29 | for (; i < limit && src[i] != L'\0'; ++i) { |
| 30 | dst[dstlen + i] = src[i]; |
| 31 | } |
| 32 | |
| 33 | // appending null terminator if there is room |
| 34 | if (dstlen + i < dstlen + dstsize) |
| 35 | dst[dstlen + i] = L'\0'; |
| 36 | return returnval; |
| 37 | } |
| 38 | |
| 39 | } // namespace LIBC_NAMESPACE_DECL |
| 40 | |