LLVM  14.0.0git
Twine.cpp
Go to the documentation of this file.
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"
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 
33  raw_svector_ostream OS(Out);
34  print(OS);
35 }
36 
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  case Twine::PtrAndLengthKind:
72  OS << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length);
73  break;
74  case Twine::FormatvObjectKind:
75  OS << *Ptr.formatvObject;
76  break;
77  case Twine::CharKind:
78  OS << Ptr.character;
79  break;
80  case Twine::DecUIKind:
81  OS << Ptr.decUI;
82  break;
83  case Twine::DecIKind:
84  OS << Ptr.decI;
85  break;
86  case Twine::DecULKind:
87  OS << *Ptr.decUL;
88  break;
89  case Twine::DecLKind:
90  OS << *Ptr.decL;
91  break;
92  case Twine::DecULLKind:
93  OS << *Ptr.decULL;
94  break;
95  case Twine::DecLLKind:
96  OS << *Ptr.decLL;
97  break;
98  case Twine::UHexKind:
99  OS.write_hex(*Ptr.uHex);
100  break;
101  }
102 }
103 
104 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
105  NodeKind Kind) const {
106  switch (Kind) {
107  case Twine::NullKind:
108  OS << "null"; break;
109  case Twine::EmptyKind:
110  OS << "empty"; break;
111  case Twine::TwineKind:
112  OS << "rope:";
113  Ptr.twine->printRepr(OS);
114  break;
115  case Twine::CStringKind:
116  OS << "cstring:\""
117  << Ptr.cString << "\"";
118  break;
119  case Twine::StdStringKind:
120  OS << "std::string:\""
121  << Ptr.stdString << "\"";
122  break;
123  case Twine::PtrAndLengthKind:
124  OS << "ptrAndLength:\""
125  << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length) << "\"";
126  break;
127  case Twine::FormatvObjectKind:
128  OS << "formatv:\"" << *Ptr.formatvObject << "\"";
129  break;
130  case Twine::CharKind:
131  OS << "char:\"" << Ptr.character << "\"";
132  break;
133  case Twine::DecUIKind:
134  OS << "decUI:\"" << Ptr.decUI << "\"";
135  break;
136  case Twine::DecIKind:
137  OS << "decI:\"" << Ptr.decI << "\"";
138  break;
139  case Twine::DecULKind:
140  OS << "decUL:\"" << *Ptr.decUL << "\"";
141  break;
142  case Twine::DecLKind:
143  OS << "decL:\"" << *Ptr.decL << "\"";
144  break;
145  case Twine::DecULLKind:
146  OS << "decULL:\"" << *Ptr.decULL << "\"";
147  break;
148  case Twine::DecLLKind:
149  OS << "decLL:\"" << *Ptr.decLL << "\"";
150  break;
151  case Twine::UHexKind:
152  OS << "uhex:\"" << Ptr.uHex << "\"";
153  break;
154  }
155 }
156 
157 void Twine::print(raw_ostream &OS) const {
158  printOneChild(OS, LHS, getLHSKind());
159  printOneChild(OS, RHS, getRHSKind());
160 }
161 
162 void Twine::printRepr(raw_ostream &OS) const {
163  OS << "(Twine ";
164  printOneChildRepr(OS, LHS, getLHSKind());
165  OS << " ";
166  printOneChildRepr(OS, RHS, getRHSKind());
167  OS << ")";
168 }
169 
170 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
172  print(dbgs());
173 }
174 
176  printRepr(dbgs());
177 }
178 #endif
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:491
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::Twine::printRepr
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Definition: Twine.cpp:162
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
SmallString.h
Twine.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
FormatVariadic.h
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:245
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::SmallString< 256 >
llvm::Twine::str
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
llvm::Twine::toVector
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
llvm::Twine::toNullTerminatedStringRef
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:37
llvm::Twine::dump
void dump() const
Dump the concatenated string represented by this twine to stderr.
Definition: Twine.cpp:171
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Twine::print
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
Definition: Twine.cpp:157
llvm::ms_demangle::NodeKind
NodeKind
Definition: MicrosoftDemangleNodes.h:226
llvm::Twine::dumpRepr
void dumpRepr() const
Dump the representation of this twine to stderr.
Definition: Twine.cpp:175
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:656
llvm::SmallVectorImpl< char >
llvm::Twine::toStringRef
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:477
raw_ostream.h
Debug.h
llvm::raw_ostream::write_hex
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
Definition: raw_ostream.cpp:140