llvm.org GIT mirror llvm / release_32 lib / Support / Twine.cpp
release_32

Tree @release_32 (Download .tar.gz)

Twine.cpp @release_32

2538f7a
 
 
 
 
 
 
 
 
 
b7be0e8
2b965b0
2538f7a
 
 
 
331dbca
 
3f25ee0
331dbca
 
b7be0e8
b357e06
2538f7a
 
 
 
 
 
 
b357e06
 
 
 
 
 
 
7dc7ac3
0dda543
 
 
 
3f25ee0
0dda543
3f25ee0
331dbca
 
0dda543
 
 
7dc7ac3
 
 
 
 
 
 
3f25ee0
2538f7a
 
 
 
763457e
3f25ee0
763457e
326990f
3f25ee0
2538f7a
 
3f25ee0
2538f7a
 
3f25ee0
 
 
 
2538f7a
2d8bc0f
3f25ee0
0165a2c
2d8bc0f
3f25ee0
0165a2c
2d8bc0f
3f25ee0
763457e
2d8bc0f
3f25ee0
2d8bc0f
 
3f25ee0
2d8bc0f
 
3f25ee0
763457e
 
3f25ee0
2538f7a
 
 
 
3f25ee0
2538f7a
 
 
 
 
 
763457e
 
3f25ee0
763457e
2538f7a
763457e
3f25ee0
2538f7a
 
763457e
3f25ee0
2538f7a
 
763457e
3f25ee0
 
 
 
2538f7a
2d8bc0f
3f25ee0
2d8bc0f
 
3f25ee0
2d8bc0f
 
3f25ee0
0165a2c
2d8bc0f
3f25ee0
0165a2c
2d8bc0f
3f25ee0
763457e
2d8bc0f
3f25ee0
763457e
 
3f25ee0
2538f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b965b0
2538f7a
 
 
2b965b0
2538f7a
//===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;

std::string Twine::str() const {
  // If we're storing only a std::string, just return it.
  if (LHSKind == StdStringKind && RHSKind == EmptyKind)
    return *LHS.stdString;

  // Otherwise, flatten and copy the contents first.
  SmallString<256> Vec;
  return toStringRef(Vec).str();
}

void Twine::toVector(SmallVectorImpl<char> &Out) const {
  raw_svector_ostream OS(Out);
  print(OS);
}

StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
  if (isSingleStringRef())
    return getSingleStringRef();
  toVector(Out);
  return StringRef(Out.data(), Out.size());
}

StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
  if (isUnary()) {
    switch (getLHSKind()) {
    case CStringKind:
      // Already null terminated, yay!
      return StringRef(LHS.cString);
    case StdStringKind: {
      const std::string *str = LHS.stdString;
      return StringRef(str->c_str(), str->size());
    }
    default:
      break;
    }
  }
  toVector(Out);
  Out.push_back(0);
  Out.pop_back();
  return StringRef(Out.data(), Out.size());
}

void Twine::printOneChild(raw_ostream &OS, Child Ptr,
                          NodeKind Kind) const {
  switch (Kind) {
  case Twine::NullKind: break;
  case Twine::EmptyKind: break;
  case Twine::TwineKind:
    Ptr.twine->print(OS);
    break;
  case Twine::CStringKind:
    OS << Ptr.cString;
    break;
  case Twine::StdStringKind:
    OS << *Ptr.stdString;
    break;
  case Twine::StringRefKind:
    OS << *Ptr.stringRef;
    break;
  case Twine::CharKind:
    OS << Ptr.character;
    break;
  case Twine::DecUIKind:
    OS << Ptr.decUI;
    break;
  case Twine::DecIKind:
    OS << Ptr.decI;
    break;
  case Twine::DecULKind:
    OS << *Ptr.decUL;
    break;
  case Twine::DecLKind:
    OS << *Ptr.decL;
    break;
  case Twine::DecULLKind:
    OS << *Ptr.decULL;
    break;
  case Twine::DecLLKind:
    OS << *Ptr.decLL;
    break;
  case Twine::UHexKind:
    OS.write_hex(*Ptr.uHex);
    break;
  }
}

void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
                              NodeKind Kind) const {
  switch (Kind) {
  case Twine::NullKind:
    OS << "null"; break;
  case Twine::EmptyKind:
    OS << "empty"; break;
  case Twine::TwineKind:
    OS << "rope:";
    Ptr.twine->printRepr(OS);
    break;
  case Twine::CStringKind:
    OS << "cstring:\""
       << Ptr.cString << "\"";
    break;
  case Twine::StdStringKind:
    OS << "std::string:\""
       << Ptr.stdString << "\"";
    break;
  case Twine::StringRefKind:
    OS << "stringref:\""
       << Ptr.stringRef << "\"";
    break;
  case Twine::CharKind:
    OS << "char:\"" << Ptr.character << "\"";
    break;
  case Twine::DecUIKind:
    OS << "decUI:\"" << Ptr.decUI << "\"";
    break;
  case Twine::DecIKind:
    OS << "decI:\"" << Ptr.decI << "\"";
    break;
  case Twine::DecULKind:
    OS << "decUL:\"" << *Ptr.decUL << "\"";
    break;
  case Twine::DecLKind:
    OS << "decL:\"" << *Ptr.decL << "\"";
    break;
  case Twine::DecULLKind:
    OS << "decULL:\"" << *Ptr.decULL << "\"";
    break;
  case Twine::DecLLKind:
    OS << "decLL:\"" << *Ptr.decLL << "\"";
    break;
  case Twine::UHexKind:
    OS << "uhex:\"" << Ptr.uHex << "\"";
    break;
  }
}

void Twine::print(raw_ostream &OS) const {
  printOneChild(OS, LHS, getLHSKind());
  printOneChild(OS, RHS, getRHSKind());
}

void Twine::printRepr(raw_ostream &OS) const {
  OS << "(Twine ";
  printOneChildRepr(OS, LHS, getLHSKind());
  OS << " ";
  printOneChildRepr(OS, RHS, getRHSKind());
  OS << ")";
}

void Twine::dump() const {
  print(llvm::dbgs());
}

void Twine::dumpRepr() const {
  printRepr(llvm::dbgs());
}