1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00
llvm-mirror/lib/Support/Twine.cpp
Sterling Augustine 59049d1a79 Add Twine support for std::string_view.
With Twine now ubiquitous after rG92a79dbe91413f685ab19295fc7a6297dbd6c824,
it needs support for string_view when building clang with newer C++ standards.

This is similar to how StringRef is handled.

Differential Revision: https://reviews.llvm.org/D103935
2021-06-08 20:19:04 -07:00

195 lines
4.9 KiB
C++

//===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormatVariadic.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;
// If we're storing a formatv_object, we can avoid an extra copy by formatting
// it immediately and returning the result.
if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
return LHS.formatvObject->str();
// 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::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;
#if __cplusplus > 201402L
case StdStringViewKind:
OS << StringRef(*Ptr.stdStringView);
break;
#endif
case Twine::StringRefKind:
OS << *Ptr.stringRef;
break;
case Twine::SmallStringKind:
OS << *Ptr.smallString;
break;
case Twine::FormatvObjectKind:
OS << *Ptr.formatvObject;
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;
#if __cplusplus > 201402L
case Twine::StdStringViewKind:
OS << "std::string_view:\"" << StringRef(*Ptr.stdStringView) << "\"";
break;
#endif
case Twine::StringRefKind:
OS << "stringref:\""
<< Ptr.stringRef << "\"";
break;
case Twine::SmallStringKind:
OS << "smallstring:\"" << *Ptr.smallString << "\"";
break;
case Twine::FormatvObjectKind:
OS << "formatv:\"" << *Ptr.formatvObject << "\"";
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 << ")";
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void Twine::dump() const {
print(dbgs());
}
LLVM_DUMP_METHOD void Twine::dumpRepr() const {
printRepr(dbgs());
}
#endif