1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
16 std::string Twine::str() const {
17 // If we're storing only a std::string, just return it.
18 if (LHSKind == StdStringKind && RHSKind == EmptyKind)
19 return *static_cast<const std::string*>(LHS);
21 // Otherwise, flatten and copy the contents first.
23 return toStringRef(Vec).str();
26 void Twine::toVector(SmallVectorImpl<char> &Out) const {
27 raw_svector_ostream OS(Out);
31 StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
32 if (isSingleStringRef())
33 return getSingleStringRef();
35 return StringRef(Out.data(), Out.size());
38 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
40 switch (getLHSKind()) {
42 // Already null terminated, yay!
43 return StringRef(static_cast<const char*>(LHS));
45 const std::string *str = static_cast<const std::string*>(LHS);
46 return StringRef(str->c_str(), str->size());
55 return StringRef(Out.data(), Out.size());
58 void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
59 NodeKind Kind) const {
61 case Twine::NullKind: break;
62 case Twine::EmptyKind: break;
63 case Twine::TwineKind:
64 static_cast<const Twine*>(Ptr)->print(OS);
66 case Twine::CStringKind:
67 OS << static_cast<const char*>(Ptr);
69 case Twine::StdStringKind:
70 OS << *static_cast<const std::string*>(Ptr);
72 case Twine::StringRefKind:
73 OS << *static_cast<const StringRef*>(Ptr);
75 case Twine::DecUIKind:
76 OS << (unsigned)(uintptr_t)Ptr;
79 OS << (int)(intptr_t)Ptr;
81 case Twine::DecULKind:
82 OS << *static_cast<const unsigned long*>(Ptr);
85 OS << *static_cast<const long*>(Ptr);
87 case Twine::DecULLKind:
88 OS << *static_cast<const unsigned long long*>(Ptr);
90 case Twine::DecLLKind:
91 OS << *static_cast<const long long*>(Ptr);
94 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
99 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
100 NodeKind Kind) const {
102 case Twine::NullKind:
104 case Twine::EmptyKind:
105 OS << "empty"; break;
106 case Twine::TwineKind:
108 static_cast<const Twine*>(Ptr)->printRepr(OS);
110 case Twine::CStringKind:
112 << static_cast<const char*>(Ptr) << "\"";
114 case Twine::StdStringKind:
115 OS << "std::string:\""
116 << static_cast<const std::string*>(Ptr) << "\"";
118 case Twine::StringRefKind:
120 << static_cast<const StringRef*>(Ptr) << "\"";
122 case Twine::DecUIKind:
123 OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
125 case Twine::DecIKind:
126 OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
128 case Twine::DecULKind:
129 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
131 case Twine::DecLKind:
132 OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
134 case Twine::DecULLKind:
135 OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
137 case Twine::DecLLKind:
138 OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
140 case Twine::UHexKind:
141 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
146 void Twine::print(raw_ostream &OS) const {
147 printOneChild(OS, LHS, getLHSKind());
148 printOneChild(OS, RHS, getRHSKind());
151 void Twine::printRepr(raw_ostream &OS) const {
153 printOneChildRepr(OS, LHS, getLHSKind());
155 printOneChildRepr(OS, RHS, getRHSKind());
159 void Twine::dump() const {
163 void Twine::dumpRepr() const {
164 printRepr(llvm::dbgs());