Skip to content

[libc] wcsncpy implementation #142237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wmemcmp
libc.src.wchar.wmempcpy
libc.src.wchar.wmemcpy
libc.src.wchar.wcsncpy
libc.src.wchar.wcscat
libc.src.wchar.wcsstr
libc.src.wchar.wcsncat
Expand Down
8 changes: 8 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ functions:
- type: const wchar_t *
- type: size_t
- name: wmemcpy
standards:
- stdc
return_type: wchar_t *
arguments:
- type: __restrict wchar_t *
- type: const __restrict wchar_t *
- type: size_t
- name: wcsncpy
standards:
- stdc
return_type: wchar_t *
Expand Down
12 changes: 12 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ add_entrypoint_object(
libc.src.__support.wctype_utils
)

add_entrypoint_object(
wcsncpy
SRCS
wcsncpy.cpp
HDRS
wcsncpy.h
DEPENDS
libc.hdr.types.size_t
libc.hdr.wchar_macros
libc.src.string.string_utils
)

add_entrypoint_object(
wcscat
SRCS
Expand Down
33 changes: 33 additions & 0 deletions libc/src/wchar/wcsncpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- Implementation of wcsncpy -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/wchar/wcsncpy.h"

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/string_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(wchar_t *, wcsncpy,
(wchar_t *__restrict s1, const wchar_t *__restrict s2,
size_t n)) {
size_t i = 0;
// Copy up until \0 is found.
for (; i < n && s2[i] != L'\0'; ++i)
s1[i] = s2[i];
// When s2 is shorter than n, append \0.
for (; i < n; ++i)
s1[i] = L'\0';
return s1;
}

} // namespace LIBC_NAMESPACE_DECL
23 changes: 23 additions & 0 deletions libc/src/wchar/wcsncpy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation header for wcsncpy ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_WCHAR_WCSNCPY_H
#define LLVM_LIBC_SRC_WCHAR_WCSNCPY_H

#include "hdr/types/size_t.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

wchar_t *wcsncpy(wchar_t *__restrict s1, const wchar_t *__restrict s2,
size_t n);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_WCHAR_WCSNCPY_H
10 changes: 10 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ add_libc_test(
libc.src.wchar.wmemcpy
)

add_libc_test(
wcsncpy_test
SUITE
libc_wchar_unittests
SRCS
wcsncpy_test.cpp
DEPENDS
libc.src.wchar.wcsncpy
)

add_libc_test(
wcscat_test
SUITE
Expand Down
66 changes: 66 additions & 0 deletions libc/test/src/wchar/wcsncpy_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===-- Unittests for wcsncpy ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/types/wchar_t.h"
#include "src/wchar/wcsncpy.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcWCSNCpyTest, CopyZero) {
// Dest should remain unchanged.
wchar_t dest[3] = {L'a', L'b', L'\0'};
const wchar_t *src = L"x";
LIBC_NAMESPACE::wcsncpy(dest, src, 0);
ASSERT_TRUE(dest[0] == L'a');
ASSERT_TRUE(dest[1] == L'b');
ASSERT_TRUE(dest[2] == L'\0');
}

TEST(LlvmLibcWCSNCpyTest, CopyFullIntoEmpty) {
// Dest should be the exact same as src.
wchar_t dest[15];
const wchar_t *src = L"aaaaabbbbccccc";
LIBC_NAMESPACE::wcsncpy(dest, src, 15);
for (int i = 0; i < 15; i++)
ASSERT_TRUE(dest[i] == src[i]);
}

TEST(LlvmLibcWCSNCpyTest, CopyPartial) {
// First two characters of dest should be the first two characters of src.
wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'};
const wchar_t *src = L"1234";
LIBC_NAMESPACE::wcsncpy(dest, src, 2);
ASSERT_TRUE(dest[0] == L'1');
ASSERT_TRUE(dest[1] == L'2');
ASSERT_TRUE(dest[2] == L'c');
ASSERT_TRUE(dest[3] == L'd');
ASSERT_TRUE(dest[4] == L'\0');
}

TEST(LlvmLibcWCSNCpyTest, CopyNullTerminator) {
// Null terminator should copy into dest.
wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'};
const wchar_t src[] = {L'\0', L'y'};
LIBC_NAMESPACE::wcsncpy(dest, src, 1);
ASSERT_TRUE(dest[0] == L'\0');
ASSERT_TRUE(dest[1] == L'b');
ASSERT_TRUE(dest[2] == L'c');
ASSERT_TRUE(dest[3] == L'd');
ASSERT_TRUE(dest[4] == L'\0');
}

TEST(LlvmLibcWCSNCpyTest, CopyPastSrc) {
// Copying past src should fill with null terminator.
wchar_t dest[] = {L'a', L'b', L'c', L'd', L'\0'};
const wchar_t src[] = {L'x', L'\0'};
LIBC_NAMESPACE::wcsncpy(dest, src, 4);
ASSERT_TRUE(dest[0] == L'x');
ASSERT_TRUE(dest[1] == L'\0');
ASSERT_TRUE(dest[2] == L'\0');
ASSERT_TRUE(dest[3] == L'\0');
ASSERT_TRUE(dest[4] == L'\0');
}
Loading