@@ -57,10 +57,9 @@ static std::optional<text_encoding::id> getKnownCharSet(StringRef CSName) {
57
57
return std::nullopt;
58
58
}
59
59
60
- #if defined(HAVE_ICONV) || defined(HAVE_ICU)
61
- static void HandleOverflow (size_t &Capacity, char *&Output,
62
- size_t &OutputLength,
63
- SmallVectorImpl<char > &Result) {
60
+ LLVM_ATTRIBUTE_UNUSED static void
61
+ HandleOverflow (size_t &Capacity, char *&Output, size_t &OutputLength,
62
+ SmallVectorImpl<char > &Result) {
64
63
// No space left in output buffer. Double the size of the underlying
65
64
// memory in the SmallVectorImpl, adjust pointer and length and continue
66
65
// the conversion.
@@ -72,7 +71,6 @@ static void HandleOverflow(size_t &Capacity, char *&Output,
72
71
Output = static_cast <char *>(Result.data ());
73
72
OutputLength = Capacity;
74
73
}
75
- #endif
76
74
77
75
namespace {
78
76
enum ConversionType {
@@ -290,8 +288,8 @@ void CharSetConverterIconv::reset() {
290
288
#endif // HAVE_ICONV
291
289
} // namespace
292
290
293
- CharSetConverter CharSetConverter::create (text_encoding::id CPFrom,
294
- text_encoding::id CPTo) {
291
+ ErrorOr< CharSetConverter> CharSetConverter::create (text_encoding::id CPFrom,
292
+ text_encoding::id CPTo) {
295
293
296
294
assert (CPFrom != CPTo && " Text encodings should be distinct" );
297
295
@@ -302,19 +300,22 @@ CharSetConverter CharSetConverter::create(text_encoding::id CPFrom,
302
300
CPTo == text_encoding::id::UTF8)
303
301
Conversion = IBM1047ToUTF8;
304
302
else
305
- llvm_unreachable (" Invalid ConversionType!" );
303
+ return std::error_code (errno, std::generic_category ());
304
+
306
305
std::unique_ptr<details::CharSetConverterImplBase> Converter =
307
306
std::make_unique<CharSetConverterTable>(Conversion);
308
-
309
307
return CharSetConverter (std::move (Converter));
310
308
}
311
309
312
310
ErrorOr<CharSetConverter> CharSetConverter::create (StringRef CSFrom,
313
311
StringRef CSTo) {
314
312
std::optional<text_encoding::id> From = getKnownCharSet (CSFrom);
315
313
std::optional<text_encoding::id> To = getKnownCharSet (CSTo);
316
- if (From && To)
317
- return create (*From, *To);
314
+ if (From && To) {
315
+ ErrorOr<CharSetConverter> Converter = create (*From, *To);
316
+ if (Converter)
317
+ return Converter;
318
+ }
318
319
#ifdef HAVE_ICU
319
320
UErrorCode EC = U_ZERO_ERROR;
320
321
UConverterUniquePtr FromConvDesc (ucnv_open (CSFrom.str ().c_str (), &EC));
0 commit comments