1
- // ===-- EncodingConverter .cpp - Encoding conversion class ---------*- C++ -*-=//
1
+ // ===-- TextEncoding .cpp - Encoding conversion class ----- ---------*- C++ -*-=//
2
2
//
3
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
4
// See https://llvm.org/LICENSE.txt for license information.
12
12
// /
13
13
// ===----------------------------------------------------------------------===//
14
14
15
- #include " llvm/Support/EncodingConverter .h"
15
+ #include " llvm/Support/TextEncoding .h"
16
16
#include " llvm/ADT/SmallString.h"
17
17
#include " llvm/ADT/SmallVector.h"
18
18
#include " llvm/ADT/StringExtras.h"
@@ -82,11 +82,12 @@ enum ConversionType {
82
82
// aforementioned encodings. The use of tables for conversion is only
83
83
// possible because EBCDIC 1047 is a single-byte, stateless encoding; other
84
84
// encodings are not supported.
85
- class EncodingConverterTable : public details ::EncodingConverterImplBase {
85
+ class TextEncodingConverterTable
86
+ : public details::TextEncodingConverterImplBase {
86
87
const ConversionType ConvType;
87
88
88
89
public:
89
- EncodingConverterTable (ConversionType ConvType) : ConvType(ConvType) {}
90
+ TextEncodingConverterTable (ConversionType ConvType) : ConvType(ConvType) {}
90
91
91
92
std::error_code convertString (StringRef Source,
92
93
SmallVectorImpl<char > &Result) override ;
@@ -95,8 +96,8 @@ class EncodingConverterTable : public details::EncodingConverterImplBase {
95
96
};
96
97
97
98
std::error_code
98
- EncodingConverterTable ::convertString (StringRef Source,
99
- SmallVectorImpl<char > &Result) {
99
+ TextEncodingConverterTable ::convertString (StringRef Source,
100
+ SmallVectorImpl<char > &Result) {
100
101
switch (ConvType) {
101
102
case IBM1047ToUTF8:
102
103
ConverterEBCDIC::convertToUTF8 (Source, Result);
@@ -117,13 +118,13 @@ struct UConverterDeleter {
117
118
};
118
119
using UConverterUniquePtr = std::unique_ptr<UConverter, UConverterDeleter>;
119
120
120
- class EncodingConverterICU : public details ::EncodingConverterImplBase {
121
+ class TextEncodingConverterICU : public details ::TextEncodingConverterImplBase {
121
122
UConverterUniquePtr FromConvDesc;
122
123
UConverterUniquePtr ToConvDesc;
123
124
124
125
public:
125
- EncodingConverterICU (UConverterUniquePtr FromConverter,
126
- UConverterUniquePtr ToConverter)
126
+ TextEncodingConverterICU (UConverterUniquePtr FromConverter,
127
+ UConverterUniquePtr ToConverter)
127
128
: FromConvDesc(std::move(FromConverter)),
128
129
ToConvDesc (std::move(ToConverter)) {}
129
130
@@ -138,8 +139,8 @@ class EncodingConverterICU : public details::EncodingConverterImplBase {
138
139
// insufficient buffer size. In the future, it would better to save the partial
139
140
// result and redo the conversion for the remaining string.
140
141
std::error_code
141
- EncodingConverterICU ::convertString (StringRef Source,
142
- SmallVectorImpl<char > &Result) {
142
+ TextEncodingConverterICU ::convertString (StringRef Source,
143
+ SmallVectorImpl<char > &Result) {
143
144
// Setup the input in case it has no backing data.
144
145
size_t InputLength = Source.size ();
145
146
const char *In = InputLength ? const_cast <char *>(Source.data ()) : " " ;
@@ -183,13 +184,14 @@ EncodingConverterICU::convertString(StringRef Source,
183
184
return std::error_code ();
184
185
}
185
186
186
- void EncodingConverterICU ::reset () {
187
+ void TextEncodingConverterICU ::reset () {
187
188
ucnv_reset (&*FromConvDesc);
188
189
ucnv_reset (&*ToConvDesc);
189
190
}
190
191
191
192
#elif HAVE_ICONV
192
- class EncodingConverterIconv : public details ::EncodingConverterImplBase {
193
+ class TextEncodingConverterIconv
194
+ : public details::TextEncodingConverterImplBase {
193
195
class UniqueIconvT {
194
196
iconv_t ConvDesc;
195
197
@@ -216,7 +218,7 @@ class EncodingConverterIconv : public details::EncodingConverterImplBase {
216
218
UniqueIconvT ConvDesc;
217
219
218
220
public:
219
- EncodingConverterIconv (UniqueIconvT ConvDesc)
221
+ TextEncodingConverterIconv (UniqueIconvT ConvDesc)
220
222
: ConvDesc(std::move(ConvDesc)) {}
221
223
222
224
std::error_code convertString (StringRef Source,
@@ -230,8 +232,8 @@ class EncodingConverterIconv : public details::EncodingConverterImplBase {
230
232
// insufficient buffer size. In the future, it would better to save the partial
231
233
// result and redo the conversion for the remaining string.
232
234
std::error_code
233
- EncodingConverterIconv ::convertString (StringRef Source,
234
- SmallVectorImpl<char > &Result) {
235
+ TextEncodingConverterIconv ::convertString (StringRef Source,
236
+ SmallVectorImpl<char > &Result) {
235
237
// Setup the output. We directly write into the SmallVector.
236
238
size_t Capacity = Result.capacity ();
237
239
char *Output = static_cast <char *>(Result.data ());
@@ -289,15 +291,15 @@ EncodingConverterIconv::convertString(StringRef Source,
289
291
return std::error_code ();
290
292
}
291
293
292
- void EncodingConverterIconv ::reset () {
294
+ void TextEncodingConverterIconv ::reset () {
293
295
iconv (ConvDesc, nullptr , nullptr , nullptr , nullptr );
294
296
}
295
297
296
298
#endif // HAVE_ICONV
297
299
} // namespace
298
300
299
- ErrorOr<EncodingConverter> EncodingConverter::create (TextEncoding CPFrom,
300
- TextEncoding CPTo) {
301
+ ErrorOr<TextEncodingConverter>
302
+ TextEncodingConverter::create (TextEncoding CPFrom, TextEncoding CPTo) {
301
303
302
304
// Text encodings should be distinct.
303
305
if (CPFrom == CPTo)
@@ -311,16 +313,17 @@ ErrorOr<EncodingConverter> EncodingConverter::create(TextEncoding CPFrom,
311
313
else
312
314
return std::error_code (errno, std::generic_category ());
313
315
314
- return EncodingConverter (
315
- std::make_unique<EncodingConverterTable >(Conversion));
316
+ return TextEncodingConverter (
317
+ std::make_unique<TextEncodingConverterTable >(Conversion));
316
318
}
317
319
318
- ErrorOr<EncodingConverter> EncodingConverter ::create (StringRef From,
319
- StringRef To) {
320
+ ErrorOr<TextEncodingConverter> TextEncodingConverter ::create (StringRef From,
321
+ StringRef To) {
320
322
std::optional<TextEncoding> FromEncoding = getKnownEncoding (From);
321
323
std::optional<TextEncoding> ToEncoding = getKnownEncoding (To);
322
324
if (FromEncoding && ToEncoding) {
323
- ErrorOr<EncodingConverter> Converter = create (*FromEncoding, *ToEncoding);
325
+ ErrorOr<TextEncodingConverter> Converter =
326
+ create (*FromEncoding, *ToEncoding);
324
327
if (Converter)
325
328
return Converter;
326
329
}
@@ -334,15 +337,16 @@ ErrorOr<EncodingConverter> EncodingConverter::create(StringRef From,
334
337
if (U_FAILURE (EC)) {
335
338
return std::error_code (errno, std::generic_category ());
336
339
}
337
- std::unique_ptr<details::EncodingConverterImplBase > Converter =
338
- std::make_unique<EncodingConverterICU >(std::move (FromConvDesc),
339
- std::move (ToConvDesc));
340
- return EncodingConverter (std::move (Converter));
340
+ std::unique_ptr<details::TextEncodingConverterImplBase > Converter =
341
+ std::make_unique<TextEncodingConverterICU >(std::move (FromConvDesc),
342
+ std::move (ToConvDesc));
343
+ return TextEncodingConverter (std::move (Converter));
341
344
#elif HAVE_ICONV
342
345
iconv_t ConvDesc = iconv_open (To.str ().c_str (), From.str ().c_str ());
343
346
if (ConvDesc == (iconv_t )-1 )
344
347
return std::error_code (errno, std::generic_category ());
345
- return EncodingConverter (std::make_unique<EncodingConverterIconv>(ConvDesc));
348
+ return TextEncodingConverter (
349
+ std::make_unique<TextEncodingConverterIconv>(ConvDesc));
346
350
#else
347
351
return std::make_error_code (std::errc::invalid_argument);
348
352
#endif
0 commit comments