2
0

Twine.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include "llvm/ADT/Twine.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. using namespace llvm;
  14. std::string Twine::str() const {
  15. // If we're storing only a std::string, just return it.
  16. if (LHSKind == StdStringKind && RHSKind == EmptyKind)
  17. return *LHS.stdString;
  18. // Otherwise, flatten and copy the contents first.
  19. SmallString<256> Vec;
  20. return toStringRef(Vec).str();
  21. }
  22. void Twine::toVector(SmallVectorImpl<char> &Out) const {
  23. raw_svector_ostream OS(Out);
  24. print(OS);
  25. }
  26. StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
  27. if (isUnary()) {
  28. switch (getLHSKind()) {
  29. case CStringKind:
  30. // Already null terminated, yay!
  31. return StringRef(LHS.cString);
  32. case StdStringKind: {
  33. const std::string *str = LHS.stdString;
  34. return StringRef(str->c_str(), str->size());
  35. }
  36. default:
  37. break;
  38. }
  39. }
  40. toVector(Out);
  41. Out.push_back(0);
  42. Out.pop_back();
  43. return StringRef(Out.data(), Out.size());
  44. }
  45. void Twine::printOneChild(raw_ostream &OS, Child Ptr,
  46. NodeKind Kind) const {
  47. switch (Kind) {
  48. case Twine::NullKind: break;
  49. case Twine::EmptyKind: break;
  50. case Twine::TwineKind:
  51. Ptr.twine->print(OS);
  52. break;
  53. case Twine::CStringKind:
  54. OS << Ptr.cString;
  55. break;
  56. case Twine::StdStringKind:
  57. OS << *Ptr.stdString;
  58. break;
  59. case Twine::StringRefKind:
  60. OS << *Ptr.stringRef;
  61. break;
  62. case Twine::SmallStringKind:
  63. OS << *Ptr.smallString;
  64. break;
  65. case Twine::CharKind:
  66. OS << Ptr.character;
  67. break;
  68. case Twine::DecUIKind:
  69. OS << Ptr.decUI;
  70. break;
  71. case Twine::DecIKind:
  72. OS << Ptr.decI;
  73. break;
  74. case Twine::DecULKind:
  75. OS << *Ptr.decUL;
  76. break;
  77. case Twine::DecLKind:
  78. OS << *Ptr.decL;
  79. break;
  80. case Twine::DecULLKind:
  81. OS << *Ptr.decULL;
  82. break;
  83. case Twine::DecLLKind:
  84. OS << *Ptr.decLL;
  85. break;
  86. case Twine::UHexKind:
  87. OS.write_hex(*Ptr.uHex);
  88. break;
  89. }
  90. }
  91. void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
  92. NodeKind Kind) const {
  93. switch (Kind) {
  94. case Twine::NullKind:
  95. OS << "null"; break;
  96. case Twine::EmptyKind:
  97. OS << "empty"; break;
  98. case Twine::TwineKind:
  99. OS << "rope:";
  100. Ptr.twine->printRepr(OS);
  101. break;
  102. case Twine::CStringKind:
  103. OS << "cstring:\""
  104. << Ptr.cString << "\"";
  105. break;
  106. case Twine::StdStringKind:
  107. OS << "std::string:\""
  108. << Ptr.stdString << "\"";
  109. break;
  110. case Twine::StringRefKind:
  111. OS << "stringref:\""
  112. << Ptr.stringRef << "\"";
  113. break;
  114. case Twine::SmallStringKind:
  115. OS << "smallstring:\"" << *Ptr.smallString << "\"";
  116. break;
  117. case Twine::CharKind:
  118. OS << "char:\"" << Ptr.character << "\"";
  119. break;
  120. case Twine::DecUIKind:
  121. OS << "decUI:\"" << Ptr.decUI << "\"";
  122. break;
  123. case Twine::DecIKind:
  124. OS << "decI:\"" << Ptr.decI << "\"";
  125. break;
  126. case Twine::DecULKind:
  127. OS << "decUL:\"" << *Ptr.decUL << "\"";
  128. break;
  129. case Twine::DecLKind:
  130. OS << "decL:\"" << *Ptr.decL << "\"";
  131. break;
  132. case Twine::DecULLKind:
  133. OS << "decULL:\"" << *Ptr.decULL << "\"";
  134. break;
  135. case Twine::DecLLKind:
  136. OS << "decLL:\"" << *Ptr.decLL << "\"";
  137. break;
  138. case Twine::UHexKind:
  139. OS << "uhex:\"" << Ptr.uHex << "\"";
  140. break;
  141. }
  142. }
  143. void Twine::print(raw_ostream &OS) const {
  144. printOneChild(OS, LHS, getLHSKind());
  145. printOneChild(OS, RHS, getRHSKind());
  146. }
  147. void Twine::printRepr(raw_ostream &OS) const {
  148. OS << "(Twine ";
  149. printOneChildRepr(OS, LHS, getLHSKind());
  150. OS << " ";
  151. printOneChildRepr(OS, RHS, getRHSKind());
  152. OS << ")";
  153. }
  154. void Twine::dump() const {
  155. print(dbgs());
  156. }
  157. void Twine::dumpRepr() const {
  158. printRepr(dbgs());
  159. }