20 char *&ResultPtr,
const UTF8 *&ErrorPtr) {
21 assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
24 if (WideCharWidth == 1) {
25 const UTF8 *Pos =
reinterpret_cast<const UTF8*
>(Source.
begin());
30 memcpy(ResultPtr, Source.
data(), Source.
size());
31 ResultPtr += Source.
size();
33 }
else if (WideCharWidth == 2) {
37 UTF16 *targetStart =
reinterpret_cast<UTF16*
>(ResultPtr);
40 &sourceStart, sourceStart + Source.
size(),
41 &targetStart, targetStart + Source.
size(), flags);
43 ResultPtr =
reinterpret_cast<char*
>(targetStart);
45 ErrorPtr = sourceStart;
46 }
else if (WideCharWidth == 4) {
50 UTF32 *targetStart =
reinterpret_cast<UTF32*
>(ResultPtr);
53 &sourceStart, sourceStart + Source.
size(),
54 &targetStart, targetStart + Source.
size(), flags);
56 ResultPtr =
reinterpret_cast<char*
>(targetStart);
58 ErrorPtr = sourceStart;
61 &&
"ConvertUTF8toUTFXX exhausted target buffer");
67 const UTF32 *SourceEnd = SourceStart + 1;
68 UTF8 *TargetStart =
reinterpret_cast<UTF8 *
>(ResultPtr);
69 UTF8 *TargetEnd = TargetStart + 4;
71 &TargetStart, TargetEnd,
76 ResultPtr =
reinterpret_cast<char*
>(TargetStart);
81 return (S.
size() >= 2 &&
82 ((S[0] ==
'\xff' && S[1] ==
'\xfe') ||
83 (S[0] ==
'\xfe' && S[1] ==
'\xff')));
90 if (SrcBytes.
size() % 2)
98 const UTF16 *SrcEnd =
reinterpret_cast<const UTF16 *
>(SrcBytes.
end());
101 std::vector<UTF16> ByteSwapped;
103 ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
104 for (
unsigned I = 0,
E = ByteSwapped.size();
I !=
E; ++
I)
106 Src = &ByteSwapped[0];
107 SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
117 UTF8 *Dst =
reinterpret_cast<UTF8 *
>(&Out[0]);
118 UTF8 *DstEnd = Dst + Out.size();
129 Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
147 if (SrcUTF8.
empty()) {
153 const UTF8 *Src =
reinterpret_cast<const UTF8 *
>(SrcUTF8.
begin());
154 const UTF8 *SrcEnd =
reinterpret_cast<const UTF8 *
>(SrcUTF8.
end());
162 UTF16 *Dst = &DstUTF16[0];
174 DstUTF16.
resize(Dst - &DstUTF16[0]);
180 static_assert(
sizeof(
wchar_t) == 1 ||
sizeof(
wchar_t) == 2 ||
181 sizeof(
wchar_t) == 4,
182 "Expected wchar_t to be 1, 2, or 4 bytes");
184 template <
typename TResult>
190 Result.resize(Source.
size() + 1);
191 char *ResultPtr =
reinterpret_cast<char *
>(&Result[0]);
192 const UTF8 *ErrorPtr;
197 Result.resize(reinterpret_cast<wchar_t *>(ResultPtr) - &Result[0]);
214 if (
sizeof(
wchar_t) == 1) {
215 const UTF8 *Start =
reinterpret_cast<const UTF8 *
>(Source.data());
217 reinterpret_cast<const UTF8 *
>(Source.data() + Source.size());
220 Result.resize(Source.size());
221 memcpy(&Result[0], Source.data(), Source.size());
223 }
else if (
sizeof(
wchar_t) == 2) {
228 }
else if (
sizeof(
wchar_t) == 4) {
229 const UTF32 *Start =
reinterpret_cast<const UTF32 *
>(Source.data());
231 reinterpret_cast<const UTF32 *
>(Source.data() + Source.size());
233 UTF8 *ResultPtr =
reinterpret_cast<UTF8 *
>(&Result[0]);
234 UTF8 *ResultEnd =
reinterpret_cast<UTF8 *
>(&Result[0] + Result.size());
237 Result.resize(reinterpret_cast<char *>(ResultPtr) - &Result[0]);
245 "Control should never reach this point; see static_assert further up");
bool hasUTF16ByteOrderMark(ArrayRef< char > SrcBytes)
Returns true if a blob of text starts with a UTF-16 big or little endian byte order mark...
This class represents lattice values for constants.
void push_back(const T &Elt)
#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
static bool ConvertUTF8toWideInternal(llvm::StringRef Source, TResult &Result)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE
bool convertWideToUTF8(const std::wstring &Source, std::string &Result)
Converts a std::wstring to a UTF-8 encoded std::string.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
bool convertUTF8ToUTF16String(StringRef SrcUTF8, SmallVectorImpl< UTF16 > &DstUTF16)
Converts a UTF-8 string into a UTF-16 string with native endianness.
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
uint16_t SwapByteOrder_16(uint16_t value)
SwapByteOrder_16 - This function returns a byte-swapped representation of the 16-bit argument...
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on WideCharWidth.
bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr)
Convert an Unicode code point to UTF8 sequence.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
LLVM_NODISCARD bool empty() const
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
bool empty() const
empty - Check if the array is empty.