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

Conversation

sribee8
Copy link
Contributor

@sribee8 sribee8 commented May 30, 2025

Implemented wcsncpy and tests for the function.

Implemented wcsncpy and tests for the function.
@llvmbot llvmbot added the libc label May 30, 2025
@llvmbot
Copy link
Member

llvmbot commented May 30, 2025

@llvm/pr-subscribers-libc

Author: None (sribee8)

Changes

Implemented wcsncpy and tests for the function.


Full diff: https://github.com/llvm/llvm-project/pull/142237.diff

7 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
  • (modified) libc/include/wchar.yaml (+9-1)
  • (modified) libc/src/wchar/CMakeLists.txt (+12)
  • (added) libc/src/wchar/wcsncpy.cpp (+33)
  • (added) libc/src/wchar/wcsncpy.h (+23)
  • (modified) libc/test/src/wchar/CMakeLists.txt (+10)
  • (added) libc/test/src/wchar/wcsncpy_test.cpp (+66)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7ddeb4d31b466..746028e0b6ff7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -370,6 +370,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wcsspn
     libc.src.wchar.wmemcmp
     libc.src.wchar.wmemcpy
+    libc.src.wchar.wcsncpy
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index bd9105f69222c..45070d202325a 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -70,5 +70,13 @@ functions:
     return_type: wchar_t *
     arguments: 
       - type: __restricted wchar_t *
-      - type: const __ restricted wchar_t *
+      - type: const __ restrict wchar_t *
+      - type: size_t
+  - name: wcsncpy
+    standards:
+      - stdc
+    return_type: wchar_t *
+    arguments: 
+      - type: __restrict wchar_t *
+      - type: const __restrict wchar_t *
       - type: size_t
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 9db121762348b..c7f105abee871 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -104,3 +104,15 @@ add_entrypoint_object(
     libc.hdr.wchar_macros
     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.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wcsncpy.cpp b/libc/src/wchar/wcsncpy.cpp
new file mode 100644
index 0000000000000..e7ae9a4a0da79
--- /dev/null
+++ b/libc/src/wchar/wcsncpy.cpp
@@ -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
diff --git a/libc/src/wchar/wcsncpy.h b/libc/src/wchar/wcsncpy.h
new file mode 100644
index 0000000000000..06c23f24bcde7
--- /dev/null
+++ b/libc/src/wchar/wcsncpy.h
@@ -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
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 9bc230e0bddf3..f2a9106a1704e 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -94,3 +94,13 @@ add_libc_test(
   DEPENDS
     libc.src.wchar.wmemcpy
 )
+
+add_libc_test(
+  wcsncpy_test
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wcsncpy_test.cpp
+  DEPENDS
+    libc.src.wchar.wcsncpy
+)
diff --git a/libc/test/src/wchar/wcsncpy_test.cpp b/libc/test/src/wchar/wcsncpy_test.cpp
new file mode 100644
index 0000000000000..9b5ffbe20b4a1
--- /dev/null
+++ b/libc/test/src/wchar/wcsncpy_test.cpp
@@ -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');
+}

@michaelrj-google michaelrj-google self-requested a review May 30, 2025 23:27
Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@michaelrj-google michaelrj-google merged commit 2ff2a07 into llvm:main Jun 3, 2025
16 checks passed
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
Implemented wcsncpy and tests for the function.

---------

Co-authored-by: Sriya Pratipati <[email protected]>
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
Implemented wcsncpy and tests for the function.

---------

Co-authored-by: Sriya Pratipati <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants