annotate lib/Support/ConvertUTFWrapper.cpp @ 124:4fa72497ed5d

fix
author mir3636
date Thu, 30 Nov 2017 20:04:56 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----===
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
10 #include "llvm/ADT/ArrayRef.h"
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
11 #include "llvm/ADT/StringRef.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
12 #include "llvm/Support/ConvertUTF.h"
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
13 #include "llvm/Support/ErrorHandling.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "llvm/Support/SwapByteOrder.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include <string>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include <vector>
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 namespace llvm {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 char *&ResultPtr, const UTF8 *&ErrorPtr) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 ConversionResult result = conversionOK;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 // Copy the character span over.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 if (WideCharWidth == 1) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 result = sourceIllegal;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 ErrorPtr = Pos;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 } else {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 memcpy(ResultPtr, Source.data(), Source.size());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 ResultPtr += Source.size();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 } else if (WideCharWidth == 2) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 const UTF8 *sourceStart = (const UTF8*)Source.data();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 // FIXME: Make the type of the result buffer correct instead of
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 // using reinterpret_cast.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 ConversionFlags flags = strictConversion;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 result = ConvertUTF8toUTF16(
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 &sourceStart, sourceStart + Source.size(),
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
42 &targetStart, targetStart + Source.size(), flags);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 if (result == conversionOK)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 ResultPtr = reinterpret_cast<char*>(targetStart);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 else
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 ErrorPtr = sourceStart;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 } else if (WideCharWidth == 4) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 const UTF8 *sourceStart = (const UTF8*)Source.data();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 // FIXME: Make the type of the result buffer correct instead of
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 // using reinterpret_cast.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 ConversionFlags flags = strictConversion;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 result = ConvertUTF8toUTF32(
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 &sourceStart, sourceStart + Source.size(),
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
55 &targetStart, targetStart + Source.size(), flags);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 if (result == conversionOK)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 ResultPtr = reinterpret_cast<char*>(targetStart);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 else
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 ErrorPtr = sourceStart;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 assert((result != targetExhausted)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 && "ConvertUTF8toUTFXX exhausted target buffer");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 return result == conversionOK;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 const UTF32 *SourceStart = &Source;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 const UTF32 *SourceEnd = SourceStart + 1;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 UTF8 *TargetEnd = TargetStart + 4;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 &TargetStart, TargetEnd,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 strictConversion);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 if (CR != conversionOK)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 ResultPtr = reinterpret_cast<char*>(TargetStart);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 bool hasUTF16ByteOrderMark(ArrayRef<char> S) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 return (S.size() >= 2 &&
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 ((S[0] == '\xff' && S[1] == '\xfe') ||
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 (S[0] == '\xfe' && S[1] == '\xff')));
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes, std::string &Out) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 assert(Out.empty());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 // Error out on an uneven byte count.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 if (SrcBytes.size() % 2)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 // Avoid OOB by returning early on empty input.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 if (SrcBytes.empty())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 const UTF16 *Src = reinterpret_cast<const UTF16 *>(SrcBytes.begin());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 const UTF16 *SrcEnd = reinterpret_cast<const UTF16 *>(SrcBytes.end());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 // Byteswap if necessary.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 std::vector<UTF16> ByteSwapped;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_SWAPPED) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 ByteSwapped.insert(ByteSwapped.end(), Src, SrcEnd);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 for (unsigned I = 0, E = ByteSwapped.size(); I != E; ++I)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 ByteSwapped[I] = llvm::sys::SwapByteOrder_16(ByteSwapped[I]);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 Src = &ByteSwapped[0];
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 SrcEnd = &ByteSwapped[ByteSwapped.size() - 1] + 1;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 // Skip the BOM for conversion.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 if (Src[0] == UNI_UTF16_BYTE_ORDER_MARK_NATIVE)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 Src++;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
115 // Just allocate enough space up front. We'll shrink it later. Allocate
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
116 // enough that we can fit a null terminator without reallocating.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
117 Out.resize(SrcBytes.size() * UNI_MAX_UTF8_BYTES_PER_CODE_POINT + 1);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 UTF8 *Dst = reinterpret_cast<UTF8 *>(&Out[0]);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 UTF8 *DstEnd = Dst + Out.size();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 ConversionResult CR =
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 ConvertUTF16toUTF8(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 assert(CR != targetExhausted);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 if (CR != conversionOK) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 Out.clear();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 Out.resize(reinterpret_cast<char *>(Dst) - &Out[0]);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
131 Out.push_back(0);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
132 Out.pop_back();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
133 return true;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
134 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
135
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
136 bool convertUTF16ToUTF8String(ArrayRef<UTF16> Src, std::string &Out)
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
137 {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
138 return convertUTF16ToUTF8String(
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
139 llvm::ArrayRef<char>(reinterpret_cast<const char *>(Src.data()),
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
140 Src.size() * sizeof(UTF16)), Out);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
141 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
142
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
143 bool convertUTF8ToUTF16String(StringRef SrcUTF8,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
144 SmallVectorImpl<UTF16> &DstUTF16) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
145 assert(DstUTF16.empty());
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
146
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
147 // Avoid OOB by returning early on empty input.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
148 if (SrcUTF8.empty()) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
149 DstUTF16.push_back(0);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
150 DstUTF16.pop_back();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
151 return true;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
152 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
153
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
154 const UTF8 *Src = reinterpret_cast<const UTF8 *>(SrcUTF8.begin());
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
155 const UTF8 *SrcEnd = reinterpret_cast<const UTF8 *>(SrcUTF8.end());
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
156
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
157 // Allocate the same number of UTF-16 code units as UTF-8 code units. Encoding
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
158 // as UTF-16 should always require the same amount or less code units than the
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
159 // UTF-8 encoding. Allocate one extra byte for the null terminator though,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
160 // so that someone calling DstUTF16.data() gets a null terminated string.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
161 // We resize down later so we don't have to worry that this over allocates.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
162 DstUTF16.resize(SrcUTF8.size()+1);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
163 UTF16 *Dst = &DstUTF16[0];
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
164 UTF16 *DstEnd = Dst + DstUTF16.size();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
165
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
166 ConversionResult CR =
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
167 ConvertUTF8toUTF16(&Src, SrcEnd, &Dst, DstEnd, strictConversion);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
168 assert(CR != targetExhausted);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
169
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
170 if (CR != conversionOK) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
171 DstUTF16.clear();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
172 return false;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
173 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
174
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
175 DstUTF16.resize(Dst - &DstUTF16[0]);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
176 DstUTF16.push_back(0);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
177 DstUTF16.pop_back();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
181 static_assert(sizeof(wchar_t) == 1 || sizeof(wchar_t) == 2 ||
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
182 sizeof(wchar_t) == 4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
183 "Expected wchar_t to be 1, 2, or 4 bytes");
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
184
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
185 template <typename TResult>
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
186 static inline bool ConvertUTF8toWideInternal(llvm::StringRef Source,
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
187 TResult &Result) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
188 // Even in the case of UTF-16, the number of bytes in a UTF-8 string is
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
189 // at least as large as the number of elements in the resulting wide
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
190 // string, because surrogate pairs take at least 4 bytes in UTF-8.
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
191 Result.resize(Source.size() + 1);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
192 char *ResultPtr = reinterpret_cast<char *>(&Result[0]);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
193 const UTF8 *ErrorPtr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
194 if (!ConvertUTF8toWide(sizeof(wchar_t), Source, ResultPtr, ErrorPtr)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
195 Result.clear();
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
196 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
197 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
198 Result.resize(reinterpret_cast<wchar_t *>(ResultPtr) - &Result[0]);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
199 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
200 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
201
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
202 bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
203 return ConvertUTF8toWideInternal(Source, Result);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
204 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
205
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
206 bool ConvertUTF8toWide(const char *Source, std::wstring &Result) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
207 if (!Source) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
208 Result.clear();
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
209 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
210 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
211 return ConvertUTF8toWide(llvm::StringRef(Source), Result);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
212 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
213
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
214 bool convertWideToUTF8(const std::wstring &Source, std::string &Result) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
215 if (sizeof(wchar_t) == 1) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
216 const UTF8 *Start = reinterpret_cast<const UTF8 *>(Source.data());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
217 const UTF8 *End =
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
218 reinterpret_cast<const UTF8 *>(Source.data() + Source.size());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
219 if (!isLegalUTF8String(&Start, End))
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
220 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
221 Result.resize(Source.size());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
222 memcpy(&Result[0], Source.data(), Source.size());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
223 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
224 } else if (sizeof(wchar_t) == 2) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
225 return convertUTF16ToUTF8String(
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
226 llvm::ArrayRef<UTF16>(reinterpret_cast<const UTF16 *>(Source.data()),
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
227 Source.size()),
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
228 Result);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
229 } else if (sizeof(wchar_t) == 4) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
230 const UTF32 *Start = reinterpret_cast<const UTF32 *>(Source.data());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
231 const UTF32 *End =
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
232 reinterpret_cast<const UTF32 *>(Source.data() + Source.size());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
233 Result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * Source.size());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
234 UTF8 *ResultPtr = reinterpret_cast<UTF8 *>(&Result[0]);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
235 UTF8 *ResultEnd = reinterpret_cast<UTF8 *>(&Result[0] + Result.size());
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
236 if (ConvertUTF32toUTF8(&Start, End, &ResultPtr, ResultEnd,
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
237 strictConversion) == conversionOK) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
238 Result.resize(reinterpret_cast<char *>(ResultPtr) - &Result[0]);
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
239 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
240 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
241 Result.clear();
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
242 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
243 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
244 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
245 llvm_unreachable(
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
246 "Control should never reach this point; see static_assert further up");
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
247 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
248 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 83
diff changeset
249
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 } // end namespace llvm
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251