[libc-commits] [libc] [libc] Simplify wcscmp (PR #143457)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Jun 9 16:19:45 PDT 2025


https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/143457

The implementation of wcscmp mimicked strcmp, but was more complicated
than necessary. This patch makes it simpler.


>From f784accc9d23ad33866da512fa4cbc8b7471782e Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Mon, 9 Jun 2025 16:18:18 -0700
Subject: [PATCH] [libc] Simplify wcscmp

The implementation of wcscmp mimicked strcmp, but was more complicated
than necessary. This patch makes it simpler.
---
 libc/src/wchar/wcscmp.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libc/src/wchar/wcscmp.cpp b/libc/src/wchar/wcscmp.cpp
index f285efd905390..48c8b3be6c701 100644
--- a/libc/src/wchar/wcscmp.cpp
+++ b/libc/src/wchar/wcscmp.cpp
@@ -19,12 +19,10 @@ 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)
+  for (; *left && (*left == *right); ++left, ++right)
     ;
 
-  return comp(*left, *right);
+  return *left - *right;
 }
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list