Skip to content

[libc] wcscpy implementation #142228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcsspn
libc.src.wchar.wmemcmp
libc.src.wchar.wmemcpy
libc.src.wchar.wcscpy

# sys/uio.h entrypoints
libc.src.sys.uio.writev
Expand Down
7 changes: 7 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ functions:
- type: __restricted wchar_t *
- type: const __ restricted wchar_t *
- type: size_t
- name: wcscpy
standards:
- stdc
return_type: wchar_t *
arguments:
- type: __restrict wchar_t *
- type: const __restrict wchar_t *
12 changes: 12 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ add_entrypoint_object(
libc.hdr.wchar_macros
libc.src.__support.wctype_utils
)

add_entrypoint_object(
wcscpy
SRCS
wcscpy.cpp
HDRS
wcscpy.h
DEPENDS
libc.hdr.types.size_t
libc.hdr.wchar_macros
libc.src.__support.wctype_utils
)
27 changes: 27 additions & 0 deletions libc/src/wchar/wcscpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation of wcscpy ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/wchar/wcscpy.h"

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/string_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(wchar_t *, wcscpy,
(wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
size_t size = internal::string_length(s2) + 1;
inline_memcpy(s1, s2, size * sizeof(wchar_t));
return s1;
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/wchar/wcscpy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for wcscpy ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_WCHAR_WCSCPY_H
#define LLVM_LIBC_SRC_WCHAR_WCSCPY_H

#include "hdr/types/wchar_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

wchar_t *wcscpy(wchar_t *__restrict s1, const wchar_t *__restrict s2);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_WCSCPY_H
10 changes: 10 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ add_libc_test(
DEPENDS
libc.src.wchar.wmemcpy
)

add_libc_test(
wcscpy_test
SUITE
libc_wchar_unittests
SRCS
wcscpy_test.cpp
DEPENDS
libc.src.wchar.wcscpy
)
48 changes: 48 additions & 0 deletions libc/test/src/wchar/wcscpy_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//===-- Unittests for wcscpy ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/types/wchar_t.h"
#include "src/wchar/wcscpy.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWCSCpyTest, EmptySrc) {
// Empty src should lead to empty destination.
wchar_t dest[4] = {L'a', L'b', L'c', L'\0'};
const wchar_t *src = L"";
LIBC_NAMESPACE::wcscpy(dest, src);
ASSERT_TRUE(dest[0] == src[0]);
ASSERT_TRUE(dest[0] == L'\0');
}

TEST(LlvmLibcWCSCpyTest, EmptyDest) {
// Empty dest should result in src
const wchar_t *src = L"abc";
wchar_t dest[4];
LIBC_NAMESPACE::wcscpy(dest, src);
ASSERT_TRUE(dest[0] == L'a');
ASSERT_TRUE(dest[1] == L'b');
ASSERT_TRUE(dest[2] == L'c');
ASSERT_TRUE(dest[3] == L'\0');
}

TEST(LlvmLibcWCSCpyTest, OffsetDest) {
// Offsetting should result in a concatenation.
const wchar_t *src = L"abc";
wchar_t dest[7];
dest[0] = L'x';
dest[1] = L'y';
dest[2] = L'z';
LIBC_NAMESPACE::wcscpy(dest + 3, src);
ASSERT_TRUE(dest[0] == L'x');
ASSERT_TRUE(dest[1] == L'y');
ASSERT_TRUE(dest[2] == L'z');
ASSERT_TRUE(dest[3] == src[0]);
ASSERT_TRUE(dest[4] == src[1]);
ASSERT_TRUE(dest[5] == src[2]);
ASSERT_TRUE(dest[6] == src[3]);
}
Loading