[libc-commits] [PATCH] D110581: [libc][NFC] Make strchr and strrchr more consistent

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Sep 28 09:49:53 PDT 2021


michaelrj updated this revision to Diff 375616.
michaelrj marked 2 inline comments as done.
michaelrj added a comment.

change to comparing signed chars


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110581/new/

https://reviews.llvm.org/D110581

Files:
  libc/src/string/strchr.cpp
  libc/src/string/strrchr.cpp


Index: libc/src/string/strrchr.cpp
===================================================================
--- libc/src/string/strrchr.cpp
+++ libc/src/string/strrchr.cpp
@@ -15,10 +15,10 @@
 LLVM_LIBC_FUNCTION(char *, strrchr, (const char *src, int c)) {
   const char ch = c;
   char *last_occurrence = nullptr;
-  do {
+  for (; *src; ++src) {
     if (*src == ch)
       last_occurrence = const_cast<char *>(src);
-  } while (*src++);
+  }
   return last_occurrence;
 }
 
Index: libc/src/string/strchr.cpp
===================================================================
--- libc/src/string/strchr.cpp
+++ libc/src/string/strchr.cpp
@@ -14,12 +14,10 @@
 
 // TODO: Look at performance benefits of comparing words.
 LLVM_LIBC_FUNCTION(char *, strchr, (const char *src, int c)) {
-  unsigned char *str =
-      const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(src));
-  const unsigned char ch = c;
-  for (; *str && *str != ch; ++str)
+  const char ch = c;
+  for (; *src && *src != ch; ++src)
     ;
-  return *str == ch ? reinterpret_cast<char *>(str) : nullptr;
+  return *src == ch ? const_cast<char *>(src) : nullptr;
 }
 
 } // namespace __llvm_libc


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110581.375616.patch
Type: text/x-patch
Size: 1176 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210928/f1ed016e/attachment.bin>


More information about the libc-commits mailing list