Skip to content

Commit b215c8e

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wcpcpy implementation (#144802)
Implemented wcpcpy and tests. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 7181785 commit b215c8e

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ set(TARGET_LIBC_ENTRYPOINTS
384384
libc.src.wchar.wcsncat
385385
libc.src.wchar.wcscpy
386386
libc.src.wchar.wmemchr
387+
libc.src.wchar.wcpcpy
387388

388389
# sys/uio.h entrypoints
389390
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,10 @@ functions:
174174
arguments:
175175
- type: wchar_t *__restrict
176176
- type: const wchar_t *__restrict
177+
- name: wcpcpy
178+
standards:
179+
- stdc
180+
return_type: wchar_t *
181+
arguments:
182+
- type: wchar_t *__restrict
183+
- type: const wchar_t *__restrict

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ add_entrypoint_object(
7676
libc.hdr.types.wchar_t
7777
)
7878

79+
add_entrypoint_object(
80+
wcpcpy
81+
SRCS
82+
wcpcpy.cpp
83+
HDRS
84+
wcpcpy.h
85+
DEPENDS
86+
libc.hdr.types.size_t
87+
libc.hdr.wchar_macros
88+
libc.src.string.string_utils
89+
)
90+
7991
add_entrypoint_object(
8092
wcschr
8193
SRCS

libc/src/wchar/wcpcpy.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation of wcpcpy ------------------------------------------===//
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/wcpcpy.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(wchar_t *, wcpcpy,
20+
(wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
21+
size_t size = internal::string_length(s2);
22+
__builtin_memcpy(s1, s2, (size + 1) * sizeof(wchar_t));
23+
wchar_t *result = s1 + size;
24+
return result;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcpcpy.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcpcpy ----------------------------------===//
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+
#ifndef LLVM_LIBC_SRC_WCHAR_WCPCPY_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCPCPY_H
11+
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
wchar_t *wcpcpy(wchar_t *__restrict ws1, const wchar_t *__restrict ws2);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCPCPY_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,13 @@ add_libc_test(
232232
DEPENDS
233233
libc.src.wchar.wcscpy
234234
)
235+
236+
add_libc_test(
237+
wcpcpy_test
238+
SUITE
239+
libc_wchar_unittests
240+
SRCS
241+
wcpcpy_test.cpp
242+
DEPENDS
243+
libc.src.wchar.wcpcpy
244+
)

libc/test/src/wchar/wcpcpy_test.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- Unittests for wcpcpy ---------------------------------------------===//
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 "hdr/types/wchar_t.h"
10+
#include "src/wchar/wcpcpy.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcWCPCpyTest, EmptySrc) {
14+
// Empty src should lead to empty destination.
15+
wchar_t dest[4] = {L'a', L'b', L'c', L'\0'};
16+
const wchar_t *src = L"";
17+
LIBC_NAMESPACE::wcpcpy(dest, src);
18+
ASSERT_TRUE(dest[0] == src[0]);
19+
ASSERT_TRUE(dest[0] == L'\0');
20+
}
21+
22+
TEST(LlvmLibcWCPCpyTest, EmptyDest) {
23+
// Empty dest should result in src
24+
const wchar_t *src = L"abc";
25+
wchar_t dest[4];
26+
wchar_t *result = LIBC_NAMESPACE::wcpcpy(dest, src);
27+
ASSERT_EQ(dest + 3, result);
28+
ASSERT_TRUE(result[0] == L'\0');
29+
ASSERT_TRUE(dest[0] == L'a');
30+
ASSERT_TRUE(dest[1] == L'b');
31+
ASSERT_TRUE(dest[2] == L'c');
32+
}
33+
34+
TEST(LlvmLibcWCPCpyTest, OffsetDest) {
35+
// Offsetting should result in a concatenation.
36+
const wchar_t *src = L"abc";
37+
wchar_t dest[7];
38+
dest[0] = L'x';
39+
dest[1] = L'y';
40+
dest[2] = L'z';
41+
wchar_t *result = LIBC_NAMESPACE::wcpcpy(dest + 3, src);
42+
ASSERT_TRUE(dest[0] == L'x');
43+
ASSERT_TRUE(dest[1] == L'y');
44+
ASSERT_TRUE(dest[2] == L'z');
45+
ASSERT_TRUE(dest[3] == src[0]);
46+
ASSERT_TRUE(dest[4] == src[1]);
47+
ASSERT_TRUE(dest[5] == src[2]);
48+
ASSERT_TRUE(result[0] == L'\0');
49+
ASSERT_EQ(dest + 6, result);
50+
}

0 commit comments

Comments
 (0)