[PATCH] D136024: [ADT] Simplify getAsInteger (NFC)

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 15 14:08:07 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbfb0b6f48db1: [ADT] Simplify getAsInteger and consumeInteger (NFC) (authored by kazu).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D136024

Files:
  llvm/include/llvm/ADT/StringRef.h


Index: llvm/include/llvm/ADT/StringRef.h
===================================================================
--- llvm/include/llvm/ADT/StringRef.h
+++ llvm/include/llvm/ADT/StringRef.h
@@ -458,28 +458,23 @@
     /// If the string is invalid or if only a subset of the string is valid,
     /// this returns true to signify the error.  The string is considered
     /// erroneous if empty or if it overflows T.
-    template <typename T>
-    std::enable_if_t<std::numeric_limits<T>::is_signed, bool>
-    getAsInteger(unsigned Radix, T &Result) const {
-      long long LLVal;
-      if (getAsSignedInteger(*this, Radix, LLVal) ||
+    template <typename T> bool getAsInteger(unsigned Radix, T &Result) const {
+      if constexpr (std::numeric_limits<T>::is_signed) {
+        long long LLVal;
+        if (getAsSignedInteger(*this, Radix, LLVal) ||
             static_cast<T>(LLVal) != LLVal)
-        return true;
-      Result = LLVal;
-      return false;
-    }
-
-    template <typename T>
-    std::enable_if_t<!std::numeric_limits<T>::is_signed, bool>
-    getAsInteger(unsigned Radix, T &Result) const {
-      unsigned long long ULLVal;
-      // The additional cast to unsigned long long is required to avoid the
-      // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
-      // 'unsigned __int64' when instantiating getAsInteger with T = bool.
-      if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
-          static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
-        return true;
-      Result = ULLVal;
+          return true;
+        Result = LLVal;
+      } else {
+        unsigned long long ULLVal;
+        // The additional cast to unsigned long long is required to avoid the
+        // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
+        // 'unsigned __int64' when instantiating getAsInteger with T = bool.
+        if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
+            static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
+          return true;
+        Result = ULLVal;
+      }
       return false;
     }
 
@@ -492,25 +487,20 @@
     /// erroneous if empty or if it overflows T.
     /// The portion of the string representing the discovered numeric value
     /// is removed from the beginning of the string.
-    template <typename T>
-    std::enable_if_t<std::numeric_limits<T>::is_signed, bool>
-    consumeInteger(unsigned Radix, T &Result) {
-      long long LLVal;
-      if (consumeSignedInteger(*this, Radix, LLVal) ||
-          static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
-        return true;
-      Result = LLVal;
-      return false;
-    }
-
-    template <typename T>
-    std::enable_if_t<!std::numeric_limits<T>::is_signed, bool>
-    consumeInteger(unsigned Radix, T &Result) {
-      unsigned long long ULLVal;
-      if (consumeUnsignedInteger(*this, Radix, ULLVal) ||
-          static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
-        return true;
-      Result = ULLVal;
+    template <typename T> bool consumeInteger(unsigned Radix, T &Result) {
+      if constexpr (std::numeric_limits<T>::is_signed) {
+        long long LLVal;
+        if (consumeSignedInteger(*this, Radix, LLVal) ||
+            static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
+          return true;
+        Result = LLVal;
+      } else {
+        unsigned long long ULLVal;
+        if (consumeUnsignedInteger(*this, Radix, ULLVal) ||
+            static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
+          return true;
+        Result = ULLVal;
+      }
       return false;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136024.468037.patch
Type: text/x-patch
Size: 3666 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221015/d696f080/attachment.bin>


More information about the llvm-commits mailing list