Skip to content

Commit 2ff2a07

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wcsncpy implementation (#142237)
Implemented wcsncpy and tests for the function. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 12f8bf3 commit 2ff2a07

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-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.wcsncpy
377378
libc.src.wchar.wcscat
378379
libc.src.wchar.wcsstr
379380
libc.src.wchar.wcsncat

libc/include/wchar.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ functions:
9595
- type: const wchar_t *
9696
- type: size_t
9797
- name: wmemcpy
98+
standards:
99+
- stdc
100+
return_type: wchar_t *
101+
arguments:
102+
- type: __restrict wchar_t *
103+
- type: const __restrict wchar_t *
104+
- type: size_t
105+
- name: wcsncpy
98106
standards:
99107
- stdc
100108
return_type: wchar_t *

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+
wcsncpy
142+
SRCS
143+
wcsncpy.cpp
144+
HDRS
145+
wcsncpy.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
wcscat
142154
SRCS

libc/src/wchar/wcsncpy.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Implementation of wcsncpy -----------------------------------------===//
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/wcsncpy.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 *, wcsncpy,
21+
(wchar_t *__restrict s1, const wchar_t *__restrict s2,
22+
size_t n)) {
23+
size_t i = 0;
24+
// Copy up until \0 is found.
25+
for (; i < n && s2[i] != L'\0'; ++i)
26+
s1[i] = s2[i];
27+
// When s2 is shorter than n, append \0.
28+
for (; i < n; ++i)
29+
s1[i] = L'\0';
30+
return s1;
31+
}
32+
33+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsncpy.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation header for wcsncpy ---------------------------------===//
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_WCSNCPY_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSNCPY_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "hdr/types/wchar_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
wchar_t *wcsncpy(wchar_t *__restrict s1, const wchar_t *__restrict s2,
19+
size_t n);
20+
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SRC_WCHAR_WCSNCPY_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+
wcsncpy_test
140+
SUITE
141+
libc_wchar_unittests
142+
SRCS
143+
wcsncpy_test.cpp
144+
DEPENDS
145+
libc.src.wchar.wcsncpy
146+
)
147+
138148
add_libc_test(
139149
wcscat_test
140150
SUITE

libc/test/src/wchar/wcsncpy_test.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===-- Unittests for wcsncpy ---------------------------------------------===//
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/wcsncpy.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcWCSNCpyTest, CopyZero) {
14+
// Dest should remain unchanged.
15+
wchar_t dest[3] = {L'a', L'b', L'\0'};
16+
const wchar_t *src = L"x";
17+
LIBC_NAMESPACE::wcsncpy(dest, src, 0);
18+
ASSERT_TRUE(dest[0] == L'a');
19+
ASSERT_TRUE(dest[1] == L'b');
20+
ASSERT_TRUE(dest[2] == L'\0');
21+
}
22+
23+
TEST(LlvmLibcWCSNCpyTest, CopyFullIntoEmpty) {
24+
// Dest should be the exact same as src.
25+
wchar_t dest[15];
26+
const wchar_t *src = L"aaaaabbbbccccc";
27+
LIBC_NAMESPACE::wcsncpy(dest, src, 15);
28+
for (int i = 0; i < 15; i++)
29+
ASSERT_TRUE(dest[i] == src[i]);
30+
}
31+
32+
TEST(LlvmLibcWCSNCpyTest, CopyPartial) {
33+
// First two characters of dest should be the first two characters of src.
34+
wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'};
35+
const wchar_t *src = L"1234";
36+
LIBC_NAMESPACE::wcsncpy(dest, src, 2);
37+
ASSERT_TRUE(dest[0] == L'1');
38+
ASSERT_TRUE(dest[1] == L'2');
39+
ASSERT_TRUE(dest[2] == L'c');
40+
ASSERT_TRUE(dest[3] == L'd');
41+
ASSERT_TRUE(dest[4] == L'\0');
42+
}
43+
44+
TEST(LlvmLibcWCSNCpyTest, CopyNullTerminator) {
45+
// Null terminator should copy into dest.
46+
wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'};
47+
const wchar_t src[] = {L'\0', L'y'};
48+
LIBC_NAMESPACE::wcsncpy(dest, src, 1);
49+
ASSERT_TRUE(dest[0] == L'\0');
50+
ASSERT_TRUE(dest[1] == L'b');
51+
ASSERT_TRUE(dest[2] == L'c');
52+
ASSERT_TRUE(dest[3] == L'd');
53+
ASSERT_TRUE(dest[4] == L'\0');
54+
}
55+
56+
TEST(LlvmLibcWCSNCpyTest, CopyPastSrc) {
57+
// Copying past src should fill with null terminator.
58+
wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'};
59+
const wchar_t src[] = {L'x', L'\0'};
60+
LIBC_NAMESPACE::wcsncpy(dest, src, 4);
61+
ASSERT_TRUE(dest[0] == L'x');
62+
ASSERT_TRUE(dest[1] == L'\0');
63+
ASSERT_TRUE(dest[2] == L'\0');
64+
ASSERT_TRUE(dest[3] == L'\0');
65+
ASSERT_TRUE(dest[4] == L'\0');
66+
}

0 commit comments

Comments
 (0)