LLVM  3.7.0
ConvertUTFWrapper.cpp
Go to the documentation of this file.
1 //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----===
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include <string>
13 #include <vector>
14 
15 namespace llvm {
16 
17 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
18  char *&ResultPtr, const UTF8 *&ErrorPtr) {
19  assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
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;
26  ErrorPtr = Pos;
27  } else {
28  memcpy(ResultPtr, Source.data(), Source.size());
29  ResultPtr += Source.size();
30  }
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);
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);
42  else
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);
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);
55  else
56  ErrorPtr = sourceStart;
57  }
58  assert((result != targetExhausted)
59  && "ConvertUTF8toUTFXX exhausted target buffer");
60  return result == conversionOK;
61 }
62 
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)
72  return false;
73 
74  ResultPtr = reinterpret_cast<char*>(TargetStart);
75  return true;
76 }
77 
79  return (S.size() >= 2 &&
80  ((S[0] == '\xff' && S[1] == '\xfe') ||
81  (S[0] == '\xfe' && S[1] == '\xff')));
82 }
83 
84 bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
85  assert(Out.empty());
86 
87  // Error out on an uneven byte count.
88  if (SrcBytes.size() % 2)
89  return false;
90 
91  // Avoid OOB by returning early on empty input.
92  if (SrcBytes.empty())
93  return true;
94 
95  const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
96  const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
97 
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;
106  }
107 
108  // Skip the BOM for conversion.
109  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
110  Src++;
111 
112  // Just allocate enough space up front. We'll shrink it later. Allocate
113  // enough that we can fit a null terminator without reallocating.
114  Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
115  UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
116  UTF8 *DstEnd = Dst + Out.size();
117 
118  ConversionResult CR =
119  ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
120  assert(CR != targetExhausted);
121 
122  if (CR != conversionOK) {
123  Out.clear();
124  return false;
125  }
126 
127  Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
128  Out.push_back(0);
129  Out.pop_back();
130  return true;
131 }
132 
134  SmallVectorImpl<UTF16> &DstUTF16) {
135  assert(DstUTF16.empty());
136 
137  // Avoid OOB by returning early on empty input.
138  if (SrcUTF8.empty()) {
139  DstUTF16.push_back(0);
140  DstUTF16.pop_back();
141  return true;
142  }
143 
144  const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
145  const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
146 
147  // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
148  // as UTF-16 should always require the same amount or less code units than the
149  // UTF-8 encoding. Allocate one extra byte for the null terminator though,
150  // so that someone calling DstUTF16.data() gets a null terminated string.
151  // We resize down later so we don't have to worry that this over allocates.
152  DstUTF16.resize(SrcUTF8.size()+1);
153  UTF16 *Dst = &DstUTF16[0];
154  UTF16 *DstEnd = Dst + DstUTF16.size();
155 
156  ConversionResult CR =
157  ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
158  assert(CR != targetExhausted);
159 
160  if (CR != conversionOK) {
161  DstUTF16.clear();
162  return false;
163  }
164 
165  DstUTF16.resize(Dst - &DstUTF16[0]);
166  DstUTF16.push_back(0);
167  DstUTF16.pop_back();
168  return true;
169 }
170 
171 } // end namespace llvm
172 
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.c:291
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
iterator end() const
Definition: ArrayRef.h:123
#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT
Definition: ConvertUTF.h:113
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.c:221
Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
Definition: ConvertUTF.c:503
ConversionResult
Definition: ConvertUTF.h:118
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
Definition: ConvertUTF.h:116
#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE
Definition: ConvertUTF.h:115
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
bool convertUTF8ToUTF16String(StringRef SrcUTF8, SmallVectorImpl< UTF16 > &DstUTF16)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
unsigned char UTF8
Definition: ConvertUTF.h:103
iterator begin() const
Definition: StringRef.h:90
uint16_t SwapByteOrder_16(uint16_t value)
SwapByteOrder_16 - This function returns a byte-swapped representation of the 16-bit argument...
Definition: SwapByteOrder.h:28
bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr)
unsigned short UTF16
Definition: ConvertUTF.h:102
iterator begin() const
Definition: ArrayRef.h:122
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:129
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
Definition: ConvertUTF.c:684
unsigned int UTF32
Definition: ConvertUTF.h:101
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
ConversionFlags
Definition: ConvertUTF.h:125
#define I(x, y, z)
Definition: MD5.cpp:54
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.c:515
bool hasUTF16ByteOrderMark(ArrayRef< char > S)
iterator end() const
Definition: StringRef.h:92
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
void resize(size_type N)
Definition: SmallVector.h:376