From 22c7030a0c535ed48d8465994f8f8aaf8fa76813 Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Thu, 26 Oct 2006 19:15:05 +0000 Subject: [PATCH] Add isCString() - returns true if a ConstantArray is a CString. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31201 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Constants.h | 5 +++++ lib/VMCore/Constants.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 3bf935bb5b0..f5fff4d7fb4 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -334,6 +334,11 @@ public: /// ubyte, and if the elements of the array are all ConstantInt's. bool isString() const; + /// isCString - This method returns true if the array is a string (see + /// isString) and it ends in a null byte \0 and does not contains any other + /// null bytes except its terminator. + bool isCString() const; + /// getAsString - If this array is isString(), then this method converts the /// array to an std::string and returns it. Otherwise, it asserts out. /// diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 2c996f5301f..ce29a21f86c 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -1073,6 +1073,19 @@ bool ConstantArray::isString() const { return true; } +/// isCString - This method returns true if the array is a string (see +/// isString) and it ends in a null byte \0 and does not contains any other +/// null bytes except its terminator. +bool ConstantArray::isCString() const { + if (!isString()) return false; + // This is safe because a ConstantArray cannot be a zero array. + for (unsigned i = 0, e = getNumOperands()-1; i != e; ++i) + if (cast(getOperand(i))->getZExtValue() == 0) + return false; + return true; +} + + // getAsString - If the sub-element type of this array is either sbyte or ubyte, // then this method converts the array to an std::string and returns it. // Otherwise, it asserts out.