| Daniel Dunbar | 2538f7a | 2009-07-24 07:04:27 +0000 | [diff] [blame] | 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/Support/raw_ostream.h" | 
|  | 12 | using namespace llvm; | 
|  | 13 |  | 
|  | 14 | std::string Twine::str() const { | 
|  | 15 | std::string Res; | 
|  | 16 | raw_string_ostream OS(Res); | 
|  | 17 | print(OS); | 
|  | 18 | return Res; | 
|  | 19 | } | 
|  | 20 |  | 
|  | 21 | void Twine::toVector(SmallVectorImpl<char> &Out) const { | 
|  | 22 | raw_svector_ostream OS(Out); | 
|  | 23 | print(OS); | 
|  | 24 | } | 
|  | 25 |  | 
|  | 26 | void Twine::printOneChild(raw_ostream &OS, const void *Ptr, | 
|  | 27 | NodeKind Kind) const { | 
|  | 28 | switch (Kind) { | 
|  | 29 | case Twine::NullKind: break; | 
|  | 30 | case Twine::EmptyKind: break; | 
|  | 31 | case Twine::CStringKind: | 
|  | 32 | OS << static_cast<const char*>(Ptr); | 
|  | 33 | break; | 
|  | 34 | case Twine::StdStringKind: | 
|  | 35 | OS << *static_cast<const std::string*>(Ptr); | 
|  | 36 | break; | 
|  | 37 | case Twine::StringRefKind: | 
|  | 38 | OS << *static_cast<const StringRef*>(Ptr); | 
|  | 39 | break; | 
|  | 40 | case Twine::TwineKind: | 
|  | 41 | static_cast<const Twine*>(Ptr)->print(OS); | 
|  | 42 | break; | 
|  | 43 | } | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, | 
|  | 47 | NodeKind Kind) const { | 
|  | 48 | switch (Kind) { | 
|  | 49 | case Twine::NullKind: | 
|  | 50 | OS << "null"; break; | 
|  | 51 | case Twine::EmptyKind: | 
|  | 52 | OS << "empty"; break; | 
|  | 53 | case Twine::CStringKind: | 
|  | 54 | OS << "cstring:\"" | 
|  | 55 | << static_cast<const char*>(Ptr) << "\""; | 
|  | 56 | break; | 
|  | 57 | case Twine::StdStringKind: | 
|  | 58 | OS << "std::string:\"" | 
|  | 59 | << *static_cast<const std::string*>(Ptr) << "\""; | 
|  | 60 | break; | 
|  | 61 | case Twine::StringRefKind: | 
|  | 62 | OS << "stringref:\"" | 
|  | 63 | << *static_cast<const StringRef*>(Ptr) << "\""; | 
|  | 64 | break; | 
|  | 65 | case Twine::TwineKind: | 
|  | 66 | OS << "rope:"; | 
|  | 67 | static_cast<const Twine*>(Ptr)->printRepr(OS); | 
|  | 68 | break; | 
|  | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | void Twine::print(raw_ostream &OS) const { | 
|  | 73 | printOneChild(OS, LHS, getLHSKind()); | 
|  | 74 | printOneChild(OS, RHS, getRHSKind()); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void Twine::printRepr(raw_ostream &OS) const { | 
|  | 78 | OS << "(Twine "; | 
|  | 79 | printOneChildRepr(OS, LHS, getLHSKind()); | 
|  | 80 | OS << " "; | 
|  | 81 | printOneChildRepr(OS, RHS, getRHSKind()); | 
|  | 82 | OS << ")"; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | void Twine::dump() const { | 
|  | 86 | print(llvm::errs()); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | void Twine::dumpRepr() const { | 
|  | 90 | printRepr(llvm::errs()); | 
|  | 91 | } |