1 //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----===
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/ConvertUTF.h"
11 #include "llvm/Support/SwapByteOrder.h"
17 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
18 char *&ResultPtr, const UTF8 *&ErrorPtr) {
19 assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
20 ConversionResult result = conversionOK;
21 // Copy the character span over.
22 if (WideCharWidth == 1) {
23 const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin());
24 if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
25 result = sourceIllegal;
28 memcpy(ResultPtr, Source.data(), Source.size());
29 ResultPtr += Source.size();
31 } else if (WideCharWidth == 2) {
32 const UTF8 *sourceStart = (const UTF8*)Source.data();
33 // FIXME: Make the type of the result buffer correct instead of
34 // using reinterpret_cast.
35 UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
36 ConversionFlags flags = strictConversion;
37 result = ConvertUTF8toUTF16(
38 &sourceStart, sourceStart + Source.size(),
39 &targetStart, targetStart + 2*Source.size(), flags);
40 if (result == conversionOK)
41 ResultPtr = reinterpret_cast<char*>(targetStart);
43 ErrorPtr = sourceStart;
44 } else if (WideCharWidth == 4) {
45 const UTF8 *sourceStart = (const UTF8*)Source.data();
46 // FIXME: Make the type of the result buffer correct instead of
47 // using reinterpret_cast.
48 UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
49 ConversionFlags flags = strictConversion;
50 result = ConvertUTF8toUTF32(
51 &sourceStart, sourceStart + Source.size(),
52 &targetStart, targetStart + 4*Source.size(), flags);
53 if (result == conversionOK)
54 ResultPtr = reinterpret_cast<char*>(targetStart);
56 ErrorPtr = sourceStart;
58 assert((result != targetExhausted)
59 && "ConvertUTF8toUTFXX exhausted target buffer");
60 return result == conversionOK;
63 bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
64 const UTF32 *SourceStart = &Source;
65 const UTF32 *SourceEnd = SourceStart + 1;
66 UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
67 UTF8 *TargetEnd = TargetStart + 4;
68 ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
69 &TargetStart, TargetEnd,
71 if (CR != conversionOK)
74 ResultPtr = reinterpret_cast<char*>(TargetStart);
78 bool hasUTF16ByteOrderMark(ArrayRef<char> S) {
79 return (S.size() >= 2 &&
80 ((S[0] == '\xff' && S[1] == '\xfe') ||
81 (S[0] == '\xfe' && S[1] == '\xff')));
84 bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
87 // Error out on an uneven byte count.
88 if (SrcBytes.size() % 2)
91 // Avoid OOB by returning early on empty input.
95 const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
96 const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
98 // Byteswap if necessary.
99 std::vector<UTF16> ByteSwapped;
100 if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
101 ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
102 for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I)
103 ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]);
104 Src = &ByteSwapped[0];
105 SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
108 // Skip the BOM for conversion.
109 if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
112 // Just allocate enough space up front. We'll shrink it later.
113 Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT);
114 UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
115 UTF8 *DstEnd = Dst + Out.size();
117 ConversionResult CR =
118 ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
119 assert(CR != targetExhausted);
121 if (CR != conversionOK) {
126 Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
130 } // end namespace llvm