summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2015-01-26 22:05:50 +0000
committerZachary Turner <zturner@google.com>2015-01-26 22:05:50 +0000
commit02991af057e81b5ee7d0f97d929612369956f838 (patch)
tree5162fa383c879a38cd7c789c8214aacde0f1cac5 /llvm/lib
parent42c8f93cc872783dd625b93dbc62dc650476a297 (diff)
downloadbcm5719-llvm-02991af057e81b5ee7d0f97d929612369956f838.tar.gz
bcm5719-llvm-02991af057e81b5ee7d0f97d929612369956f838.zip
Have the UTF conversion wrappers append a null terminator.
This is especially useful for the UTF8 -> UTF16 direction, since there is no equivalent of llvm::SmallString<> for wide characters. This means that anyone who wants a null terminated string is forced to manually push and pop their own null terminator. Reviewed by: Reid Kleckner. llvm-svn: 227143
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Support/ConvertUTFWrapper.cpp15
1 files changed, 11 insertions, 4 deletions
diff --git a/llvm/lib/Support/ConvertUTFWrapper.cpp b/llvm/lib/Support/ConvertUTFWrapper.cpp
index 8f77bff4668..4feff01b08e 100644
--- a/llvm/lib/Support/ConvertUTFWrapper.cpp
+++ b/llvm/lib/Support/ConvertUTFWrapper.cpp
@@ -109,8 +109,9 @@ bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
Src++;
- // Just allocate enough space up front. We'll shrink it later.
- Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT);
+ // Just allocate enough space up front. We'll shrink it later. Allocate
+ // enough that we can fit a null terminator without reallocating.
+ Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
UTF8 *DstEnd = Dst + Out.size();
@@ -124,6 +125,8 @@ bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
}
Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
+ Out.push_back(0);
+ Out.pop_back();
return true;
}
@@ -140,8 +143,10 @@ bool convertUTF8ToUTF16String(StringRef SrcUTF8,
// Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
// as UTF-16 should always require the same amount or less code units than the
- // UTF-8 encoding.
- DstUTF16.resize(SrcUTF8.size());
+ // UTF-8 encoding. Allocate one extra byte for the null terminator though,
+ // so that someone calling DstUTF16.data() gets a null terminated string.
+ // We resize down later so we don't have to worry that this over allocates.
+ DstUTF16.resize(SrcUTF8.size()+1);
UTF16 *Dst = &DstUTF16[0];
UTF16 *DstEnd = Dst + DstUTF16.size();
@@ -155,6 +160,8 @@ bool convertUTF8ToUTF16String(StringRef SrcUTF8,
}
DstUTF16.resize(Dst - &DstUTF16[0]);
+ DstUTF16.push_back(0);
+ DstUTF16.pop_back();
return true;
}
OpenPOWER on IntegriCloud