1f4a2713aSLionel Sambuc //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "llvm/ADT/Twine.h"
11f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
12f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
13f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
14f4a2713aSLionel Sambuc using namespace llvm;
15f4a2713aSLionel Sambuc 
str() const16f4a2713aSLionel Sambuc std::string Twine::str() const {
17f4a2713aSLionel Sambuc   // If we're storing only a std::string, just return it.
18f4a2713aSLionel Sambuc   if (LHSKind == StdStringKind && RHSKind == EmptyKind)
19f4a2713aSLionel Sambuc     return *LHS.stdString;
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc   // Otherwise, flatten and copy the contents first.
22f4a2713aSLionel Sambuc   SmallString<256> Vec;
23f4a2713aSLionel Sambuc   return toStringRef(Vec).str();
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc 
toVector(SmallVectorImpl<char> & Out) const26f4a2713aSLionel Sambuc void Twine::toVector(SmallVectorImpl<char> &Out) const {
27f4a2713aSLionel Sambuc   raw_svector_ostream OS(Out);
28f4a2713aSLionel Sambuc   print(OS);
29f4a2713aSLionel Sambuc }
30f4a2713aSLionel Sambuc 
toStringRef(SmallVectorImpl<char> & Out) const31f4a2713aSLionel Sambuc StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
32f4a2713aSLionel Sambuc   if (isSingleStringRef())
33f4a2713aSLionel Sambuc     return getSingleStringRef();
34f4a2713aSLionel Sambuc   toVector(Out);
35f4a2713aSLionel Sambuc   return StringRef(Out.data(), Out.size());
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
toNullTerminatedStringRef(SmallVectorImpl<char> & Out) const38f4a2713aSLionel Sambuc StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
39f4a2713aSLionel Sambuc   if (isUnary()) {
40f4a2713aSLionel Sambuc     switch (getLHSKind()) {
41f4a2713aSLionel Sambuc     case CStringKind:
42f4a2713aSLionel Sambuc       // Already null terminated, yay!
43f4a2713aSLionel Sambuc       return StringRef(LHS.cString);
44f4a2713aSLionel Sambuc     case StdStringKind: {
45f4a2713aSLionel Sambuc       const std::string *str = LHS.stdString;
46f4a2713aSLionel Sambuc       return StringRef(str->c_str(), str->size());
47f4a2713aSLionel Sambuc     }
48f4a2713aSLionel Sambuc     default:
49f4a2713aSLionel Sambuc       break;
50f4a2713aSLionel Sambuc     }
51f4a2713aSLionel Sambuc   }
52f4a2713aSLionel Sambuc   toVector(Out);
53f4a2713aSLionel Sambuc   Out.push_back(0);
54f4a2713aSLionel Sambuc   Out.pop_back();
55f4a2713aSLionel Sambuc   return StringRef(Out.data(), Out.size());
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc 
printOneChild(raw_ostream & OS,Child Ptr,NodeKind Kind) const58f4a2713aSLionel Sambuc void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59f4a2713aSLionel Sambuc                           NodeKind Kind) const {
60f4a2713aSLionel Sambuc   switch (Kind) {
61f4a2713aSLionel Sambuc   case Twine::NullKind: break;
62f4a2713aSLionel Sambuc   case Twine::EmptyKind: break;
63f4a2713aSLionel Sambuc   case Twine::TwineKind:
64f4a2713aSLionel Sambuc     Ptr.twine->print(OS);
65f4a2713aSLionel Sambuc     break;
66f4a2713aSLionel Sambuc   case Twine::CStringKind:
67f4a2713aSLionel Sambuc     OS << Ptr.cString;
68f4a2713aSLionel Sambuc     break;
69f4a2713aSLionel Sambuc   case Twine::StdStringKind:
70f4a2713aSLionel Sambuc     OS << *Ptr.stdString;
71f4a2713aSLionel Sambuc     break;
72f4a2713aSLionel Sambuc   case Twine::StringRefKind:
73f4a2713aSLionel Sambuc     OS << *Ptr.stringRef;
74f4a2713aSLionel Sambuc     break;
75f4a2713aSLionel Sambuc   case Twine::CharKind:
76f4a2713aSLionel Sambuc     OS << Ptr.character;
77f4a2713aSLionel Sambuc     break;
78f4a2713aSLionel Sambuc   case Twine::DecUIKind:
79f4a2713aSLionel Sambuc     OS << Ptr.decUI;
80f4a2713aSLionel Sambuc     break;
81f4a2713aSLionel Sambuc   case Twine::DecIKind:
82f4a2713aSLionel Sambuc     OS << Ptr.decI;
83f4a2713aSLionel Sambuc     break;
84f4a2713aSLionel Sambuc   case Twine::DecULKind:
85f4a2713aSLionel Sambuc     OS << *Ptr.decUL;
86f4a2713aSLionel Sambuc     break;
87f4a2713aSLionel Sambuc   case Twine::DecLKind:
88f4a2713aSLionel Sambuc     OS << *Ptr.decL;
89f4a2713aSLionel Sambuc     break;
90f4a2713aSLionel Sambuc   case Twine::DecULLKind:
91f4a2713aSLionel Sambuc     OS << *Ptr.decULL;
92f4a2713aSLionel Sambuc     break;
93f4a2713aSLionel Sambuc   case Twine::DecLLKind:
94f4a2713aSLionel Sambuc     OS << *Ptr.decLL;
95f4a2713aSLionel Sambuc     break;
96f4a2713aSLionel Sambuc   case Twine::UHexKind:
97f4a2713aSLionel Sambuc     OS.write_hex(*Ptr.uHex);
98f4a2713aSLionel Sambuc     break;
99f4a2713aSLionel Sambuc   }
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
printOneChildRepr(raw_ostream & OS,Child Ptr,NodeKind Kind) const102f4a2713aSLionel Sambuc void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
103f4a2713aSLionel Sambuc                               NodeKind Kind) const {
104f4a2713aSLionel Sambuc   switch (Kind) {
105f4a2713aSLionel Sambuc   case Twine::NullKind:
106f4a2713aSLionel Sambuc     OS << "null"; break;
107f4a2713aSLionel Sambuc   case Twine::EmptyKind:
108f4a2713aSLionel Sambuc     OS << "empty"; break;
109f4a2713aSLionel Sambuc   case Twine::TwineKind:
110f4a2713aSLionel Sambuc     OS << "rope:";
111f4a2713aSLionel Sambuc     Ptr.twine->printRepr(OS);
112f4a2713aSLionel Sambuc     break;
113f4a2713aSLionel Sambuc   case Twine::CStringKind:
114f4a2713aSLionel Sambuc     OS << "cstring:\""
115f4a2713aSLionel Sambuc        << Ptr.cString << "\"";
116f4a2713aSLionel Sambuc     break;
117f4a2713aSLionel Sambuc   case Twine::StdStringKind:
118f4a2713aSLionel Sambuc     OS << "std::string:\""
119f4a2713aSLionel Sambuc        << Ptr.stdString << "\"";
120f4a2713aSLionel Sambuc     break;
121f4a2713aSLionel Sambuc   case Twine::StringRefKind:
122f4a2713aSLionel Sambuc     OS << "stringref:\""
123f4a2713aSLionel Sambuc        << Ptr.stringRef << "\"";
124f4a2713aSLionel Sambuc     break;
125f4a2713aSLionel Sambuc   case Twine::CharKind:
126f4a2713aSLionel Sambuc     OS << "char:\"" << Ptr.character << "\"";
127f4a2713aSLionel Sambuc     break;
128f4a2713aSLionel Sambuc   case Twine::DecUIKind:
129f4a2713aSLionel Sambuc     OS << "decUI:\"" << Ptr.decUI << "\"";
130f4a2713aSLionel Sambuc     break;
131f4a2713aSLionel Sambuc   case Twine::DecIKind:
132f4a2713aSLionel Sambuc     OS << "decI:\"" << Ptr.decI << "\"";
133f4a2713aSLionel Sambuc     break;
134f4a2713aSLionel Sambuc   case Twine::DecULKind:
135f4a2713aSLionel Sambuc     OS << "decUL:\"" << *Ptr.decUL << "\"";
136f4a2713aSLionel Sambuc     break;
137f4a2713aSLionel Sambuc   case Twine::DecLKind:
138f4a2713aSLionel Sambuc     OS << "decL:\"" << *Ptr.decL << "\"";
139f4a2713aSLionel Sambuc     break;
140f4a2713aSLionel Sambuc   case Twine::DecULLKind:
141f4a2713aSLionel Sambuc     OS << "decULL:\"" << *Ptr.decULL << "\"";
142f4a2713aSLionel Sambuc     break;
143f4a2713aSLionel Sambuc   case Twine::DecLLKind:
144f4a2713aSLionel Sambuc     OS << "decLL:\"" << *Ptr.decLL << "\"";
145f4a2713aSLionel Sambuc     break;
146f4a2713aSLionel Sambuc   case Twine::UHexKind:
147f4a2713aSLionel Sambuc     OS << "uhex:\"" << Ptr.uHex << "\"";
148f4a2713aSLionel Sambuc     break;
149f4a2713aSLionel Sambuc   }
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc 
print(raw_ostream & OS) const152f4a2713aSLionel Sambuc void Twine::print(raw_ostream &OS) const {
153f4a2713aSLionel Sambuc   printOneChild(OS, LHS, getLHSKind());
154f4a2713aSLionel Sambuc   printOneChild(OS, RHS, getRHSKind());
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc 
printRepr(raw_ostream & OS) const157f4a2713aSLionel Sambuc void Twine::printRepr(raw_ostream &OS) const {
158f4a2713aSLionel Sambuc   OS << "(Twine ";
159f4a2713aSLionel Sambuc   printOneChildRepr(OS, LHS, getLHSKind());
160f4a2713aSLionel Sambuc   OS << " ";
161f4a2713aSLionel Sambuc   printOneChildRepr(OS, RHS, getRHSKind());
162f4a2713aSLionel Sambuc   OS << ")";
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc 
dump() const165f4a2713aSLionel Sambuc void Twine::dump() const {
166*0a6a1f1dSLionel Sambuc   print(dbgs());
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc 
dumpRepr() const169f4a2713aSLionel Sambuc void Twine::dumpRepr() const {
170*0a6a1f1dSLionel Sambuc   printRepr(dbgs());
171f4a2713aSLionel Sambuc }
172