Skip to content

Commit e383753

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wcscpy implementation (#142228)
Implemented wcscpy as well as tests for the function. --------- Co-authored-by: Sriya Pratipati <sriyap@google.com>
1 parent 7365f02 commit e383753

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ set(TARGET_LIBC_ENTRYPOINTS
370370
libc.src.wchar.wcsspn
371371
libc.src.wchar.wmemcmp
372372
libc.src.wchar.wmemcpy
373+
libc.src.wchar.wcscpy
373374

374375
# sys/uio.h entrypoints
375376
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ functions:
7272
- type: __restrict wchar_t *
7373
- type: const __restrict wchar_t *
7474
- type: size_t
75+
- name: wcscpy
76+
standards:
77+
- stdc
78+
return_type: wchar_t *
79+
arguments:
80+
- type: __restrict wchar_t *
81+
- type: const __restrict wchar_t *

libc/src/wchar/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,16 @@ add_entrypoint_object(
104104
libc.hdr.wchar_macros
105105
libc.src.__support.wctype_utils
106106
)
107+
108+
add_entrypoint_object(
109+
wcscpy
110+
SRCS
111+
wcscpy.cpp
112+
HDRS
113+
wcscpy.h
114+
DEPENDS
115+
libc.hdr.types.size_t
116+
libc.hdr.wchar_macros
117+
libc.src.string.memory_utils.inline_memcpy
118+
libc.src.string.string_utils
119+
)

libc/src/wchar/wcscpy.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation of wcscpy ------------------------------------------===//
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/wcscpy.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/memory_utils/inline_memcpy.h"
16+
#include "src/string/string_utils.h"
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
20+
LLVM_LIBC_FUNCTION(wchar_t *, wcscpy,
21+
(wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
22+
size_t size = internal::string_length(s2) + 1;
23+
inline_memcpy(s1, s2, size * sizeof(wchar_t));
24+
return s1;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcscpy.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcscpy ----------------------------------===//
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_WCSCPY_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSCPY_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 *wcscpy(wchar_t *__restrict s1, const wchar_t *__restrict s2);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCSCPY_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,13 @@ add_libc_test(
9494
DEPENDS
9595
libc.src.wchar.wmemcpy
9696
)
97+
98+
add_libc_test(
99+
wcscpy_test
100+
SUITE
101+
libc_wchar_unittests
102+
SRCS
103+
wcscpy_test.cpp
104+
DEPENDS
105+
libc.src.wchar.wcscpy
106+
)

libc/test/src/wchar/wcscpy_test.cpp

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

0 commit comments

Comments
 (0)