]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Support/Twine.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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   // If we're storing only a std::string, just return it.
18   if (LHSKind == StdStringKind && RHSKind == EmptyKind)
19     return *static_cast<const std::string*>(LHS);
20
21   // Otherwise, flatten and copy the contents first.
22   SmallString<256> Vec;
23   return toStringRef(Vec).str();
24 }
25
26 void Twine::toVector(SmallVectorImpl<char> &Out) const {
27   raw_svector_ostream OS(Out);
28   print(OS);
29 }
30
31 StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
32   if (isSingleStringRef())
33     return getSingleStringRef();
34   toVector(Out);
35   return StringRef(Out.data(), Out.size());
36 }
37
38 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
39   if (isUnary()) {
40     switch (getLHSKind()) {
41     case CStringKind:
42       // Already null terminated, yay!
43       return StringRef(static_cast<const char*>(LHS));
44     case StdStringKind: {
45       const std::string *str = static_cast<const std::string*>(LHS);
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, const void *Ptr,
59                           NodeKind Kind) const {
60   switch (Kind) {
61   case Twine::NullKind: break;
62   case Twine::EmptyKind: break;
63   case Twine::TwineKind:
64     static_cast<const Twine*>(Ptr)->print(OS);
65     break;
66   case Twine::CStringKind:
67     OS << static_cast<const char*>(Ptr);
68     break;
69   case Twine::StdStringKind:
70     OS << *static_cast<const std::string*>(Ptr);
71     break;
72   case Twine::StringRefKind:
73     OS << *static_cast<const StringRef*>(Ptr);
74     break;
75   case Twine::DecUIKind:
76     OS << (unsigned)(uintptr_t)Ptr;
77     break;
78   case Twine::DecIKind:
79     OS << (int)(intptr_t)Ptr;
80     break;
81   case Twine::DecULKind:
82     OS << *static_cast<const unsigned long*>(Ptr);
83     break;
84   case Twine::DecLKind:
85     OS << *static_cast<const long*>(Ptr);
86     break;
87   case Twine::DecULLKind:
88     OS << *static_cast<const unsigned long long*>(Ptr);
89     break;
90   case Twine::DecLLKind:
91     OS << *static_cast<const long long*>(Ptr);
92     break;
93   case Twine::UHexKind:
94     OS.write_hex(*static_cast<const uint64_t*>(Ptr));
95     break;
96   }
97 }
98
99 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
100                               NodeKind Kind) const {
101   switch (Kind) {
102   case Twine::NullKind:
103     OS << "null"; break;
104   case Twine::EmptyKind:
105     OS << "empty"; break;
106   case Twine::TwineKind:
107     OS << "rope:";
108     static_cast<const Twine*>(Ptr)->printRepr(OS);
109     break;
110   case Twine::CStringKind:
111     OS << "cstring:\""
112        << static_cast<const char*>(Ptr) << "\"";
113     break;
114   case Twine::StdStringKind:
115     OS << "std::string:\""
116        << static_cast<const std::string*>(Ptr) << "\"";
117     break;
118   case Twine::StringRefKind:
119     OS << "stringref:\""
120        << static_cast<const StringRef*>(Ptr) << "\"";
121     break;
122   case Twine::DecUIKind:
123     OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
124     break;
125   case Twine::DecIKind:
126     OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
127     break;
128   case Twine::DecULKind:
129     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
130     break;
131   case Twine::DecLKind:
132     OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
133     break;
134   case Twine::DecULLKind:
135     OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
136     break;
137   case Twine::DecLLKind:
138     OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
139     break;
140   case Twine::UHexKind:
141     OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
142     break;
143   }
144 }
145
146 void Twine::print(raw_ostream &OS) const {
147   printOneChild(OS, LHS, getLHSKind());
148   printOneChild(OS, RHS, getRHSKind());
149 }
150
151 void Twine::printRepr(raw_ostream &OS) const {
152   OS << "(Twine ";
153   printOneChildRepr(OS, LHS, getLHSKind());
154   OS << " ";
155   printOneChildRepr(OS, RHS, getRHSKind());
156   OS << ")";
157 }
158
159 void Twine::dump() const {
160   print(llvm::dbgs());
161 }
162
163 void Twine::dumpRepr() const {
164   printRepr(llvm::dbgs());
165 }