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 {
34 if (isSingleStringRef()) {
35 StringRef sr = getSingleStringRef();
36 if (*(sr.begin() + sr.size()) == 0)
42 return StringRef(Out.data(), Out.size());
45 void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
46 NodeKind Kind) const {
48 case Twine::NullKind: break;
49 case Twine::EmptyKind: break;
50 case Twine::TwineKind:
51 static_cast<const Twine*>(Ptr)->print(OS);
53 case Twine::CStringKind:
54 OS << static_cast<const char*>(Ptr);
56 case Twine::StdStringKind:
57 OS << *static_cast<const std::string*>(Ptr);
59 case Twine::StringRefKind:
60 OS << *static_cast<const StringRef*>(Ptr);
62 case Twine::DecUIKind:
63 OS << (unsigned)(uintptr_t)Ptr;
66 OS << (int)(intptr_t)Ptr;
68 case Twine::DecULKind:
69 OS << *static_cast<const unsigned long*>(Ptr);
72 OS << *static_cast<const long*>(Ptr);
74 case Twine::DecULLKind:
75 OS << *static_cast<const unsigned long long*>(Ptr);
77 case Twine::DecLLKind:
78 OS << *static_cast<const long long*>(Ptr);
81 OS.write_hex(*static_cast<const uint64_t*>(Ptr));
86 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
87 NodeKind Kind) const {
91 case Twine::EmptyKind:
93 case Twine::TwineKind:
95 static_cast<const Twine*>(Ptr)->printRepr(OS);
97 case Twine::CStringKind:
99 << static_cast<const char*>(Ptr) << "\"";
101 case Twine::StdStringKind:
102 OS << "std::string:\""
103 << static_cast<const std::string*>(Ptr) << "\"";
105 case Twine::StringRefKind:
107 << static_cast<const StringRef*>(Ptr) << "\"";
109 case Twine::DecUIKind:
110 OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
112 case Twine::DecIKind:
113 OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
115 case Twine::DecULKind:
116 OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
118 case Twine::DecLKind:
119 OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
121 case Twine::DecULLKind:
122 OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
124 case Twine::DecLLKind:
125 OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
127 case Twine::UHexKind:
128 OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
133 void Twine::print(raw_ostream &OS) const {
134 printOneChild(OS, LHS, getLHSKind());
135 printOneChild(OS, RHS, getRHSKind());
138 void Twine::printRepr(raw_ostream &OS) const {
140 printOneChildRepr(OS, LHS, getLHSKind());
142 printOneChildRepr(OS, RHS, getRHSKind());
146 void Twine::dump() const {
150 void Twine::dumpRepr() const {
151 printRepr(llvm::dbgs());