Adjust the interface to ConstantArray::get. The previous
authorReid Spencer <rspencer@reidspencer.com>
Tue, 30 May 2006 08:23:18 +0000 (08:23 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Tue, 30 May 2006 08:23:18 +0000 (08:23 +0000)
implementation always added a null byte to the end of the string. It turns
out that this is not always wanted. By adding a length parameter we preserve
this behavior when length==0 (default value) but also allow other lengths
(not null terminated) to be created.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@28552 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
lib/VMCore/Constants.cpp

index d555d3ef1731853bb7906d1ec5e37a2f189fc155..f0f8146c75b2e57fb2ef7619ac35c8ba3de0883b 100644 (file)
@@ -345,7 +345,7 @@ protected:
 public:
   /// get() - Static factory methods - Return objects of the specified value
   static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
-  static Constant *get(const std::string &Initializer);
+  static Constant *get(const std::string &Initializer, unsigned len = 0);
 
   /// getType - Specialize the getType() method to always return an ArrayType,
   /// which reduces the amount of casting needed in parts of the compiler.
index d6e524e54ea19c4535b6d00124a0323606d44a84..d5f221f5c1f04da50581f991d62d103c4414dfe7 100644 (file)
@@ -924,20 +924,27 @@ void ConstantArray::destroyConstant() {
   destroyConstantImpl();
 }
 
-// ConstantArray::get(const string&) - Return an array that is initialized to
-// contain the specified string.  A null terminator is added to the specified
-// string so that it may be used in a natural way...
-//
-Constant *ConstantArray::get(const std::string &Str) {
+/// ConstantArray::get(const string&) - Return an array that is initialized to
+/// contain the specified string.  If length is zero then a null terminator is 
+/// added to the specified string so that it may be used in a natural way. 
+/// 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");
   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]));
 
   // Add a null terminator to the string...
-  ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
+  if (length == 0) {
+    ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
+    copy_len++;
+  }
 
-  ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
+  ArrayType *ATy = ArrayType::get(Type::SByteTy, copy_len);
   return ConstantArray::get(ATy, ElementVals);
 }