LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 (isSingleStringRef())
33  return getSingleStringRef();
34  toVector(Out);
35  return StringRef(Out.data(), Out.size());
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::CharKind:
76  OS << Ptr.character;
77  break;
78  case Twine::DecUIKind:
79  OS << Ptr.decUI;
80  break;
81  case Twine::DecIKind:
82  OS << Ptr.decI;
83  break;
84  case Twine::DecULKind:
85  OS << *Ptr.decUL;
86  break;
87  case Twine::DecLKind:
88  OS << *Ptr.decL;
89  break;
90  case Twine::DecULLKind:
91  OS << *Ptr.decULL;
92  break;
93  case Twine::DecLLKind:
94  OS << *Ptr.decLL;
95  break;
96  case Twine::UHexKind:
97  OS.write_hex(*Ptr.uHex);
98  break;
99  }
100 }
101 
102 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
103  NodeKind Kind) const {
104  switch (Kind) {
105  case Twine::NullKind:
106  OS << "null"; break;
107  case Twine::EmptyKind:
108  OS << "empty"; break;
109  case Twine::TwineKind:
110  OS << "rope:";
111  Ptr.twine->printRepr(OS);
112  break;
113  case Twine::CStringKind:
114  OS << "cstring:\""
115  << Ptr.cString << "\"";
116  break;
117  case Twine::StdStringKind:
118  OS << "std::string:\""
119  << Ptr.stdString << "\"";
120  break;
121  case Twine::StringRefKind:
122  OS << "stringref:\""
123  << Ptr.stringRef << "\"";
124  break;
125  case Twine::CharKind:
126  OS << "char:\"" << Ptr.character << "\"";
127  break;
128  case Twine::DecUIKind:
129  OS << "decUI:\"" << Ptr.decUI << "\"";
130  break;
131  case Twine::DecIKind:
132  OS << "decI:\"" << Ptr.decI << "\"";
133  break;
134  case Twine::DecULKind:
135  OS << "decUL:\"" << *Ptr.decUL << "\"";
136  break;
137  case Twine::DecLKind:
138  OS << "decL:\"" << *Ptr.decL << "\"";
139  break;
140  case Twine::DecULLKind:
141  OS << "decULL:\"" << *Ptr.decULL << "\"";
142  break;
143  case Twine::DecLLKind:
144  OS << "decLL:\"" << *Ptr.decLL << "\"";
145  break;
146  case Twine::UHexKind:
147  OS << "uhex:\"" << Ptr.uHex << "\"";
148  break;
149  }
150 }
151 
152 void Twine::print(raw_ostream &OS) const {
153  printOneChild(OS, LHS, getLHSKind());
154  printOneChild(OS, RHS, getRHSKind());
155 }
156 
157 void Twine::printRepr(raw_ostream &OS) const {
158  OS << "(Twine ";
159  printOneChildRepr(OS, LHS, getLHSKind());
160  OS << " ";
161  printOneChildRepr(OS, RHS, getRHSKind());
162  OS << ")";
163 }
164 
165 void Twine::dump() const {
166  print(llvm::dbgs());
167 }
168 
169 void Twine::dumpRepr() const {
171 }
void toVector(SmallVectorImpl< char > &Out) const
Definition: Twine.cpp:26
void push_back(const T &Elt)
Definition: SmallVector.h:236
bool isSingleStringRef() const
Definition: Twine.h:393
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
void dump() const
Dump the concatenated string represented by this twine to stderr.
Definition: Twine.cpp:165
void dumpRepr() const
Dump the representation of this twine to stderr.
Definition: Twine.cpp:169
std::string str() const
str - Return the twine contents as a std::string.
Definition: Twine.cpp:16
raw_ostream & write_hex(unsigned long long N)
write_hex - Output N in hexadecimal, without any prefix or padding.
StringRef getSingleStringRef() const
Definition: Twine.h:426
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Definition: Twine.cpp:157
StringRef toStringRef(SmallVectorImpl< char > &Out) const
Definition: Twine.cpp:31
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
pointer data()
data - Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:135
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
Definition: Twine.cpp:38
void print(raw_ostream &OS) const
Definition: Twine.cpp:152