Provide a simpler interface for getting a ConstantArray from a character

string. Instead of specifying the length, just specify whether the user
wants a terminating null or not. The default is "true" to retain the same
behavior as previously provided by this function.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28562 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2006-05-30 18:15:07 +00:00
parent 6b8e5a9318
commit 461bed2b75
2 changed files with 10 additions and 14 deletions

View File

@ -347,12 +347,12 @@ public:
static Constant *get(const ArrayType *T, const std::vector<Constant*> &); static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
/// This method constructs a ConstantArray and initializes it with a text /// This method constructs a ConstantArray and initializes it with a text
/// string. The default behavior (len==0) causes the null terminator to /// string. The default behavior (AddNull==true) causes a null terminator to
/// be copied as well. However, in some situations this is not desired so /// be placed at the end of the array. This effectively increases the length
/// if len <= Initializer.length() (but not 0) then only that portion of /// of the array by one (you've been warned). However, in some situations
/// the string is copied and there is no null termination. If len > /// this is not desired so if AddNull==false then the string is copied without
/// than Initializer's length then the function asserts out (don't do that). /// null termination.
static Constant *get(const std::string &Initializer, unsigned len = 0); static Constant *get(const std::string &Initializer, bool AddNull = true);
/// getType - Specialize the getType() method to always return an ArrayType, /// getType - Specialize the getType() method to always return an ArrayType,
/// which reduces the amount of casting needed in parts of the compiler. /// which reduces the amount of casting needed in parts of the compiler.

View File

@ -930,21 +930,17 @@ void ConstantArray::destroyConstant() {
/// Otherwise, the length parameter specifies how much of the string to use /// Otherwise, the length parameter specifies how much of the string to use
/// and it won't be null terminated. /// and it won't be null terminated.
/// ///
Constant *ConstantArray::get(const std::string &Str, unsigned length) { Constant *ConstantArray::get(const std::string &Str, bool AddNull) {
assert(length <= Str.length() && "Invalid length for string");
std::vector<Constant*> ElementVals; std::vector<Constant*> ElementVals;
for (unsigned i = 0; i < Str.length(); ++i)
unsigned copy_len = (length == 0 ? Str.length() : length);
for (unsigned i = 0; i < copy_len; ++i)
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
// Add a null terminator to the string... // Add a null terminator to the string...
if (length == 0) { if (AddNull) {
ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0)); ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
copy_len++;
} }
ArrayType *ATy = ArrayType::get(Type::SByteTy, copy_len); ArrayType *ATy = ArrayType::get(Type::SByteTy, ElementVals.size());
return ConstantArray::get(ATy, ElementVals); return ConstantArray::get(ATy, ElementVals);
} }