[libc-commits] [libc] [libc] wcsncat implementation (PR #142431)

via libc-commits libc-commits at lists.llvm.org
Mon Jun 2 13:07:59 PDT 2025


https://github.com/sribee8 updated https://github.com/llvm/llvm-project/pull/142431

>From 1aba677553e162637e9c9c1b717c30367d76d3e5 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Mon, 2 Jun 2025 16:49:04 +0000
Subject: [PATCH 1/3] [libc] wcsncat implementation

Implemented wcsncat and tests.
---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/include/wchar.yaml                  |  8 +++
 libc/src/wchar/CMakeLists.txt            | 12 ++++
 libc/src/wchar/wcsncat.cpp               | 32 +++++++++
 libc/src/wchar/wcsncat.h                 | 22 +++++++
 libc/test/src/wchar/CMakeLists.txt       | 10 +++
 libc/test/src/wchar/wcsncat_test.cpp     | 82 ++++++++++++++++++++++++
 7 files changed, 167 insertions(+)
 create mode 100644 libc/src/wchar/wcsncat.cpp
 create mode 100644 libc/src/wchar/wcsncat.h
 create mode 100644 libc/test/src/wchar/wcsncat_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 545b9227349fe..6c3c5f7fc0de0 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.wcsncat
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index bfd9a10342019..5528d6a65553c 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -72,3 +72,11 @@ functions:
       - type: __restrict wchar_t *
       - type: const __restrict wchar_t *
       - type: size_t
+  - name: wcsncat
+    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..dba5f8ee525f2 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(
+  wcsncat
+  SRCS
+    wcsncat.cpp
+  HDRS
+    wcsncat.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.wchar_macros
+    libc.src.string.string_utils
+)
diff --git a/libc/src/wchar/wcsncat.cpp b/libc/src/wchar/wcsncat.cpp
new file mode 100644
index 0000000000000..c8be2903a1158
--- /dev/null
+++ b/libc/src/wchar/wcsncat.cpp
@@ -0,0 +1,32 @@
+//===-- Implementation of wcsncat -----------------------------------------===//
+//
+// 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/wcsncat.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/string_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(wchar_t *, wcsncat,
+                   (wchar_t *__restrict s1, const wchar_t *__restrict s2,
+                    size_t n)) {
+  size_t size = internal::string_length(s1);
+  size_t i = 0;
+  for (; s2[i] && i < n; ++i) {
+    s1[size + i] = s2[i];
+  }
+  // Appending null character to the end of the result.
+  s1[size + i] = L'\0';
+  return s1;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcsncat.h b/libc/src/wchar/wcsncat.h
new file mode 100644
index 0000000000000..d7b03e9d43616
--- /dev/null
+++ b/libc/src/wchar/wcsncat.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wcsncat ---------------------------------===//
+//
+// 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_WCSNCAT_H
+#define LLVM_LIBC_SRC_WCHAR_WCSNCAT_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 *wcsncat(wchar_t *__restrict s1, const wchar_t *__restrict s2, size_t n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSNCAT_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 9bc230e0bddf3..8e5a389257842 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(
+  wcsncat_test
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wcsncat_test.cpp
+  DEPENDS
+    libc.src.wchar.wcsncat
+)
diff --git a/libc/test/src/wchar/wcsncat_test.cpp b/libc/test/src/wchar/wcsncat_test.cpp
new file mode 100644
index 0000000000000..47359f88cec9e
--- /dev/null
+++ b/libc/test/src/wchar/wcsncat_test.cpp
@@ -0,0 +1,82 @@
+//===-- Unittests for wcscat ---------------------------------------------===//
+//
+// 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/wcsncat.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWCSNCatTest, EmptyDest) {
+  wchar_t dest[4] = {L'\0'};
+  const wchar_t *src = L"abc";
+
+  // Start by copying nothing
+  LIBC_NAMESPACE::wcsncat(dest, src, 0);
+  ASSERT_TRUE(dest[0] == L'\0');
+
+  // Copying part of it.
+  LIBC_NAMESPACE::wcsncat(dest, src, 1);
+  ASSERT_TRUE(dest[0] == L'a');
+  ASSERT_TRUE(dest[1] == L'\0');
+
+  // Resetting for the last test.
+  dest[0] = '\0';
+
+  // Copying all of it.
+  LIBC_NAMESPACE::wcsncat(dest, src, 3);
+  ASSERT_TRUE(dest[0] == L'a');
+  ASSERT_TRUE(dest[1] == L'b');
+  ASSERT_TRUE(dest[2] == L'c');
+  ASSERT_TRUE(dest[3] == L'\0');
+}
+
+TEST(LlvmLibcWCSNCatTest, NonEmptyDest) {
+  wchar_t dest[7] = {L'x', L'y', L'z', L'\0'};
+  const wchar_t *src = L"abc";
+
+  // Adding on only part of the string
+  LIBC_NAMESPACE::wcsncat(dest, src, 1);
+  ASSERT_TRUE(dest[0] == L'x');
+  ASSERT_TRUE(dest[1] == L'y');
+  ASSERT_TRUE(dest[2] == L'z');
+  ASSERT_TRUE(dest[3] == L'a');
+  ASSERT_TRUE(dest[4] == L'\0');
+
+  // Copying more without resetting
+  LIBC_NAMESPACE::wcsncat(dest, src, 2);
+  ASSERT_TRUE(dest[0] == L'x');
+  ASSERT_TRUE(dest[1] == L'y');
+  ASSERT_TRUE(dest[2] == L'z');
+  ASSERT_TRUE(dest[3] == L'a');
+  ASSERT_TRUE(dest[4] == L'a');
+  ASSERT_TRUE(dest[5] == L'b');
+  ASSERT_TRUE(dest[6] == L'\0');
+
+  // Setting end marker to make sure it overwrites properly.
+  dest[3] = L'\0';
+
+  // Copying all of it.
+  LIBC_NAMESPACE::wcsncat(dest, src, 3);
+  ASSERT_TRUE(dest[0] == L'x');
+  ASSERT_TRUE(dest[1] == L'y');
+  ASSERT_TRUE(dest[2] == L'z');
+  ASSERT_TRUE(dest[3] == L'a');
+  ASSERT_TRUE(dest[4] == L'b');
+  ASSERT_TRUE(dest[5] == L'c');
+  ASSERT_TRUE(dest[6] == L'\0');
+
+  // Check that copying still works when count > src length.
+  dest[0] = L'\0';
+  // And that it doesn't write beyond what is necessary.
+  dest[4] = L'Z';
+  LIBC_NAMESPACE::wcsncat(dest, src, 4);
+  ASSERT_TRUE(dest[0] == L'a');
+  ASSERT_TRUE(dest[1] == L'b');
+  ASSERT_TRUE(dest[2] == L'c');
+  ASSERT_TRUE(dest[3] == L'\0');
+  ASSERT_TRUE(dest[4] == L'Z');
+}

