[llvm-commits] [llvm] r77576 - in /llvm/trunk: include/llvm/ADT/Twine.h lib/Support/Twine.cpp unittests/ADT/TwineTest.cpp

Daniel Dunbar daniel at zuster.org
Wed Jul 29 20:47:24 PDT 2009


Author: ddunbar
Date: Wed Jul 29 22:47:15 2009
New Revision: 77576

URL: http://llvm.org/viewvc/llvm-project?rev=77576&view=rev
Log:
Twine: Provide [u]int{32,64} conversions via implicit constructors instead of
explicitly.

Modified:
    llvm/trunk/include/llvm/ADT/Twine.h
    llvm/trunk/lib/Support/Twine.cpp
    llvm/trunk/unittests/ADT/TwineTest.cpp

Modified: llvm/trunk/include/llvm/ADT/Twine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Twine.h?rev=77576&r1=77575&r2=77576&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/Twine.h (original)
+++ llvm/trunk/include/llvm/ADT/Twine.h Wed Jul 29 22:47:15 2009
@@ -101,14 +101,21 @@
 
       /// A pointer to a uint64_t value, to render as an unsigned decimal
       /// integer.
-      UDecKind,
+      UDec32Kind,
 
-      /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
+      /// A pointer to a uint64_t value, to render as a signed decimal integer.
+      SDec32Kind,
+
+      /// A pointer to a uint64_t value, to render as an unsigned decimal
       /// integer.
-      UHexKind,
+      UDec64Kind,
 
       /// A pointer to a uint64_t value, to render as a signed decimal integer.
-      SDecKind
+      SDec64Kind,
+
+      /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
+      /// integer.
+      UHexKind
     };
 
   private:
@@ -244,6 +251,26 @@
       assert(isValid() && "Invalid twine!");
     }
 
+    /// Construct a twine to print \arg Val as an unsigned decimal integer.
+    Twine(const uint32_t &Val) 
+      : LHS(&Val), LHSKind(UDec32Kind), RHSKind(EmptyKind) {
+    }
+
+    /// Construct a twine to print \arg Val as a signed decimal integer.
+    Twine(const int32_t &Val) 
+      : LHS(&Val), LHSKind(SDec32Kind), RHSKind(EmptyKind) {
+    }
+
+    /// Construct a twine to print \arg Val as an unsigned decimal integer.
+    Twine(const uint64_t &Val) 
+      : LHS(&Val), LHSKind(UDec64Kind), RHSKind(EmptyKind) {
+    }
+
+    /// Construct a twine to print \arg Val as a signed decimal integer.
+    Twine(const int64_t &Val) 
+      : LHS(&Val), LHSKind(SDec64Kind), RHSKind(EmptyKind) {
+    }
+
     // FIXME: Unfortunately, to make sure this is as efficient as possible we
     // need extra binary constructors from particular types. We can't rely on
     // the compiler to be smart enough to fold operator+()/concat() down to the
@@ -271,16 +298,6 @@
     /// @name Numeric Conversions
     /// @{
 
-    /// Construct a twine to print \arg Val as an unsigned decimal integer.
-    static Twine utostr(const uint64_t &Val) {
-      return Twine(&Val, UDecKind, 0, EmptyKind);
-    }
-
-    /// Construct a twine to print \arg Val as a signed decimal integer.
-    static Twine itostr(const int64_t &Val) {
-      return Twine(&Val, SDecKind, 0, EmptyKind);
-    }
-
     // Construct a twine to print \arg Val as an unsigned hexadecimal integer.
     static Twine utohexstr(const uint64_t &Val) {
       return Twine(&Val, UHexKind, 0, EmptyKind);

Modified: llvm/trunk/lib/Support/Twine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Twine.cpp?rev=77576&r1=77575&r2=77576&view=diff

==============================================================================
--- llvm/trunk/lib/Support/Twine.cpp (original)
+++ llvm/trunk/lib/Support/Twine.cpp Wed Jul 29 22:47:15 2009
@@ -47,10 +47,16 @@
   case Twine::StringRefKind:
     OS << *static_cast<const StringRef*>(Ptr); 
     break;
-  case Twine::UDecKind:
+  case Twine::UDec32Kind:
+    OS << *static_cast<const uint32_t*>(Ptr);
+    break;
+  case Twine::SDec32Kind:
+    OS << *static_cast<const int32_t*>(Ptr);
+    break;
+  case Twine::UDec64Kind:
     OS << *static_cast<const uint64_t*>(Ptr);
     break;
-  case Twine::SDecKind:
+  case Twine::SDec64Kind:
     OS << *static_cast<const int64_t*>(Ptr);
     break;
   case Twine::UHexKind:
@@ -83,11 +89,17 @@
     OS << "stringref:\""
        << static_cast<const StringRef*>(Ptr) << "\"";
     break;
-  case Twine::UDecKind:
-    OS << "udec:" << static_cast<const uint64_t*>(Ptr) << "\"";
+  case Twine::UDec32Kind:
+    OS << "udec32:" << static_cast<const uint64_t*>(Ptr) << "\"";
+    break;
+  case Twine::SDec32Kind:
+    OS << "sdec32:" << static_cast<const int64_t*>(Ptr) << "\"";
+    break;
+  case Twine::UDec64Kind:
+    OS << "udec64:" << static_cast<const uint64_t*>(Ptr) << "\"";
     break;
-  case Twine::SDecKind:
-    OS << "sdec:" << static_cast<const int64_t*>(Ptr) << "\"";
+  case Twine::SDec64Kind:
+    OS << "sdec64:" << static_cast<const int64_t*>(Ptr) << "\"";
     break;
   case Twine::UHexKind:
     OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";

Modified: llvm/trunk/unittests/ADT/TwineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/TwineTest.cpp?rev=77576&r1=77575&r2=77576&view=diff

==============================================================================
--- llvm/trunk/unittests/ADT/TwineTest.cpp (original)
+++ llvm/trunk/unittests/ADT/TwineTest.cpp Wed Jul 29 22:47:15 2009
@@ -31,12 +31,13 @@
 }
 
 TEST(TwineTest, Numbers) {
-  EXPECT_EQ("123", Twine::utostr(123).str());
-  EXPECT_EQ("-123", Twine::itostr(-123).str());
-  EXPECT_EQ("123", Twine::utostr(123).str());
-  EXPECT_EQ("-123", Twine::itostr(-123).str());
-  EXPECT_EQ("123", Twine::utostr((char) 123).str());
-  EXPECT_EQ("-123", Twine::itostr((signed char) -123).str());
+  EXPECT_EQ("123", Twine(123U).str());
+  EXPECT_EQ("123", Twine(123).str());
+  EXPECT_EQ("-123", Twine(-123).str());
+  EXPECT_EQ("123", Twine(123).str());
+  EXPECT_EQ("-123", Twine(-123).str());
+  EXPECT_EQ("123", Twine((char) 123).str());
+  EXPECT_EQ("-123", Twine((signed char) -123).str());
 
   EXPECT_EQ("7B", Twine::utohexstr(123).str());
   EXPECT_EQ("FFFFFFFFFFFFFF85", Twine::itohexstr(-123).str());





More information about the llvm-commits mailing list