1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/Twine.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/FormatVariadic.h"
14 #include "llvm/Support/raw_ostream.h"
15 using namespace llvm;
16 
17 std::string Twine::str() const {
18   // If we're storing only a std::string, just return it.
19   if (LHSKind == StdStringKind && RHSKind == EmptyKind)
20     return *LHS.stdString;
21 
22   // If we're storing a formatv_object, we can avoid an extra copy by formatting
23   // it immediately and returning the result.
24   if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
25     return LHS.formatvObject->str();
26 
27   // Otherwise, flatten and copy the contents first.
28   SmallString<256> Vec;
29   return toStringRef(Vec).str();
30 }
31 
32 void Twine::toVector(SmallVectorImpl<char> &Out) const {
33   raw_svector_ostream OS(Out);
34   print(OS);
35 }
36 
37 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
38   if (isUnary()) {
39     switch (getLHSKind()) {
40     case CStringKind:
41       // Already null terminated, yay!
42       return StringRef(LHS.cString);
43     case StdStringKind: {
44       const std::string *str = LHS.stdString;
45       return StringRef(str->c_str(), str->size());
46     }
47     default:
48       break;
49     }
50   }
51   toVector(Out);
52   Out.push_back(0);
53   Out.pop_back();
54   return StringRef(Out.data(), Out.size());
55 }
56 
57 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
58                           NodeKind Kind) const {
59   switch (Kind) {
60   case Twine::NullKind: break;
61   case Twine::EmptyKind: break;
62   case Twine::TwineKind:
63     Ptr.twine->print(OS);
64     break;
65   case Twine::CStringKind:
66     OS << Ptr.cString;
67     break;
68   case Twine::StdStringKind:
69     OS << *Ptr.stdString;
70     break;
71 #if __cplusplus > 201402L
72   case StdStringViewKind:
73     OS << StringRef(*Ptr.stdStringView);
74     break;
75 #endif
76   case Twine::StringRefKind:
77     OS << *Ptr.stringRef;
78     break;
79   case Twine::SmallStringKind:
80     OS << *Ptr.smallString;
81     break;
82   case Twine::FormatvObjectKind:
83     OS << *Ptr.formatvObject;
84     break;
85   case Twine::CharKind:
86     OS << Ptr.character;
87     break;
88   case Twine::DecUIKind:
89     OS << Ptr.decUI;
90     break;
91   case Twine::DecIKind:
92     OS << Ptr.decI;
93     break;
94   case Twine::DecULKind:
95     OS << *Ptr.decUL;
96     break;
97   case Twine::DecLKind:
98     OS << *Ptr.decL;
99     break;
100   case Twine::DecULLKind:
101     OS << *Ptr.decULL;
102     break;
103   case Twine::DecLLKind:
104     OS << *Ptr.decLL;
105     break;
106   case Twine::UHexKind:
107     OS.write_hex(*Ptr.uHex);
108     break;
109   }
110 }
111 
112 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
113                               NodeKind Kind) const {
114   switch (Kind) {
115   case Twine::NullKind:
116     OS << "null"; break;
117   case Twine::EmptyKind:
118     OS << "empty"; break;
119   case Twine::TwineKind:
120     OS << "rope:";
121     Ptr.twine->printRepr(OS);
122     break;
123   case Twine::CStringKind:
124     OS << "cstring:\""
125        << Ptr.cString << "\"";
126     break;
127   case Twine::StdStringKind:
128     OS << "std::string:\""
129        << Ptr.stdString << "\"";
130     break;
131 #if __cplusplus > 201402L
132   case Twine::StdStringViewKind:
133     OS << "std::string_view:\"" << StringRef(*Ptr.stdStringView) << "\"";
134     break;
135 #endif
136   case Twine::StringRefKind:
137     OS << "stringref:\""
138        << Ptr.stringRef << "\"";
139     break;
140   case Twine::SmallStringKind:
141     OS << "smallstring:\"" << *Ptr.smallString << "\"";
142     break;
143   case Twine::FormatvObjectKind:
144     OS << "formatv:\"" << *Ptr.formatvObject << "\"";
145     break;
146   case Twine::CharKind:
147     OS << "char:\"" << Ptr.character << "\"";
148     break;
149   case Twine::DecUIKind:
150     OS << "decUI:\"" << Ptr.decUI << "\"";
151     break;
152   case Twine::DecIKind:
153     OS << "decI:\"" << Ptr.decI << "\"";
154     break;
155   case Twine::DecULKind:
156     OS << "decUL:\"" << *Ptr.decUL << "\"";
157     break;
158   case Twine::DecLKind:
159     OS << "decL:\"" << *Ptr.decL << "\"";
160     break;
161   case Twine::DecULLKind:
162     OS << "decULL:\"" << *Ptr.decULL << "\"";
163     break;
164   case Twine::DecLLKind:
165     OS << "decLL:\"" << *Ptr.decLL << "\"";
166     break;
167   case Twine::UHexKind:
168     OS << "uhex:\"" << Ptr.uHex << "\"";
169     break;
170   }
171 }
172 
173 void Twine::print(raw_ostream &OS) const {
174   printOneChild(OS, LHS, getLHSKind());
175   printOneChild(OS, RHS, getRHSKind());
176 }
177 
178 void Twine::printRepr(raw_ostream &OS) const {
179   OS << "(Twine ";
180   printOneChildRepr(OS, LHS, getLHSKind());
181   OS << " ";
182   printOneChildRepr(OS, RHS, getRHSKind());
183   OS << ")";
184 }
185 
186 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
187 LLVM_DUMP_METHOD void Twine::dump() const {
188   print(dbgs());
189 }
190 
191 LLVM_DUMP_METHOD void Twine::dumpRepr() const {
192   printRepr(dbgs());
193 }
194 #endif
195