[libc-commits] [libc] [libc] Implemented wcscmp (PR #142423)

Uzair Nawaz via libc-commits libc-commits at lists.llvm.org
Mon Jun 2 11:25:21 PDT 2025


https://github.com/uzairnawaz updated https://github.com/llvm/llvm-project/pull/142423

>From e6e516721c8c75fbbf87ddff1832ad605bdf921e Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Mon, 2 Jun 2025 16:13:06 +0000
Subject: [PATCH 1/2] implemented wcscmp

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/include/wchar.yaml                  |  7 ++
 libc/src/wchar/CMakeLists.txt            | 11 +++
 libc/src/wchar/wcscmp.cpp                | 30 ++++++++
 libc/src/wchar/wcscmp.h                  | 22 ++++++
 libc/test/src/wchar/CMakeLists.txt       | 10 +++
 libc/test/src/wchar/wcscmp_test.cpp      | 97 ++++++++++++++++++++++++
 7 files changed, 178 insertions(+)
 create mode 100644 libc/src/wchar/wcscmp.cpp
 create mode 100644 libc/src/wchar/wcscmp.h
 create mode 100644 libc/test/src/wchar/wcscmp_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 545b9227349fe..9c76067a9a042 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -366,6 +366,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wctob
     libc.src.wchar.wmemset
     libc.src.wchar.wcschr
+    libc.src.wchar.wcscmp
     libc.src.wchar.wcspbrk
     libc.src.wchar.wcsspn
     libc.src.wchar.wmemcmp
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index bfd9a10342019..a6be5bedf7a1e 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -42,6 +42,13 @@ functions:
     arguments: 
       - type: const wchar_t *
       - type: wchar_t
+  - name: wcscmp
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: const wchar_t *
+      - type: const wchar_t *
   - name: wcspbrk
     standards:
       - stdc
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 9db121762348b..420a8a277c4a0 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -57,6 +57,17 @@ add_entrypoint_object(
     libc.src.__support.wctype_utils
 )
 
+add_entrypoint_object(
+  wcscmp
+  SRCS
+    wcscmp.cpp
+  HDRS
+    wcscmp.h
+  DEPENDS
+    libc.hdr.wchar_macros
+    libc.src.__support.wctype_utils
+)
+
 add_entrypoint_object(
   wcspbrk
   SRCS
diff --git a/libc/src/wchar/wcscmp.cpp b/libc/src/wchar/wcscmp.cpp
new file mode 100644
index 0000000000000..f285efd905390
--- /dev/null
+++ b/libc/src/wchar/wcscmp.cpp
@@ -0,0 +1,30 @@
+//===-- Implementation of wcscmp ------------------------------------------===//
+//
+// 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/wcscmp.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, wcscmp, (const wchar_t *left, const wchar_t *right)) {
+  LIBC_CRASH_ON_NULLPTR(left);
+  LIBC_CRASH_ON_NULLPTR(right);
+
+  auto comp = [](wchar_t l, wchar_t r) -> int { return l - r; };
+
+  for (; *left && !comp(*left, *right); ++left, ++right)
+    ;
+
+  return comp(*left, *right);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcscmp.h b/libc/src/wchar/wcscmp.h
new file mode 100644
index 0000000000000..af82d063f017f
--- /dev/null
+++ b/libc/src/wchar/wcscmp.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wcscmp ----------------------------------===//
+//
+// 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_WCSCMP_H
+#define LLVM_LIBC_SRC_WCHAR_WCSCMP_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int wcscmp(const wchar_t *left, const wchar_t *right);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCSCMP_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 9bc230e0bddf3..9a3585554a0db 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -55,6 +55,16 @@ add_libc_test(
     libc.src.wchar.wcschr
 )
 
+add_libc_test(
+  wcscmp_test
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    wcscmp_test.cpp
+  DEPENDS
+    libc.src.wchar.wcscmp
+)
+
 add_libc_test(
   wcspbrk_test
   SUITE
diff --git a/libc/test/src/wchar/wcscmp_test.cpp b/libc/test/src/wchar/wcscmp_test.cpp
new file mode 100644
index 0000000000000..6572aadb066ae
--- /dev/null
+++ b/libc/test/src/wchar/wcscmp_test.cpp
@@ -0,0 +1,97 @@
+//===-- Unittests for wcscmp ----------------------------------------------===//
+//
+// 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/wcscmp.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWcscmpTest, EmptyStringsShouldReturnZero) {
+  const wchar_t *s1 = L"";
+  const wchar_t *s2 = L"";
+  int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+  ASSERT_EQ(result, 0);
+
+  // Verify operands reversed.
+  result = LIBC_NAMESPACE::wcscmp(s2, s1);
+  ASSERT_EQ(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, EmptyStringShouldNotEqualNonEmptyString) {
+  const wchar_t *empty = L"";
+  const wchar_t *s2 = L"abc";
+  int result = LIBC_NAMESPACE::wcscmp(empty, s2);
+  ASSERT_LT(result, 0);
+
+  // Similar case if empty string is second argument.
+  const wchar_t *s3 = L"123";
+  result = LIBC_NAMESPACE::wcscmp(s3, empty);
+  ASSERT_GT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, EqualStringsShouldReturnZero) {
+  const wchar_t *s1 = L"abc";
+  const wchar_t *s2 = L"abc";
+  int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+  ASSERT_EQ(result, 0);
+
+  // Verify operands reversed.
+  result = LIBC_NAMESPACE::wcscmp(s2, s1);
+  ASSERT_EQ(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, ShouldReturnResultOfFirstDifference) {
+  const wchar_t *s1 = L"___B42__";
+  const wchar_t *s2 = L"___C55__";
+  int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+  ASSERT_LT(result, 0);
+
+  // Verify operands reversed.
+  result = LIBC_NAMESPACE::wcscmp(s2, s1);
+  ASSERT_GT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, CapitalizedLetterShouldNotBeEqual) {
+  const wchar_t *s1 = L"abcd";
+  const wchar_t *s2 = L"abCd";
+  int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+  ASSERT_GT(result, 0);
+
+  // Verify operands reversed.
+  result = LIBC_NAMESPACE::wcscmp(s2, s1);
+  ASSERT_LT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, UnequalLengthStringsShouldNotReturnZero) {
+  const wchar_t *s1 = L"abc";
+  const wchar_t *s2 = L"abcd";
+  int result = LIBC_NAMESPACE::wcscmp(s1, s2);
+  ASSERT_LT(result, 0);
+
+  // Verify operands reversed.
+  result = LIBC_NAMESPACE::wcscmp(s2, s1);
+  ASSERT_GT(result, 0);
+}
+
+TEST(LlvmLibcWcscmpTest, StringArgumentSwapChangesSign) {
+  const wchar_t *a = L"a";
+  const wchar_t *b = L"b";
+  int result = LIBC_NAMESPACE::wcscmp(b, a);
+  ASSERT_GT(result, 0);
+
+  result = LIBC_NAMESPACE::wcscmp(a, b);
+  ASSERT_LT(result, 0);
+}
+
+#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
+TEST(LlvmLibcWcscmpTest, NullptrCrash) {
+  // Passing in a nullptr should crash the program.
+  EXPECT_DEATH([] { LIBC_NAMESPACE::wcscmp(L"aaaaaaaaaaaaaa", nullptr); },
+               WITH_SIGNAL(-1));
+  EXPECT_DEATH([] { LIBC_NAMESPACE::wcscmp(nullptr, L"aaaaaaaaaaaaaa"); },
+               WITH_SIGNAL(-1));
+}
+#endif // LIBC_HAS_ADDRESS_SANITIZER

>From 5c3c5ca08371161628a9a65b310c0fcfc22c56f1 Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Mon, 2 Jun 2025 18:24:46 +0000
Subject: [PATCH 2/2] removed unnecessary dependency

---
 libc/src/wchar/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 420a8a277c4a0..2482ebb761b70 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -65,7 +65,6 @@ add_entrypoint_object(
     wcscmp.h
   DEPENDS
     libc.hdr.wchar_macros
-    libc.src.__support.wctype_utils
 )
 
 add_entrypoint_object(



More information about the libc-commits mailing list