Skip to content

[libc] Simplify wcscmp #143457

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 1 commit into from
Jun 10, 2025
Merged

Conversation

michaelrj-google
Copy link
Contributor

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

The implementation of wcscmp mimicked strcmp, but was more complicated
than necessary. This patch makes it simpler.
@llvmbot llvmbot added the libc label Jun 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 9, 2025

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

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


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

1 Files Affected:

  • (modified) libc/src/wchar/wcscmp.cpp (+2-4)
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

@michaelrj-google michaelrj-google merged commit 06f6a77 into llvm:main Jun 10, 2025
15 checks passed
@michaelrj-google michaelrj-google deleted the libcSimplifyWcscmp branch June 10, 2025 18:24
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
The implementation of wcscmp mimicked strcmp, but was more complicated
than necessary. This patch makes it simpler.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
The implementation of wcscmp mimicked strcmp, but was more complicated
than necessary. This patch makes it simpler.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
The implementation of wcscmp mimicked strcmp, but was more complicated
than necessary. This patch makes it simpler.
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