>From f8f8f1ef07c5a5280843a2c9a88b4a1af12c7b6d Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Mon, 2 Jun 2025 16:50:25 +0000
Subject: [PATCH 2/3] [libc] wcsncat implementation

Implemented wcsncat and tests.
---
 libc/src/wchar/wcsncat.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/wchar/wcsncat.h b/libc/src/wchar/wcsncat.h
index d7b03e9d43616..978645e90c6ba 100644
--- a/libc/src/wchar/wcsncat.h
+++ b/libc/src/wchar/wcsncat.h
@@ -15,7 +15,8 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-wchar_t *wcsncat(wchar_t *__restrict s1, const wchar_t *__restrict s2, size_t n);
+wchar_t *wcsncat(wchar_t *__restrict s1, const wchar_t *__restrict s2,
+                 size_t n);
 
 } // namespace LIBC_NAMESPACE_DECL
 

>From 21d368d6a78fabdf8ad4c8ece6f2c3e8c0a38d16 Mon Sep 17 00:00:00 2001
From: Sriya Pratipati <sriyap at google.com>
Date: Mon, 2 Jun 2025 20:07:48 +0000
Subject: [PATCH 3/3] Removed unnecessary braces

---
 libc/src/wchar/wcsncat.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/src/wchar/wcsncat.cpp b/libc/src/wchar/wcsncat.cpp
index c8be2903a1158..62595b4b5418c 100644
--- a/libc/src/wchar/wcsncat.cpp
+++ b/libc/src/wchar/wcsncat.cpp
@@ -21,9 +21,8 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcsncat,
                     size_t n)) {
   size_t size = internal::string_length(s1);
   size_t i = 0;
-  for (; s2[i] && i < n; ++i) {
+  for (; s2[i] && i < n; ++i)
     s1[size + i] = s2[i];
-  }
   // Appending null character to the end of the result.
   s1[size + i] = L'\0';
   return s1;



More information about the libc-commits mailing list