- /*
- * If mbrtowc() failed, then we have a character that isn't valid
- * in the current encoding, or len wasn't enough for the whole
- * multi-byte rune to be read. Replace it with a '?'. We do that by
- * setting the alstr variable to the value of the replacement string;
- * altstr is used below when the bytes are copied into the output
- * buffer.
- */
- if (char_len < 0) {
- altstr = "?";
- char_len = mbtowc(&wide_char, altstr, 1);
- }
+ if (!str)
+ return; /* It's unclear why no padding in this case. */
+ end = str + strlen(str);
+
+ if (mbtowc(NULL, NULL, 0))
+ {} /* Reset shift state. */
+
+ squash = true; /* Trim `space' or `cntrl' from the start. */
+ while (max) {
+ if (!*str)
+ return; /* It's unclear why no padding in this case. */
+
+ srclen = mbtowc(&rune, str, end - str);
+ if (srclen == -1) {
+ /* Invalid rune, or not enough bytes to finish it. */
+ rune = L'?';
+ src = oddchar;
+ srclen = oddlen;
+ str++; /* Skip one byte. */
+ } else {
+ src = str;
+ str += srclen;
+ }