+ /*
+ * See the relevant comments in cpstripped() to explain what's
+ * going on here; we want to handle the case where we get
+ * characters that mbtowc() cannot handle
+ */
+
+ if (char_len < 0) {
+ altstr = "?";
+ char_len = mbtowc(&wide_char, altstr, 1);
+ }
+
+ if (char_len <= 0) {
+ break;
+ }
+
+ w = wcwidth(wide_char);
+
+ /* If w > remaining, w must be positive. */
+ if (w > remaining) {
+ break;
+ }
+
+ end -= char_len;
+
+ if (iswcntrl(wide_char) || iswspace(wide_char)) {
+ sp += char_len;
+#else
+ int c;
+ end--;
+ /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
+ intentionally warns if they are passed a char. */
+ c = (unsigned char) *sp;
+ if (iscntrl(c) || isspace(c)) {
+ sp++;
+#endif
+ if (!prevCtrl) {
+ charstring_push_back (dest, ' ');
+ remaining--;
+ }
+
+ prevCtrl = 1;
+ continue;
+ }
+ prevCtrl = 0;
+
+#ifdef MULTIBYTE_SUPPORT
+ if (w >= 0 && remaining >= w) {
+ charstring_push_back_chars (dest, altstr ? altstr : sp,
+ char_len, w);
+ remaining -= w;
+ altstr = NULL;
+ }
+ sp += char_len;
+#else
+ charstring_push_back (dest, *sp++);
+ remaining--;
+#endif
+ }
+ }
+
+ if (rjust) {
+ if (remaining > 0) {
+ /* copy string to the right */
+ charstring_t copy = charstring_copy (dest);
+
+ /* add padding at the beginning */
+ charstring_clear (dest);
+ for (; remaining > 0; --remaining) {
+ charstring_push_back (dest, fill);
+ }
+
+ charstring_append (dest, copy);
+
+ charstring_free (copy);
+ }
+ } else {
+ /* pad remaining space */
+ while (remaining-- > 0) {
+ charstring_push_back (dest, fill);
+ }
+ }