Skip to content

Commit 3887c23

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wcscat implementation (llvm#142243)
Implemented wcscat and tests. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent a568617 commit 3887c23

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
@@ -374,6 +374,7 @@ set(TARGET_LIBC_ENTRYPOINTS
374374
libc.src.wchar.wmemcmp
375375
libc.src.wchar.wmempcpy
376376
libc.src.wchar.wmemcpy
377+
libc.src.wchar.wcscat
377378
libc.src.wchar.wcsstr
378379
libc.src.wchar.wcsncat
379380
libc.src.wchar.wcscpy

libc/include/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ functions:
102102
- type: __restrict wchar_t *
103103
- type: const __restrict wchar_t *
104104
- type: size_t
105+
- name: wcscat
106+
standards:
107+
- stdc
108+
return_type: wchar_t *
109+
arguments:
110+
- type: __restrict wchar_t *
111+
- type: const __restrict wchar_t *
105112
- name: wcsstr
106113
standards:
107114
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ add_entrypoint_object(
137137
libc.src.__support.wctype_utils
138138
)
139139

140+
add_entrypoint_object(
141+
wcscat
142+
SRCS
143+
wcscat.cpp
144+
HDRS
145+
wcscat.h
146+
DEPENDS
147+
libc.hdr.types.size_t
148+
libc.hdr.wchar_macros
149+
libc.src.string.string_utils
150+
)
151+
140152
add_entrypoint_object(
141153
wcsstr
142154
SRCS

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 i = 0;
23+
for (; s2[i] != L'\0'; i++)
24+
s1[size_1 + i] = s2[i];
25+
s1[size_1 + i] = L'\0';
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
@@ -135,6 +135,16 @@ add_libc_test(
135135
libc.src.wchar.wmemcpy
136136
)
137137

138+
add_libc_test(
139+
wcscat_test
140+
SUITE
141+
libc_wchar_unittests
142+
SRCS
143+
wcscat_test.cpp
144+
DEPENDS
145+
libc.src.wchar.wcscat
146+
)
147+
138148
add_libc_test(
139149
wcsstr_test
140150
SUITE

libc/test/src/wchar/wcscat_test.cpp

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)