LLVM  16.0.0git
ConvertUTFWrapper.cpp
Go to the documentation of this file.
1 //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----===
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/StringRef.h"
14 #include <string>
15 #include <vector>
16 
17 namespace llvm {
18 
19 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
20  char *&ResultPtr, const UTF8 *&ErrorPtr) {
21  assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
23  // Copy the character span over.
24  if (WideCharWidth == 1) {
25  const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin());
26  if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
28  ErrorPtr = Pos;
29  } else {
30  memcpy(ResultPtr, Source.data(), Source.size());
31  ResultPtr += Source.size();
32  }
33  } else if (WideCharWidth == 2) {
34  const UTF8 *sourceStart = (const UTF8*)Source.data();
35  // FIXME: Make the type of the result buffer correct instead of
36  // using reinterpret_cast.
37  UTF16 *targetStart = reinterpret_cast<UTF16 *>(ResultPtr);
39  result =
40  ConvertUTF8toUTF16(&sourceStart, sourceStart + Source.size(),
41  &targetStart, targetStart + Source.size(), flags);
42  if (result == conversionOK)
43  ResultPtr = reinterpret_cast<char *>(targetStart);
44  else
45  ErrorPtr = sourceStart;
46  } else if (WideCharWidth == 4) {
47  const UTF8 *sourceStart = (const UTF8 *)Source.data();
48  // FIXME: Make the type of the result buffer correct instead of
49  // using reinterpret_cast.
50  UTF32 *targetStart = reinterpret_cast<UTF32 *>(ResultPtr);
52  result =
53  ConvertUTF8toUTF32(&sourceStart, sourceStart + Source.size(),
54  &targetStart, targetStart + Source.size(), flags);
55  if (result == conversionOK)
56  ResultPtr = reinterpret_cast<char *>(targetStart);
57  else
58  ErrorPtr = sourceStart;
59  }
61  "ConvertUTF8toUTFXX exhausted target buffer");
62  return result == conversionOK;
63 }
64 
65 bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
66  const UTF32 *SourceStart = &Source;
67  const UTF32 *SourceEnd = SourceStart + 1;
68  UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
69  UTF8 *TargetEnd = TargetStart + 4;
71  &SourceStart, SourceEnd, &TargetStart, TargetEnd, strictConversion);
72  if (CR != conversionOK)
73  return false;
74 
75  ResultPtr = reinterpret_cast<char *>(TargetStart);
76  return true;
77 }
78 
80  return (S.size() >= 2 && ((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  assert((uintptr_t)Src % sizeof(UTF16) == 0);
99 
100  // Byteswap if necessary.
101  std::vector<UTF16> ByteSwapped;
102  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
103  ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
104  for (UTF16 &I : ByteSwapped)
105  I = llvm::ByteSwap_16(I);
106  Src = &ByteSwapped[0];
107  SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
108  }
109 
110  // Skip the BOM for conversion.
111  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
112  Src++;
113 
114  // Just allocate enough space up front. We'll shrink it later. Allocate
115  // enough that we can fit a null terminator without reallocating.
116  Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
117  UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
118  UTF8 *DstEnd = Dst + Out.size();
119 
120  ConversionResult CR =
121  ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
122  assert(CR != targetExhausted);
123 
124  if (CR != conversionOK) {
125  Out.clear();
126  return false;
127  }
128 
129  Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
130  Out.push_back(0);
131  Out.pop_back();
132  return true;
133 }
134 
135 bool convertUTF16ToUTF8String(ArrayRef<UTF16> Src, std::string &Out) {
137  llvm::ArrayRef<char>(reinterpret_cast<const char *>(Src.data()),
138  Src.size() * sizeof(UTF16)),
139  Out);
140 }
141 
142 bool convertUTF32ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
143  assert(Out.empty());
144 
145  // Error out on an uneven byte count.
146  if (SrcBytes.size() % 4)
147  return false;
148 
149  // Avoid OOB by returning early on empty input.
150  if (SrcBytes.empty())
151  return true;
152 
153  const UTF32 *Src = reinterpret_cast<const UTF32 *>(SrcBytes.begin());
154  const UTF32 *SrcEnd = reinterpret_cast<const UTF32 *>(SrcBytes.end());
155 
156  assert((uintptr_t)Src % sizeof(UTF32) == 0);
157 
158  // Byteswap if necessary.
159  std::vector<UTF32> ByteSwapped;
160  if (Src[0] == UNI_UTF32_BYTE_ORDER_MARK_SWAPPED) {
161  ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
162  for (UTF32 &I : ByteSwapped)
163  I = llvm::ByteSwap_32(I);
164  Src = &ByteSwapped[0];
165  SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
166  }
167 
168  // Skip the BOM for conversion.
169  if (Src[0] == UNI_UTF32_BYTE_ORDER_MARK_NATIVE)
170  Src++;
171 
172  // Just allocate enough space up front. We'll shrink it later. Allocate
173  // enough that we can fit a null terminator without reallocating.
174  Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
175  UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
176  UTF8 *DstEnd = Dst + Out.size();
177 
178  ConversionResult CR =
179  ConvertUTF32toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
180  assert(CR != targetExhausted);
181 
182  if (CR != conversionOK) {
183  Out.clear();
184  return false;
185  }
186 
187  Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
188  Out.push_back(0);
189  Out.pop_back();
190  return true;
191 }
192 
193 bool convertUTF32ToUTF8String(ArrayRef<UTF32> Src, std::string &Out) {
195  llvm::ArrayRef<char>(reinterpret_cast<const char *>(Src.data()),
196  Src.size() * sizeof(UTF32)),
197  Out);
198 }
199 
201  SmallVectorImpl<UTF16> &DstUTF16) {
202  assert(DstUTF16.empty());
203 
204  // Avoid OOB by returning early on empty input.
205  if (SrcUTF8.empty()) {
206  DstUTF16.push_back(0);
207  DstUTF16.pop_back();
208  return true;
209  }
210 
211  const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
212  const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
213 
214  // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
215  // as UTF-16 should always require the same amount or less code units than the
216  // UTF-8 encoding. Allocate one extra byte for the null terminator though,
217  // so that someone calling DstUTF16.data() gets a null terminated string.
218  // We resize down later so we don't have to worry that this over allocates.
219  DstUTF16.resize(SrcUTF8.size()+1);
220  UTF16 *Dst = &DstUTF16[0];
221  UTF16 *DstEnd = Dst + DstUTF16.size();
222 
223  ConversionResult CR =
224  ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
225  assert(CR != targetExhausted);
226 
227  if (CR != conversionOK) {
228  DstUTF16.clear();
229  return false;
230  }
231 
232  DstUTF16.resize(Dst - &DstUTF16[0]);
233  DstUTF16.push_back(0);
234  DstUTF16.pop_back();
235  return true;
236 }
237 
238 static_assert(sizeof(wchar_t) == 1 || sizeof(wchar_t) == 2 ||
239  sizeof(wchar_t) == 4,
240  "Expected wchar_t to be 1, 2, or 4 bytes");
241 
242 template <typename TResult>
244  TResult &Result) {
245  // Even in the case of UTF-16, the number of bytes in a UTF-8 string is
246  // at least as large as the number of elements in the resulting wide
247  // string, because surrogate pairs take at least 4 bytes in UTF-8.
248  Result.resize(Source.size() + 1);
249  char *ResultPtr = reinterpret_cast<char *>(&Result[0]);
250  const UTF8 *ErrorPtr;
251  if (!ConvertUTF8toWide(sizeof(wchar_t), Source, ResultPtr, ErrorPtr)) {
252  Result.clear();
253  return false;
254  }
255  Result.resize(reinterpret_cast<wchar_t *>(ResultPtr) - &Result[0]);
256  return true;
257 }
258 
259 bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result) {
260  return ConvertUTF8toWideInternal(Source, Result);
261 }
262 
263 bool ConvertUTF8toWide(const char *Source, std::wstring &Result) {
264  if (!Source) {
265  Result.clear();
266  return true;
267  }
268  return ConvertUTF8toWide(llvm::StringRef(Source), Result);
269 }
270 
271 bool convertWideToUTF8(const std::wstring &Source, std::string &Result) {
272  if (sizeof(wchar_t) == 1) {
273  const UTF8 *Start = reinterpret_cast<const UTF8 *>(Source.data());
274  const UTF8 *End =
275  reinterpret_cast<const UTF8 *>(Source.data() + Source.size());
276  if (!isLegalUTF8String(&Start, End))
277  return false;
278  Result.resize(Source.size());
279  memcpy(&Result[0], Source.data(), Source.size());
280  return true;
281  } else if (sizeof(wchar_t) == 2) {
283  llvm::ArrayRef<UTF16>(reinterpret_cast<const UTF16 *>(Source.data()),
284  Source.size()),
285  Result);
286  } else if (sizeof(wchar_t) == 4) {
287  const UTF32 *Start = reinterpret_cast<const UTF32 *>(Source.data());
288  const UTF32 *End =
289  reinterpret_cast<const UTF32 *>(Source.data() + Source.size());
290  Result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * Source.size());
291  UTF8 *ResultPtr = reinterpret_cast<UTF8 *>(&Result[0]);
292  UTF8 *ResultEnd = reinterpret_cast<UTF8 *>(&Result[0] + Result.size());
293  if (ConvertUTF32toUTF8(&Start, End, &ResultPtr, ResultEnd,
295  Result.resize(reinterpret_cast<char *>(ResultPtr) - &Result[0]);
296  return true;
297  } else {
298  Result.clear();
299  return false;
300  }
301  } else {
303  "Control should never reach this point; see static_assert further up");
304  }
305 }
306 
307 } // end namespace llvm
308 
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
UNI_UTF16_BYTE_ORDER_MARK_NATIVE
#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE
Definition: ConvertUTF.h:142
StringRef.h
llvm::conversionOK
@ conversionOK
Definition: ConvertUTF.h:149
llvm::ConvertCodePointToUTF8
bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr)
Convert an Unicode code point to UTF8 sequence.
Definition: ConvertUTFWrapper.cpp:65
ErrorHandling.h
llvm::hasUTF16ByteOrderMark
bool hasUTF16ByteOrderMark(ArrayRef< char > SrcBytes)
Returns true if a blob of text starts with a UTF-16 big or little endian byte order mark.
Definition: ConvertUTFWrapper.cpp:79
SwapByteOrder.h
UNI_MAX_UTF8_BYTES_PER_CODE_POINT
#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT
Definition: ConvertUTF.h:140
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
result
It looks like we only need to define PPCfmarto for these because according to these instructions perform RTO on fma s result
Definition: README_P9.txt:256
llvm::sourceIllegal
@ sourceIllegal
Definition: ConvertUTF.h:152
llvm::ConvertUTF32toUTF8
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:333
UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
Definition: ConvertUTF.h:143
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:624
llvm::ConversionFlags
ConversionFlags
Definition: ConvertUTF.h:155
llvm::targetExhausted
@ targetExhausted
Definition: ConvertUTF.h:151
llvm::convertUTF16ToUTF8String
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
Definition: ConvertUTFWrapper.cpp:84
llvm::ByteSwap_16
uint16_t ByteSwap_16(uint16_t value)
ByteSwap_16 - This function returns a byte-swapped representation of the 16-bit argument.
Definition: SwapByteOrder.h:53
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
llvm::ConversionResult
ConversionResult
Definition: ConvertUTF.h:148
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:113
I
#define I(x, y, z)
Definition: MD5.cpp:58
ArrayRef.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
UNI_UTF32_BYTE_ORDER_MARK_NATIVE
#define UNI_UTF32_BYTE_ORDER_MARK_NATIVE
Definition: ConvertUTF.h:145
llvm::ConvertUTF8toUTF32
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
Definition: ConvertUTF.cpp:736
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:98
llvm::ArrayRef< char >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::convertWideToUTF8
bool convertWideToUTF8(const std::wstring &Source, std::string &Result)
Converts a std::wstring to a UTF-8 encoded std::string.
Definition: ConvertUTFWrapper.cpp:271
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::strictConversion
@ strictConversion
Definition: ConvertUTF.h:156
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
ConvertUTF.h
llvm::convertUTF8ToUTF16String
bool convertUTF8ToUTF16String(StringRef SrcUTF8, SmallVectorImpl< UTF16 > &DstUTF16)
Converts a UTF-8 string into a UTF-16 string with native endianness.
Definition: ConvertUTFWrapper.cpp:200
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
llvm::ConvertUTF8toUTF16
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:567
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
llvm::ConvertUTF16toUTF8
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:263
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:596
llvm::ConvertUTF8toWideInternal
static bool ConvertUTF8toWideInternal(llvm::StringRef Source, TResult &Result)
Definition: ConvertUTFWrapper.cpp:243
llvm::isLegalUTF8String
Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
Definition: ConvertUTF.cpp:555
llvm::ConvertUTF8toWide
bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on WideCharWidth.
Definition: ConvertUTFWrapper.cpp:19
UNI_UTF32_BYTE_ORDER_MARK_SWAPPED
#define UNI_UTF32_BYTE_ORDER_MARK_SWAPPED
Definition: ConvertUTF.h:146
llvm::UTF32
unsigned int UTF32
Definition: ConvertUTF.h:128
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::ByteSwap_32
uint32_t ByteSwap_32(uint32_t value)
This function returns a byte-swapped representation of the 32-bit argument.
Definition: SwapByteOrder.h:66
llvm::UTF16
unsigned short UTF16
Definition: ConvertUTF.h:129
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:111
llvm::UTF8
unsigned char UTF8
Definition: ConvertUTF.h:130
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
llvm::convertUTF32ToUTF8String
bool convertUTF32ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF32 into a UTF8 std::string.
Definition: ConvertUTFWrapper.cpp:142