1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
16 std::string Twine::str() const {
18 return toStringRef(Vec).str();
21 void Twine::toVector(SmallVectorImpl<char> &Out) const {
22 raw_svector_ostream OS(Out);
26 StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
27 if (isSingleStringRef())
28 return getSingleStringRef();
30 return StringRef(Out.data(), Out.size());
33 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
35 switch (getLHSKind()) {
37 // Already null terminated, yay!
38 return StringRef(static_cast<const char*>(LHS));
40 const std::string *str = static_cast<const std::string*>(LHS);
41 return StringRef(str->c_str(), str->size());
50 return StringRef(Out.data(), Out.size());
53 void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
54 NodeKind Kind) const {
56 case Twine::NullKind: break;
57 case Twine::EmptyKind: break;
58 case Twine::TwineKind:
59 static_cast<const Twine*>(Ptr)->print(OS);
61 case Twine::CStringKind:
62 OS << static_cast<const char*>(Ptr);
64 case Twine::StdStringKind:
65 OS << *static_cast<const std::string*>(Ptr);
67 case Twine::StringRefKind:
68 OS << *static_cast<const StringRef*>(Ptr);
70 case Twine::DecUIKind:
71 OS << (unsigned)(uintptr_t)Ptr;
74 OS << (int)(intptr_t)Ptr;
76 case Twine::DecULKind:
77 OS << *static_cast<const unsigned long*>(Ptr);
80 OS << *static_cast<const long*>(Ptr);
82 case Twine::DecULLKind:
83 OS << *static_cast<const unsigned long long*>(Ptr);
85 case Twine::DecLLKind:
86 OS << *static_cast<const long long*>(Ptr);
89 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
94 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
95 NodeKind Kind) const {
99 case Twine::EmptyKind:
100 OS << "empty"; break;
101 case Twine::TwineKind:
103 static_cast<const Twine*>(Ptr)->printRepr(OS);
105 case Twine::CStringKind:
107 << static_cast<const char*>(Ptr) << "\"";
109 case Twine::StdStringKind:
110 OS << "std::string:\""
111 << static_cast<const std::string*>(Ptr) << "\"";
113 case Twine::StringRefKind:
115 << static_cast<const StringRef*>(Ptr) << "\"";
117 case Twine::DecUIKind:
118 OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
120 case Twine::DecIKind:
121 OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
123 case Twine::DecULKind:
124 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
126 case Twine::DecLKind:
127 OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
129 case Twine::DecULLKind:
130 OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
132 case Twine::DecLLKind:
133 OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
135 case Twine::UHexKind:
136 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
141 void Twine::print(raw_ostream &OS) const {
142 printOneChild(OS, LHS, getLHSKind());
143 printOneChild(OS, RHS, getRHSKind());
146 void Twine::printRepr(raw_ostream &OS) const {
148 printOneChildRepr(OS, LHS, getLHSKind());
150 printOneChildRepr(OS, RHS, getRHSKind());
154 void Twine::dump() const {
158 void Twine::dumpRepr() const {
159 printRepr(llvm::dbgs());