diff options
author | Reid Kleckner <reid@kleckner.net> | 2015-01-26 19:51:00 +0000 |
---|---|---|
committer | Reid Kleckner <reid@kleckner.net> | 2015-01-26 19:51:00 +0000 |
commit | d8cb6b00c535fac4404d77413e3f437a0dc33a9b (patch) | |
tree | 2185a062f4b85258981011d00c59f924b7110e33 /llvm/lib/Support | |
parent | b11a1b7b2c1c0cbc6adace00b6084c3e1994730a (diff) | |
download | bcm5719-llvm-d8cb6b00c535fac4404d77413e3f437a0dc33a9b.tar.gz bcm5719-llvm-d8cb6b00c535fac4404d77413e3f437a0dc33a9b.zip |
Add a UTF8 to UTF16 conversion wrapper for use in the pdb dumper
This can also be used instead of the WindowsSupport.h ConvertUTF8ToUTF16
helpers, but that will require massaging some character types. The
Windows support routines want wchar_t output, but wchar_t is often 32
bits on non-Windows OSs.
llvm-svn: 227122
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/ConvertUTFWrapper.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/llvm/lib/Support/ConvertUTFWrapper.cpp b/llvm/lib/Support/ConvertUTFWrapper.cpp index e45335ddcb6..8f77bff4668 100644 --- a/llvm/lib/Support/ConvertUTFWrapper.cpp +++ b/llvm/lib/Support/ConvertUTFWrapper.cpp @@ -127,5 +127,36 @@ bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) { return true; } +bool convertUTF8ToUTF16String(StringRef SrcUTF8, + SmallVectorImpl<UTF16> &DstUTF16) { + assert(DstUTF16.empty()); + + // Avoid OOB by returning early on empty input. + if (SrcUTF8.empty()) + return true; + + const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin()); + const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end()); + + // 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()); + UTF16 *Dst = &DstUTF16[0]; + UTF16 *DstEnd = Dst + DstUTF16.size(); + + ConversionResult CR = + ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion); + assert(CR != targetExhausted); + + if (CR != conversionOK) { + DstUTF16.clear(); + return false; + } + + DstUTF16.resize(Dst - &DstUTF16[0]); + return true; +} + } // end namespace llvm |