[libc-commits] [PATCH] D106511: [libc] Add explicit C++ casting to strchr and strrchr

Alf via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jul 21 18:39:47 PDT 2021


gAlfonso-bit updated this revision to Diff 360679.
gAlfonso-bit added a comment.

Added \0 so we know what we are searching for


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D106511

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
@@ -13,12 +13,12 @@
 namespace __llvm_libc {
 
 LLVM_LIBC_FUNCTION(char *, strrchr, (const char *src, int c)) {
-  const char ch = c;
+  const char ch = static_cast<char>(c);
   char *last_occurrence = nullptr;
   do {
     if (*src == ch)
       last_occurrence = const_cast<char *>(src);
-  } while (*src++);
+  } while (*src++ != '\0');
   return last_occurrence;
 }
 
Index: libc/src/string/strchr.cpp
===================================================================
--- libc/src/string/strchr.cpp
+++ libc/src/string/strchr.cpp
@@ -14,12 +14,12 @@
 
 // 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)
-    ;
-  return *str == ch ? reinterpret_cast<char *>(str) : nullptr;
+  const char ch = static_cast<char>(c);
+  for (; *src != ch; ++src)
+    if (*src == '\0')
+      return nullptr;
+
+  return const_cast<char *>(src);
 }
 
 } // namespace __llvm_libc


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106511.360679.patch
Type: text/x-patch
Size: 1279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20210722/39f5c023/attachment.bin>


More information about the libc-commits mailing list