[llvm-commits] [llvm] r77445 - 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 00:08:44 PDT 2009
Author: ddunbar
Date: Wed Jul 29 02:08:44 2009
New Revision: 77445
URL: http://llvm.org/viewvc/llvm-project?rev=77445&view=rev
Log:
Twines: Support numeric conversion directly (uitostr, etc).
- Provides static constructors for doing number to string conversions without
using temporaries.
- There are several ways to do this, I think given the Twine constraints this
is the simplest one.
- One FIXME for fast number -> hex conversion.
- Added another comment on one last major bit of perf work Twines need, which
is to make raw_svector_ostream more efficient.
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=77445&r1=77444&r2=77445&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Twine.h (original)
+++ llvm/trunk/include/llvm/ADT/Twine.h Wed Jul 29 02:08:44 2009
@@ -86,6 +86,9 @@
/// The empty string.
EmptyKind,
+ /// A pointer to a Twine instance.
+ TwineKind,
+
/// A pointer to a C string instance.
CStringKind,
@@ -95,8 +98,16 @@
/// A pointer to a StringRef instance.
StringRefKind,
- /// A pointer to a Twine instance.
- TwineKind
+ /// A pointer to a uint64_t value, to render as an unsigned decimal
+ /// integer.
+ UDecKind,
+
+ /// A pointer to a uint64_t value, to render as an unsigned hexadecimal
+ /// integer.
+ UHexKind,
+
+ /// A pointer to a uint64_t value, to render as a signed decimal integer.
+ SDecKind
};
private:
@@ -232,12 +243,6 @@
assert(isValid() && "Invalid twine!");
}
- /// Create a 'null' string, which is an empty string that always
- /// concatenates to form another empty string.
- static Twine createNull() {
- return Twine(NullKind);
- }
-
// 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
@@ -255,6 +260,38 @@
assert(isValid() && "Invalid twine!");
}
+ /// Create a 'null' string, which is an empty string that always
+ /// concatenates to form another empty string.
+ static Twine createNull() {
+ return Twine(NullKind);
+ }
+
+ /// @}
+ /// @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);
+ }
+
+ // Construct a twine to print \arg Val as an unsigned hexadecimal
+ // integer. This routine is provided as a convenience to sign extend values
+ // before printing.
+ static Twine itohexstr(const int64_t &Val) {
+ return Twine(&Val, UHexKind, 0, EmptyKind);
+ }
+
/// @}
/// @name String Operations
/// @{
Modified: llvm/trunk/lib/Support/Twine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Twine.cpp?rev=77445&r1=77444&r2=77445&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Twine.cpp (original)
+++ llvm/trunk/lib/Support/Twine.cpp Wed Jul 29 02:08:44 2009
@@ -19,6 +19,13 @@
}
void Twine::toVector(SmallVectorImpl<char> &Out) const {
+ // FIXME: This is very inefficient, since we are creating a large raw_ostream
+ // buffer -- hitting malloc, which we were supposed to avoid -- all when we
+ // have this pretty little small vector available.
+ //
+ // The best way to fix this is to make raw_svector_ostream do the right thing
+ // and be efficient, by augmenting the base raw_ostream with the ability to
+ // have the buffer managed by a concrete implementation.
raw_svector_ostream OS(Out);
print(OS);
}
@@ -28,6 +35,9 @@
switch (Kind) {
case Twine::NullKind: break;
case Twine::EmptyKind: break;
+ case Twine::TwineKind:
+ static_cast<const Twine*>(Ptr)->print(OS);
+ break;
case Twine::CStringKind:
OS << static_cast<const char*>(Ptr);
break;
@@ -37,8 +47,15 @@
case Twine::StringRefKind:
OS << *static_cast<const StringRef*>(Ptr);
break;
- case Twine::TwineKind:
- static_cast<const Twine*>(Ptr)->print(OS);
+ case Twine::UDecKind:
+ OS << *static_cast<const uint64_t*>(Ptr);
+ break;
+ case Twine::SDecKind:
+ OS << *static_cast<const int64_t*>(Ptr);
+ break;
+ case Twine::UHexKind:
+ // FIXME: Add raw_ostream functionality for this.
+ OS << ::utohexstr(*static_cast<const uint64_t*>(Ptr));
break;
}
}
@@ -50,21 +67,30 @@
OS << "null"; break;
case Twine::EmptyKind:
OS << "empty"; break;
+ case Twine::TwineKind:
+ OS << "rope:";
+ static_cast<const Twine*>(Ptr)->printRepr(OS);
+ break;
case Twine::CStringKind:
- OS << "cstring:\""
- << static_cast<const char*>(Ptr) << "\"";
+ OS << "cstring:\""
+ << static_cast<const char*>(Ptr) << "\"";
break;
case Twine::StdStringKind:
- OS << "std::string:\""
- << *static_cast<const std::string*>(Ptr) << "\"";
+ OS << "std::string:\""
+ << static_cast<const std::string*>(Ptr) << "\"";
break;
case Twine::StringRefKind:
- OS << "stringref:\""
- << *static_cast<const StringRef*>(Ptr) << "\"";
+ OS << "stringref:\""
+ << static_cast<const StringRef*>(Ptr) << "\"";
break;
- case Twine::TwineKind:
- OS << "rope:";
- static_cast<const Twine*>(Ptr)->printRepr(OS);
+ case Twine::UDecKind:
+ OS << "udec:" << static_cast<const uint64_t*>(Ptr) << "\"";
+ break;
+ case Twine::SDecKind:
+ OS << "sdec:" << static_cast<const int64_t*>(Ptr) << "\"";
+ break;
+ case Twine::UHexKind:
+ OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";
break;
}
}
Modified: llvm/trunk/unittests/ADT/TwineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/TwineTest.cpp?rev=77445&r1=77444&r2=77445&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/TwineTest.cpp (original)
+++ llvm/trunk/unittests/ADT/TwineTest.cpp Wed Jul 29 02:08:44 2009
@@ -30,6 +30,18 @@
EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
}
+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((char) -123).str());
+
+ EXPECT_EQ("7B", Twine::utohexstr(123).str());
+ EXPECT_EQ("FFFFFFFFFFFFFF85", Twine::itohexstr(-123).str());
+}
+
TEST(TwineTest, Concat) {
// Check verse repr, since we care about the actual representation not just
// the result.
More information about the llvm-commits
mailing list