diff options
author | Reid Spencer <rspencer@reidspencer.com> | 2006-05-30 18:15:07 +0000 |
---|---|---|
committer | Reid Spencer <rspencer@reidspencer.com> | 2006-05-30 18:15:07 +0000 |
commit | 82ebabafdeea9cbff0e47e4626e6e6f09afa801f (patch) | |
tree | 66f72ed4de888c21860d486309e373f62c7aff61 | |
parent | d12c97d23a45a7cb32c36017afb781ef4ee1d7c5 (diff) | |
download | bcm5719-llvm-82ebabafdeea9cbff0e47e4626e6e6f09afa801f.tar.gz bcm5719-llvm-82ebabafdeea9cbff0e47e4626e6e6f09afa801f.zip |
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.
llvm-svn: 28562
-rw-r--r-- | llvm/include/llvm/Constants.h | 12 | ||||
-rw-r--r-- | llvm/lib/VMCore/Constants.cpp | 12 |
2 files changed, 10 insertions, 14 deletions
diff --git a/llvm/include/llvm/Constants.h b/llvm/include/llvm/Constants.h index dc7e698cf6b..cce4d49d1fb 100644 --- a/llvm/include/llvm/Constants.h +++ b/llvm/include/llvm/Constants.h @@ -347,12 +347,12 @@ public: static Constant *get(const ArrayType *T, const std::vector<Constant*> &); /// This method constructs a ConstantArray and initializes it with a text - /// string. The default behavior (len==0) causes the null terminator to - /// be copied as well. However, in some situations this is not desired so - /// if len <= Initializer.length() (but not 0) then only that portion of - /// the string is copied and there is no null termination. If len > - /// than Initializer's length then the function asserts out (don't do that). - static Constant *get(const std::string &Initializer, unsigned len = 0); + /// string. The default behavior (AddNull==true) causes a null terminator to + /// be placed at the end of the array. This effectively increases the length + /// of the array by one (you've been warned). However, in some situations + /// this is not desired so if AddNull==false then the string is copied without + /// null termination. + static Constant *get(const std::string &Initializer, bool AddNull = true); /// getType - Specialize the getType() method to always return an ArrayType, /// which reduces the amount of casting needed in parts of the compiler. diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index d5f221f5c1f..3351385d0a9 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -930,21 +930,17 @@ void ConstantArray::destroyConstant() { /// Otherwise, the length parameter specifies how much of the string to use /// and it won't be null terminated. /// -Constant *ConstantArray::get(const std::string &Str, unsigned length) { - assert(length <= Str.length() && "Invalid length for string"); +Constant *ConstantArray::get(const std::string &Str, bool AddNull) { std::vector<Constant*> ElementVals; - - unsigned copy_len = (length == 0 ? Str.length() : length); - for (unsigned i = 0; i < copy_len; ++i) + for (unsigned i = 0; i < Str.length(); ++i) ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i])); // Add a null terminator to the string... - if (length == 0) { + if (AddNull) { 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); } |