+#define WORKAROUND_ICONV_BUG 1
+#if WORKAROUND_ICONV_BUG
+ // Due to a bug in some iconv versions, when the last char
+ // in the buffer is a wide char and the output encoding is
+ // ISO-2022-JP and we are going to switch to another encoding,
+ // the appropriate escape sequence for changing the character
+ // set is not output (see bugs 5216, 5280, and also 5489).
+ // As a workaround, we append a nul char in order to force
+ // a switch to ASCII, and then remove it from output after
+ // the conversion.
+ intern_type * from_new = 0;
+ intern_type const * from_old = from;
+ size_t extra = 0;
+ if (*(from_end - 1) >= 0x80 && encoding_ == "ISO-2022-JP") {
+ size_t len = from_end - from;
+ from_new = new intern_type[len + 1];
+ memcpy(from_new, from, len * sizeof(intern_type));
+ from_new[len] = 0;
+ from_end = from_new + len + 1;
+ from = from_new;
+ extra = 1;
+ }
+#endif