[libc-commits] [libc] 43c543a - [libc][NFC] Make strchr and strrchr more consistent

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Sep 28 10:42:08 PDT 2021


Author: Michael Jones
Date: 2021-09-28T17:42:03Z
New Revision: 43c543aab7e3e6d5727097600ef13a352d1fb4e9

URL: https://github.com/llvm/llvm-project/commit/43c543aab7e3e6d5727097600ef13a352d1fb4e9
DIFF: https://github.com/llvm/llvm-project/commit/43c543aab7e3e6d5727097600ef13a352d1fb4e9.diff

LOG: [libc][NFC] Make strchr and strrchr more consistent

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D110581

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libc/src/string/strchr.cpp b/libc/src/string/strchr.cpp
index 6288b64b74628..ab663eac500e1 100644
--- a/libc/src/string/strchr.cpp
+++ b/libc/src/string/strchr.cpp
@@ -14,12 +14,10 @@ namespace __llvm_libc {
 
 // 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

diff  --git a/libc/src/string/strrchr.cpp b/libc/src/string/strrchr.cpp
index 33a638bacd862..b5852add7fe9a 100644
--- a/libc/src/string/strrchr.cpp
+++ b/libc/src/string/strrchr.cpp
@@ -15,10 +15,10 @@ namespace __llvm_libc {
 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;
 }
 


        


More information about the libc-commits mailing list