aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/ADT/Twine.h
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-01-13 07:12:06 +0000
committerChris Lattner <sabre@nondot.org>2010-01-13 07:12:06 +0000
commitbf86e5df180139310bf2f0d71bef58e208dce31d (patch)
treef36f04487db30ccf5c65a3def34030974a43f46f /include/llvm/ADT/Twine.h
parent0e7ab8cb07716305894fabcc512b8d5d0318bd4d (diff)
downloadexternal_llvm-bf86e5df180139310bf2f0d71bef58e208dce31d.zip
external_llvm-bf86e5df180139310bf2f0d71bef58e208dce31d.tar.gz
external_llvm-bf86e5df180139310bf2f0d71bef58e208dce31d.tar.bz2
add new isSingleStringRef()/getSingleStringRef() methods to twine,
and use them to avoid a copy of a string in getNameWithPrefix in the common case. It seems like Value::setName and other places should use this as well? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93301 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT/Twine.h')
-rw-r--r--include/llvm/ADT/Twine.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/include/llvm/ADT/Twine.h b/include/llvm/ADT/Twine.h
index ca0be53..29490b9 100644
--- a/include/llvm/ADT/Twine.h
+++ b/include/llvm/ADT/Twine.h
@@ -329,6 +329,22 @@ namespace llvm {
bool isTriviallyEmpty() const {
return isNullary();
}
+
+ /// isSingleStringRef - Return true if this twine can be dynamically
+ /// accessed as a single StringRef value with getSingleStringRef().
+ bool isSingleStringRef() const {
+ if (getRHSKind() != EmptyKind) return false;
+
+ switch (getLHSKind()) {
+ case EmptyKind:
+ case CStringKind:
+ case StdStringKind:
+ case StringRefKind:
+ return true;
+ default:
+ return false;
+ }
+ }
/// @}
/// @name String Operations
@@ -347,6 +363,20 @@ namespace llvm {
/// SmallVector.
void toVector(SmallVectorImpl<char> &Out) const;
+ /// getSingleStringRef - This returns the twine as a single StringRef. This
+ /// method is only valid if isSingleStringRef() is true.
+ StringRef getSingleStringRef() const {
+ assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
+ switch (getLHSKind()) {
+ default: assert(0 && "Out of sync with isSingleStringRef");
+ case EmptyKind: return StringRef();
+ case CStringKind: return StringRef((const char*)LHS);
+ case StdStringKind: return StringRef(*(const std::string*)LHS);
+ case StringRefKind: return *(const StringRef*)LHS;
+ }
+ }
+
+
/// print - Write the concatenated string represented by this twine to the
/// stream \arg OS.
void print(raw_ostream &OS) const;