@@ -91,13 +91,15 @@ class CharSetConverterTable : public details::CharSetConverterImplBase {
91
91
public:
92
92
CharSetConverterTable (ConversionType ConvType) : ConvType(ConvType) {}
93
93
94
- std::error_code convert (StringRef Source,
95
- SmallVectorImpl<char > &Result) const override ;
94
+ std::error_code convertString (StringRef Source,
95
+ SmallVectorImpl<char > &Result) override ;
96
+
97
+ void reset () override {}
96
98
};
97
99
98
100
std::error_code
99
- CharSetConverterTable::convert (StringRef Source,
100
- SmallVectorImpl<char > &Result) const {
101
+ CharSetConverterTable::convertString (StringRef Source,
102
+ SmallVectorImpl<char > &Result) {
101
103
if (ConvType == IBM1047ToUTF8) {
102
104
ConverterEBCDIC::convertToUTF8 (Source, Result);
103
105
return std::error_code ();
@@ -127,13 +129,15 @@ class CharSetConverterICU : public details::CharSetConverterImplBase {
127
129
: FromConvDesc(std::move(FromConverter)),
128
130
ToConvDesc (std::move(ToConverter)) {}
129
131
130
- std::error_code convert (StringRef Source,
131
- SmallVectorImpl<char > &Result) const override ;
132
+ std::error_code convertString (StringRef Source,
133
+ SmallVectorImpl<char > &Result) override ;
134
+
135
+ void reset () override ;
132
136
};
133
137
134
138
std::error_code
135
- CharSetConverterICU::convert (StringRef Source,
136
- SmallVectorImpl<char > &Result) const {
139
+ CharSetConverterICU::convertString (StringRef Source,
140
+ SmallVectorImpl<char > &Result) {
137
141
// Setup the input in case it has no backing data.
138
142
size_t InputLength = Source.size ();
139
143
const char *In = InputLength ? const_cast <char *>(Source.data ()) : " " ;
@@ -171,6 +175,11 @@ CharSetConverterICU::convert(StringRef Source,
171
175
return std::error_code ();
172
176
}
173
177
178
+ void CharSetConverterICU::reset () {
179
+ ucnv_reset (&*FromConvDesc);
180
+ ucnv_reset (&*ToConvDesc);
181
+ }
182
+
174
183
#elif defined(HAVE_ICONV)
175
184
class CharSetConverterIconv : public details ::CharSetConverterImplBase {
176
185
class UniqueIconvT {
@@ -202,13 +211,15 @@ class CharSetConverterIconv : public details::CharSetConverterImplBase {
202
211
CharSetConverterIconv (UniqueIconvT ConvDesc)
203
212
: ConvDesc(std::move(ConvDesc)) {}
204
213
205
- std::error_code convert (StringRef Source,
206
- SmallVectorImpl<char > &Result) const override ;
214
+ std::error_code convertString (StringRef Source,
215
+ SmallVectorImpl<char > &Result) override ;
216
+
217
+ void reset () override ;
207
218
};
208
219
209
220
std::error_code
210
- CharSetConverterIconv::convert (StringRef Source,
211
- SmallVectorImpl<char > &Result) const {
221
+ CharSetConverterIconv::convertString (StringRef Source,
222
+ SmallVectorImpl<char > &Result) {
212
223
// Setup the output. We directly write into the SmallVector.
213
224
size_t Capacity = Result.capacity ();
214
225
char *Output = static_cast <char *>(Result.data ());
@@ -262,10 +273,14 @@ CharSetConverterIconv::convert(StringRef Source,
262
273
} while (true );
263
274
264
275
// Re-adjust size to actual size.
265
- Result.resize (Capacity - OutputLength );
276
+ Result.resize (Output - Result. data () );
266
277
return std::error_code ();
267
278
}
268
279
280
+ void CharSetConverterIconv::reset () {
281
+ iconv (ConvDesc, nullptr , nullptr , nullptr , nullptr );
282
+ }
283
+
269
284
#endif // HAVE_ICONV
270
285
} // namespace
271
286
@@ -281,8 +296,7 @@ CharSetConverter CharSetConverter::create(text_encoding::id CPFrom,
281
296
CPTo == text_encoding::id::UTF8)
282
297
Conversion = IBM1047ToUTF8;
283
298
else
284
- assert (false &&
285
- " Only conversions between UTF-8 and IBM-1047 are supported" );
299
+ llvm_unreachable (" Invalid ConversionType!" );
286
300
std::unique_ptr<details::CharSetConverterImplBase> Converter =
287
301
std::make_unique<CharSetConverterTable>(Conversion);
288
302
@@ -316,6 +330,7 @@ ErrorOr<CharSetConverter> CharSetConverter::create(StringRef CSFrom,
316
330
std::unique_ptr<details::CharSetConverterImplBase> Converter =
317
331
std::make_unique<CharSetConverterIconv>(ConvDesc);
318
332
return CharSetConverter (std::move (Converter));
319
- #endif
333
+ #else
320
334
return std::make_error_code (std::errc::invalid_argument);
335
+ #endif
321
336
}
0 commit comments