Skip to content

Commit 0fdb865

Browse files
author
Sriya Pratipati
committed
[libc] wcscat implementation
Implemented wcscat and tests.
1 parent 339851e commit 0fdb865

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.wcscat
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: wcscat
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,15 @@ add_entrypoint_object(
104104
libc.hdr.wchar_macros
105105
libc.src.__support.wctype_utils
106106
)
107+
108+
add_entrypoint_object(
109+
wcscat
110+
SRCS
111+
wcscat.cpp
112+
HDRS
113+
wcscat.h
114+
DEPENDS
115+
libc.hdr.types.size_t
116+
libc.hdr.wchar_macros
117+
libc.src.string.string_utils
118+
)

libc/src/wchar/wcscat.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Implementation of wcscat ------------------------------------------===//
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/wcscat.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 *, wcscat,
20+
(wchar_t *__restrict s1, const wchar_t *__restrict s2)) {
21+
size_t size_1 = internal::string_length(s1);
22+
size_t size_2 = internal::string_length(s2);
23+
for (size_t i = 0; i <= size_2; i++) {
24+
s1[size_1 + i] = s2[i];
25+
}
26+
return s1;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcscat.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcscat ----------------------------------===//
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_WCSCAT_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSCAT_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 *wcscat(wchar_t *__restrict s1, const wchar_t *__restrict s2);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCSCAT_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+
wcscat_test
100+
SUITE
101+
libc_wchar_unittests
102+
SRCS
103+
wcscat_test.cpp
104+
DEPENDS
105+
libc.src.wchar.wcscat
106+
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- Unittests for wcscat ---------------------------------------------===//
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/wcscat.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcWCSCatTest, EmptyDest) {
14+
// Dest should be fully replaced with src.
15+
wchar_t dest[4] = {L'\0'};
16+
const wchar_t *src = L"abc";
17+
LIBC_NAMESPACE::wcscat(dest, src);
18+
ASSERT_TRUE(dest[0] == L'a');
19+
ASSERT_TRUE(dest[1] == L'b');
20+
ASSERT_TRUE(dest[2] == L'c');
21+
ASSERT_TRUE(dest[3] == L'\0');
22+
}
23+
24+
TEST(LlvmLibcWCSCatTest, NonEmptyDest) {
25+
// Src should be appended on to dest.
26+
wchar_t dest[7] = {L'x', L'y', L'z', L'\0'};
27+
const wchar_t *src = L"abc";
28+
LIBC_NAMESPACE::wcscat(dest, src);
29+
ASSERT_TRUE(dest[0] == L'x');
30+
ASSERT_TRUE(dest[1] == L'y');
31+
ASSERT_TRUE(dest[2] == L'z');
32+
ASSERT_TRUE(dest[3] == L'a');
33+
ASSERT_TRUE(dest[4] == L'b');
34+
ASSERT_TRUE(dest[5] == L'c');
35+
ASSERT_TRUE(dest[6] == L'\0');
36+
}
37+
38+
TEST(LlvmLibcWCSCatTest, EmptySrc) {
39+
// Dest should remain intact.
40+
wchar_t dest[4] = {L'x', L'y', L'z', L'\0'};
41+
const wchar_t *src = L"";
42+
LIBC_NAMESPACE::wcscat(dest, src);
43+
ASSERT_TRUE(dest[0] == L'x');
44+
ASSERT_TRUE(dest[1] == L'y');
45+
ASSERT_TRUE(dest[2] == L'z');
46+
ASSERT_TRUE(dest[3] == L'\0');
47+
}

0 commit comments

Comments
 (0)