LLVM  8.0.1
Twine.cpp
Go to the documentation of this file.
1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/Support/Debug.h"
16 using namespace llvm;
17 
18 std::string Twine::str() const {
19  // If we're storing only a std::string, just return it.
20  if (LHSKind == StdStringKind && RHSKind == EmptyKind)
21  return *LHS.stdString;
22 
23  // If we're storing a formatv_object, we can avoid an extra copy by formatting
24  // it immediately and returning the result.
25  if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
26  return LHS.formatvObject->str();
27 
28  // Otherwise, flatten and copy the contents first.
29  SmallString<256> Vec;
30  return toStringRef(Vec).str();
31 }
32 
34  raw_svector_ostream OS(Out);
35  print(OS);
36 }
37 
39  if (isUnary()) {
40  switch (getLHSKind()) {
41  case CStringKind:
42  // Already null terminated, yay!
43  return StringRef(LHS.cString);
44  case StdStringKind: {
45  const std::string *str = LHS.stdString;
46  return StringRef(str->c_str(), str->size());
47  }
48  default:
49  break;
50  }
51  }
52  toVector(Out);
53  Out.push_back(0);
54  Out.pop_back();
55  return StringRef(Out.data(), Out.size());
56 }
57 
58 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59  NodeKind Kind) const {
60  switch (Kind) {
61  case Twine::NullKind: break;
62  case Twine::EmptyKind: break;
63  case Twine::TwineKind:
64  Ptr.twine->print(OS);
65  break;
66  case Twine::CStringKind:
67  OS << Ptr.cString;
68  break;
69  case Twine::StdStringKind:
70  OS << *Ptr.stdString;
71  break;
72  case Twine::StringRefKind:
73  OS << *Ptr.stringRef;
74  break;
75  case Twine::SmallStringKind:
76  OS << *Ptr.smallString;
77  break;
78  case Twine::FormatvObjectKind:
79  OS << *Ptr.formatvObject;
80  break;
81  case Twine::CharKind:
82  OS << Ptr.character;
83  break;
84  case Twine::DecUIKind:
85  OS << Ptr.decUI;
86  break;
87  case Twine::DecIKind:
88  OS << Ptr.decI;
89  break;
90  case Twine::DecULKind:
91  OS << *Ptr.decUL;
92  break;
93  case Twine::DecLKind:
94  OS << *Ptr.decL;
95  break;
96  case Twine::DecULLKind:
97  OS << *Ptr.decULL;
98  break;
99  case Twine::DecLLKind:
100  OS << *Ptr.decLL;
101  break;
102  case Twine::UHexKind:
103  OS.write_hex(*Ptr.uHex);
104  break;
105  }
106 }
107 
108 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
109  NodeKind Kind) const {
110  switch (Kind) {
111  case Twine::NullKind:
112  OS << "null"; break;
113  case Twine::EmptyKind:
114  OS << "empty"; break;
115  case Twine::TwineKind:
116  OS << "rope:";
117  Ptr.twine->printRepr(OS);
118  break;
119  case Twine::CStringKind:
120  OS << "cstring:\""
121  << Ptr.cString << "\"";
122  break;
123  case Twine::StdStringKind:
124  OS << "std::string:\""
125  << Ptr.stdString << "\"";
126  break;
127  case Twine::StringRefKind:
128  OS << "stringref:\""
129  << Ptr.stringRef << "\"";
130  break;
131  case Twine::SmallStringKind:
132  OS << "smallstring:\"" << *Ptr.smallString << "\"";
133  break;
134  case Twine::FormatvObjectKind:
135  OS << "formatv:\"" << *Ptr.formatvObject << "\"";
136  break;
137  case Twine::CharKind:
138  OS << "char:\"" << Ptr.character << "\"";
139  break;
140  case Twine::DecUIKind:
141  OS << "decUI:\"" << Ptr.decUI << "\"";
142  break;
143  case Twine::DecIKind:
144  OS << "decI:\"" << Ptr.decI << "\"";
145  break;
146  case Twine::DecULKind:
147  OS << "decUL:\"" << *Ptr.decUL << "\"";
148  break;
149  case Twine::DecLKind:
150  OS << "decL:\"" << *Ptr.decL << "\"";
151  break;
152  case Twine::DecULLKind:
153  OS << "decULL:\"" << *Ptr.decULL << "\"";
154  break;
155  case Twine::DecLLKind:
156  OS << "decLL:\"" << *Ptr.decLL << "\"";
157  break;
158  case Twine::UHexKind:
159  OS << "uhex:\"" << Ptr.uHex << "\"";
160  break;
161  }
162 }
163 
164 void Twine::print(raw_ostream &OS) const {
165  printOneChild(OS, LHS, getLHSKind());
166  printOneChild(OS, RHS, getRHSKind());
167 }
168 
169 void Twine::printRepr(raw_ostream &OS) const {
170  OS << "(Twine ";
171  printOneChildRepr(OS, LHS, getLHSKind());
172  OS << " ";
173  printOneChildRepr(OS, RHS, getRHSKind());
174  OS << ")";
175 }
176 
177 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
179  print(dbgs());
180 }
181 
183  printRepr(dbgs());
184 }
185 #endif
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void push_back(const T &Elt)
Definition: SmallVector.h:218
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:453
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
void dumpRepr() const
Dump the representation of this twine to stderr.
Definition: Twine.cpp:182
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
void dump() const
Dump the concatenated string represented by this twine to stderr.
Definition: Twine.cpp:178
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
Definition: Twine.cpp:38
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:33
size_t size() const
Definition: SmallVector.h:53
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:18
const unsigned Kind
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Definition: Twine.cpp:169
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
Definition: Twine.cpp:164