LLVM  3.7.0
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/Support/Debug.h"
14 using namespace llvm;
15 
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 *LHS.stdString;
20 
21  // Otherwise, flatten and copy the contents first.
22  SmallString<256> Vec;
23  return toStringRef(Vec).str();
24 }
25 
27  raw_svector_ostream OS(Out);
28  print(OS);
29 }
30 
32  if (isUnary()) {
33  switch (getLHSKind()) {
34  case CStringKind:
35  // Already null terminated, yay!
36  return StringRef(LHS.cString);
37  case StdStringKind: {
38  const std::string *str = LHS.stdString;
39  return StringRef(str->c_str(), str->size());
40  }
41  default:
42  break;
43  }
44  }
45  toVector(Out);
46  Out.push_back(0);
47  Out.pop_back();
48  return StringRef(Out.data(), Out.size());
49 }
50 
51 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
52  NodeKind Kind) const {
53  switch (Kind) {
54  case Twine::NullKind: break;
55  case Twine::EmptyKind: break;
56  case Twine::TwineKind:
57  Ptr.twine->print(OS);
58  break;
59  case Twine::CStringKind:
60  OS << Ptr.cString;
61  break;
62  case Twine::StdStringKind:
63  OS << *Ptr.stdString;
64  break;
65  case Twine::StringRefKind:
66  OS << *Ptr.stringRef;
67  break;
68  case Twine::SmallStringKind:
69  OS << *Ptr.smallString;
70  break;
71  case Twine::CharKind:
72  OS << Ptr.character;
73  break;
74  case Twine::DecUIKind:
75  OS << Ptr.decUI;
76  break;
77  case Twine::DecIKind:
78  OS << Ptr.decI;
79  break;
80  case Twine::DecULKind:
81  OS << *Ptr.decUL;
82  break;
83  case Twine::DecLKind:
84  OS << *Ptr.decL;
85  break;
86  case Twine::DecULLKind:
87  OS << *Ptr.decULL;
88  break;
89  case Twine::DecLLKind:
90  OS << *Ptr.decLL;
91  break;
92  case Twine::UHexKind:
93  OS.write_hex(*Ptr.uHex);
94  break;
95  }
96 }
97 
98 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
99  NodeKind Kind) const {
100  switch (Kind) {
101  case Twine::NullKind:
102  OS << "null"; break;
103  case Twine::EmptyKind:
104  OS << "empty"; break;
105  case Twine::TwineKind:
106  OS << "rope:";
107  Ptr.twine->printRepr(OS);
108  break;
109  case Twine::CStringKind:
110  OS << "cstring:\""
111  << Ptr.cString << "\"";
112  break;
113  case Twine::StdStringKind:
114  OS << "std::string:\""
115  << Ptr.stdString << "\"";
116  break;
117  case Twine::StringRefKind:
118  OS << "stringref:\""
119  << Ptr.stringRef << "\"";
120  break;
121  case Twine::SmallStringKind:
122  OS << "smallstring:\"" << *Ptr.smallString << "\"";
123  break;
124  case Twine::CharKind:
125  OS << "char:\"" << Ptr.character << "\"";
126  break;
127  case Twine::DecUIKind:
128  OS << "decUI:\"" << Ptr.decUI << "\"";
129  break;
130  case Twine::DecIKind:
131  OS << "decI:\"" << Ptr.decI << "\"";
132  break;
133  case Twine::DecULKind:
134  OS << "decUL:\"" << *Ptr.decUL << "\"";
135  break;
136  case Twine::DecLKind:
137  OS << "decL:\"" << *Ptr.decL << "\"";
138  break;
139  case Twine::DecULLKind:
140  OS << "decULL:\"" << *Ptr.decULL << "\"";
141  break;
142  case Twine::DecLLKind:
143  OS << "decLL:\"" << *Ptr.decLL << "\"";
144  break;
145  case Twine::UHexKind:
146  OS << "uhex:\"" << Ptr.uHex << "\"";
147  break;
148  }
149 }
150 
151 void Twine::print(raw_ostream &OS) const {
152  printOneChild(OS, LHS, getLHSKind());
153  printOneChild(OS, RHS, getRHSKind());
154 }
155 
156 void Twine::printRepr(raw_ostream &OS) const {
157  OS << "(Twine ";
158  printOneChildRepr(OS, LHS, getLHSKind());
159  OS << " ";
160  printOneChildRepr(OS, RHS, getRHSKind());
161  OS << ")";
162 }
163 
164 void Twine::dump() const {
165  print(dbgs());
166 }
167 
168 void Twine::dumpRepr() const {
169  printRepr(dbgs());
170 }
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:26
void push_back(const T &Elt)
Definition: SmallVector.h:222
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:188
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
void dump() const
Dump the concatenated string represented by this twine to stderr.
Definition: Twine.cpp:164
void dumpRepr() const
Dump the representation of this twine to stderr.
Definition: Twine.cpp:168
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:16
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Definition: Twine.cpp:156
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:454
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
const ARM::ArchExtKind Kind
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:31
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
Definition: Twine.cpp:151
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40