diff options
author | Chris Lattner <sabre@nondot.org> | 2012-01-24 09:01:07 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2012-01-24 09:01:07 +0000 |
commit | 5dd4d87ce098e466f50d90e62c9b4379986c024f (patch) | |
tree | 6c957d76bc3fc0c5336232cb9ce749d3d766e888 /llvm/lib/VMCore/Constants.cpp | |
parent | 8e6fde457a89d6eb368276db26a00bcd9bfb4c55 (diff) | |
download | bcm5719-llvm-5dd4d87ce098e466f50d90e62c9b4379986c024f.tar.gz bcm5719-llvm-5dd4d87ce098e466f50d90e62c9b4379986c024f.zip |
Add various "string" methods to ConstantDataSequential, which have the
same semantics as ConstantArray's but much more efficient because they
don't have to return std::string's. The ConstantArray methods will
eventually be removed.
llvm-svn: 148792
Diffstat (limited to 'llvm/lib/VMCore/Constants.cpp')
-rw-r--r-- | llvm/lib/VMCore/Constants.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 55b97ef7062..0525882828e 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -2221,7 +2221,34 @@ Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const { return ConstantInt::get(getElementType(), getElementAsInteger(Elt)); } +/// isString - This method returns true if this is an array of i8. +bool ConstantDataSequential::isString() const { + return isa<ArrayType>(getType()) && getElementType()->isIntegerTy(8); +} + +/// getAsString - If this array is isString(), then this method returns the +/// array as a StringRef. Otherwise, it asserts out. +/// +StringRef ConstantDataSequential::getAsString() const { + assert(isString() && "Not a string"); + return StringRef(DataElements, getType()->getNumElements()); +} + +/// isCString - This method returns true if the array "isString", ends with a +/// nul byte, and does not contains any other nul bytes. +bool ConstantDataSequential::isCString() const { + if (!isString()) + return false; + + StringRef Str = getAsString(); + + // The last value must be nul. + if (Str.back() != 0) return false; + + // Other elements must be non-nul. + return Str.drop_back().find(0) == StringRef::npos; +} //===----------------------------------------------------------------------===// |