LLVM  14.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);
40  &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);
53  &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;
70  ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
71  &TargetStart, TargetEnd,
73  if (CR != conversionOK)
74  return false;
75 
76  ResultPtr = reinterpret_cast<char*>(TargetStart);
77  return true;
78 }
79 
81  return (S.size() >= 2 &&
82  ((S[0] == '\xff' && S[1] == '\xfe') ||
83  (S[0] == '\xfe' && S[1] == '\xff')));
84 }
85 
86 bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
87  assert(Out.empty());
88 
89  // Error out on an uneven byte count.
90  if (SrcBytes.size() % 2)
91  return false;
92 
93  // Avoid OOB by returning early on empty input.
94  if (SrcBytes.empty())
95  return true;
96 
97  const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
98  const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
99 
100  assert((uintptr_t)Src % sizeof(UTF16) == 0);
101 
102  // Byteswap if necessary.
103  std::vector<UTF16> ByteSwapped;
104  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
105  ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
106  for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I)
107  ByteSwapped[I] = llvm::ByteSwap_16(ByteSwapped[I]);
108  Src = &ByteSwapped[0];
109  SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
110  }
111 
112  // Skip the BOM for conversion.
113  if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
114  Src++;
115 
116  // Just allocate enough space up front. We'll shrink it later. Allocate
117  // enough that we can fit a null terminator without reallocating.
118  Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
119  UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
120  UTF8 *DstEnd = Dst + Out.size();
121 
122  ConversionResult CR =
123  ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
124  assert(CR != targetExhausted);
125 
126  if (CR != conversionOK) {
127  Out.clear();
128  return false;
129  }
130 
131  Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
132  Out.push_back(0);
133  Out.pop_back();
134  return true;
135 }
136 
137 bool convertUTF16ToUTF8String(ArrayRef<UTF16> Src, std::string &Out)
138 {
140  llvm::ArrayRef<char>(reinterpret_cast<const char *>(Src.data()),
141  Src.size() * sizeof(UTF16)), Out);
142 }
143 
145  SmallVectorImpl<UTF16> &DstUTF16) {
146  assert(DstUTF16.empty());
147 
148  // Avoid OOB by returning early on empty input.
149  if (SrcUTF8.empty()) {
150  DstUTF16.push_back(0);
151  DstUTF16.pop_back();
152  return true;
153  }
154 
155  const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
156  const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
157 
158  // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
159  // as UTF-16 should always require the same amount or less code units than the
160  // UTF-8 encoding. Allocate one extra byte for the null terminator though,
161  // so that someone calling DstUTF16.data() gets a null terminated string.
162  // We resize down later so we don't have to worry that this over allocates.
163  DstUTF16.resize(SrcUTF8.size()+1);
164  UTF16 *Dst = &DstUTF16[0];
165  UTF16 *DstEnd = Dst + DstUTF16.size();
166 
167  ConversionResult CR =
168  ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
169  assert(CR != targetExhausted);
170 
171  if (CR != conversionOK) {
172  DstUTF16.clear();
173  return false;
174  }
175 
176  DstUTF16.resize(Dst - &DstUTF16[0]);
177  DstUTF16.push_back(0);
178  DstUTF16.pop_back();
179  return true;
180 }
181 
182 static_assert(sizeof(wchar_t) == 1 || sizeof(wchar_t) == 2 ||
183  sizeof(wchar_t) == 4,
184  "Expected wchar_t to be 1, 2, or 4 bytes");
185 
186 template <typename TResult>
188  TResult &Result) {
189  // Even in the case of UTF-16, the number of bytes in a UTF-8 string is
190  // at least as large as the number of elements in the resulting wide
191  // string, because surrogate pairs take at least 4 bytes in UTF-8.
192  Result.resize(Source.size() + 1);
193  char *ResultPtr = reinterpret_cast<char *>(&Result[0]);
194  const UTF8 *ErrorPtr;
195  if (!ConvertUTF8toWide(sizeof(wchar_t), Source, ResultPtr, ErrorPtr)) {
196  Result.clear();
197  return false;
198  }
199  Result.resize(reinterpret_cast<wchar_t *>(ResultPtr) - &Result[0]);
200  return true;
201 }
202 
203 bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result) {
204  return ConvertUTF8toWideInternal(Source, Result);
205 }
206 
207 bool ConvertUTF8toWide(const char *Source, std::wstring &Result) {
208  if (!Source) {
209  Result.clear();
210  return true;
211  }
212  return ConvertUTF8toWide(llvm::StringRef(Source), Result);
213 }
214 
215 bool convertWideToUTF8(const std::wstring &Source, std::string &Result) {
216  if (sizeof(wchar_t) == 1) {
217  const UTF8 *Start = reinterpret_cast<const UTF8 *>(Source.data());
218  const UTF8 *End =
219  reinterpret_cast<const UTF8 *>(Source.data() + Source.size());
220  if (!isLegalUTF8String(&Start, End))
221  return false;
222  Result.resize(Source.size());
223  memcpy(&Result[0], Source.data(), Source.size());
224  return true;
225  } else if (sizeof(wchar_t) == 2) {
227  llvm::ArrayRef<UTF16>(reinterpret_cast<const UTF16 *>(Source.data()),
228  Source.size()),
229  Result);
230  } else if (sizeof(wchar_t) == 4) {
231  const UTF32 *Start = reinterpret_cast<const UTF32 *>(Source.data());
232  const UTF32 *End =
233  reinterpret_cast<const UTF32 *>(Source.data() + Source.size());
234  Result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * Source.size());
235  UTF8 *ResultPtr = reinterpret_cast<UTF8 *>(&Result[0]);
236  UTF8 *ResultEnd = reinterpret_cast<UTF8 *>(&Result[0] + Result.size());
237  if (ConvertUTF32toUTF8(&Start, End, &ResultPtr, ResultEnd,
239  Result.resize(reinterpret_cast<char *>(ResultPtr) - &Result[0]);
240  return true;
241  } else {
242  Result.clear();
243  return false;
244  }
245  } else {
247  "Control should never reach this point; see static_assert further up");
248  }
249 }
250 
251 } // end namespace llvm
252 
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
UNI_UTF16_BYTE_ORDER_MARK_NATIVE
#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE
Definition: ConvertUTF.h:123
StringRef.h
llvm::conversionOK
@ conversionOK
Definition: ConvertUTF.h:127
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:80
SwapByteOrder.h
UNI_MAX_UTF8_BYTES_PER_CODE_POINT
#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT
Definition: ConvertUTF.h:121
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
llvm::ArrayRef::data
const T * data() const
Definition: ArrayRef.h:162
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:130
llvm::ConvertUTF32toUTF8
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:317
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
Definition: ConvertUTF.h:124
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:606
llvm::ConversionFlags
ConversionFlags
Definition: ConvertUTF.h:133
llvm::targetExhausted
@ targetExhausted
Definition: ConvertUTF.h:129
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:86
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::ConversionResult
ConversionResult
Definition: ConvertUTF.h:126
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:130
I
#define I(x, y, z)
Definition: MD5.cpp:59
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(<
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:710
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
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:215
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::strictConversion
@ strictConversion
Definition: ConvertUTF.h:134
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:144
llvm::ConvertUTF8toUTF16
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:541
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:153
llvm::ConvertUTF16toUTF8
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:247
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:585
llvm::ConvertUTF8toWideInternal
static bool ConvertUTF8toWideInternal(llvm::StringRef Source, TResult &Result)
Definition: ConvertUTFWrapper.cpp:187
llvm::isLegalUTF8String
Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
Definition: ConvertUTF.cpp:529
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
llvm::UTF32
unsigned int UTF32
Definition: ConvertUTF.h:109
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::UTF16
unsigned short UTF16
Definition: ConvertUTF.h:110
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
llvm::UTF8
unsigned char UTF8
Definition: ConvertUTF.h:111
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:154