LLVM API Documentation
00001 //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----=== 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/Support/ConvertUTF.h" 00011 #include "llvm/Support/SwapByteOrder.h" 00012 #include <string> 00013 #include <vector> 00014 00015 namespace llvm { 00016 00017 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, 00018 char *&ResultPtr, const UTF8 *&ErrorPtr) { 00019 assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4); 00020 ConversionResult result = conversionOK; 00021 // Copy the character span over. 00022 if (WideCharWidth == 1) { 00023 const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin()); 00024 if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) { 00025 result = sourceIllegal; 00026 ErrorPtr = Pos; 00027 } else { 00028 memcpy(ResultPtr, Source.data(), Source.size()); 00029 ResultPtr += Source.size(); 00030 } 00031 } else if (WideCharWidth == 2) { 00032 const UTF8 *sourceStart = (const UTF8*)Source.data(); 00033 // FIXME: Make the type of the result buffer correct instead of 00034 // using reinterpret_cast. 00035 UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr); 00036 ConversionFlags flags = strictConversion; 00037 result = ConvertUTF8toUTF16( 00038 &sourceStart, sourceStart + Source.size(), 00039 &targetStart, targetStart + 2*Source.size(), flags); 00040 if (result == conversionOK) 00041 ResultPtr = reinterpret_cast<char*>(targetStart); 00042 else 00043 ErrorPtr = sourceStart; 00044 } else if (WideCharWidth == 4) { 00045 const UTF8 *sourceStart = (const UTF8*)Source.data(); 00046 // FIXME: Make the type of the result buffer correct instead of 00047 // using reinterpret_cast. 00048 UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr); 00049 ConversionFlags flags = strictConversion; 00050 result = ConvertUTF8toUTF32( 00051 &sourceStart, sourceStart + Source.size(), 00052 &targetStart, targetStart + 4*Source.size(), flags); 00053 if (result == conversionOK) 00054 ResultPtr = reinterpret_cast<char*>(targetStart); 00055 else 00056 ErrorPtr = sourceStart; 00057 } 00058 assert((result != targetExhausted) 00059 && "ConvertUTF8toUTFXX exhausted target buffer"); 00060 return result == conversionOK; 00061 } 00062 00063 bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) { 00064 const UTF32 *SourceStart = &Source; 00065 const UTF32 *SourceEnd = SourceStart + 1; 00066 UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr); 00067 UTF8 *TargetEnd = TargetStart + 4; 00068 ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd, 00069 &TargetStart, TargetEnd, 00070 strictConversion); 00071 if (CR != conversionOK) 00072 return false; 00073 00074 ResultPtr = reinterpret_cast<char*>(TargetStart); 00075 return true; 00076 } 00077 00078 bool hasUTF16ByteOrderMark(ArrayRef<char> S) { 00079 return (S.size() >= 2 && 00080 ((S[0] == '\xff' && S[1] == '\xfe') || 00081 (S[0] == '\xfe' && S[1] == '\xff'))); 00082 } 00083 00084 bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) { 00085 assert(Out.empty()); 00086 00087 // Error out on an uneven byte count. 00088 if (SrcBytes.size() % 2) 00089 return false; 00090 00091 // Avoid OOB by returning early on empty input. 00092 if (SrcBytes.empty()) 00093 return true; 00094 00095 const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin()); 00096 const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end()); 00097 00098 // Byteswap if necessary. 00099 std::vector<UTF16> ByteSwapped; 00100 if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) { 00101 ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd); 00102 for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I) 00103 ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]); 00104 Src = &ByteSwapped[0]; 00105 SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1; 00106 } 00107 00108 // Skip the BOM for conversion. 00109 if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE) 00110 Src++; 00111 00112 // Just allocate enough space up front. We'll shrink it later. 00113 Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT); 00114 UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]); 00115 UTF8 *DstEnd = Dst + Out.size(); 00116 00117 ConversionResult CR = 00118 ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion); 00119 assert(CR != targetExhausted); 00120 00121 if (CR != conversionOK) { 00122 Out.clear(); 00123 return false; 00124 } 00125 00126 Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]); 00127 return true; 00128 } 00129 00130 } // end namespace llvm 00131