4f6f479a7eae910f13cdd12b80c622b0204b7221
[oota-llvm.git] / lib / Support / Twine.cpp
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"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 std::string Twine::str() const {
17   SmallString<256> Vec;
18   return toStringRef(Vec).str();
19 }
20
21 void Twine::toVector(SmallVectorImpl<char> &Out) const {
22   raw_svector_ostream OS(Out);
23   print(OS);
24 }
25
26 StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
27   if (isSingleStringRef())
28     return getSingleStringRef();
29   toVector(Out);
30   return StringRef(Out.data(), Out.size());
31 }
32
33 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
34   if (isSingleStringRef()) {
35     StringRef sr = getSingleStringRef();
36     if (*(sr.begin() + sr.size()) == 0)
37       return sr;
38   }
39   toVector(Out);
40   Out.push_back(0);
41   Out.pop_back();
42   return StringRef(Out.data(), Out.size());
43 }
44
45 void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
46                           NodeKind Kind) const {
47   switch (Kind) {
48   case Twine::NullKind: break;
49   case Twine::EmptyKind: break;
50   case Twine::TwineKind:
51     static_cast<const Twine*>(Ptr)->print(OS);
52     break;
53   case Twine::CStringKind:
54     OS << static_cast<const char*>(Ptr);
55     break;
56   case Twine::StdStringKind:
57     OS << *static_cast<const std::string*>(Ptr);
58     break;
59   case Twine::StringRefKind:
60     OS << *static_cast<const StringRef*>(Ptr);
61     break;
62   case Twine::DecUIKind:
63     OS << (unsigned)(uintptr_t)Ptr;
64     break;
65   case Twine::DecIKind:
66     OS << (int)(intptr_t)Ptr;
67     break;
68   case Twine::DecULKind:
69     OS << *static_cast<const unsigned long*>(Ptr);
70     break;
71   case Twine::DecLKind:
72     OS << *static_cast<const long*>(Ptr);
73     break;
74   case Twine::DecULLKind:
75     OS << *static_cast<const unsigned long long*>(Ptr);
76     break;
77   case Twine::DecLLKind:
78     OS << *static_cast<const long long*>(Ptr);
79     break;
80   case Twine::UHexKind:
81     OS.write_hex(*static_cast<const uint64_t*>(Ptr));
82     break;
83   }
84 }
85
86 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
87                               NodeKind Kind) const {
88   switch (Kind) {
89   case Twine::NullKind:
90     OS << "null"; break;
91   case Twine::EmptyKind:
92     OS << "empty"; break;
93   case Twine::TwineKind:
94     OS << "rope:";
95     static_cast<const Twine*>(Ptr)->printRepr(OS);
96     break;
97   case Twine::CStringKind:
98     OS << "cstring:\""
99        << static_cast<const char*>(Ptr) << "\"";
100     break;
101   case Twine::StdStringKind:
102     OS << "std::string:\""
103        << static_cast<const std::string*>(Ptr) << "\"";
104     break;
105   case Twine::StringRefKind:
106     OS << "stringref:\""
107        << static_cast<const StringRef*>(Ptr) << "\"";
108     break;
109   case Twine::DecUIKind:
110     OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
111     break;
112   case Twine::DecIKind:
113     OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
114     break;
115   case Twine::DecULKind:
116     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
117     break;
118   case Twine::DecLKind:
119     OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
120     break;
121   case Twine::DecULLKind:
122     OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
123     break;
124   case Twine::DecLLKind:
125     OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
126     break;
127   case Twine::UHexKind:
128     OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
129     break;
130   }
131 }
132
133 void Twine::print(raw_ostream &OS) const {
134   printOneChild(OS, LHS, getLHSKind());
135   printOneChild(OS, RHS, getRHSKind());
136 }
137
138 void Twine::printRepr(raw_ostream &OS) const {
139   OS << "(Twine ";
140   printOneChildRepr(OS, LHS, getLHSKind());
141   OS << " ";
142   printOneChildRepr(OS, RHS, getRHSKind());
143   OS << ")";
144 }
145
146 void Twine::dump() const {
147   print(llvm::dbgs());
148 }
149
150 void Twine::dumpRepr() const {
151   printRepr(llvm::dbgs());
152 }