cpstripped: Add braces to make code more legible.
[mmh] / sbr / fmt_scan.c
index 37bf16a..ca7f5e8 100644 (file)
@@ -188,26 +188,59 @@ cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
 static void
 cpstripped(char **start, char *end, char *str)
 {
+#ifdef MULTIBYTE_SUPPORT
+       int char_len;  /* bytes in current character */
+       int nbytes;
+       wchar_t wide_char;
+#else
        int c;
+#endif
        char *s = str;
 
        if (!s)
                return;
 
        /* skip any initial control characters or spaces */
+#ifdef MULTIBYTE_SUPPORT
+       nbytes = end - *start + 1;
+       mbtowc(NULL, NULL, 0); /* reset shift state */
+       while ((char_len = mbtowc(&wide_char, s, nbytes)) > 0 && (iswcntrl(wide_char) || iswspace(wide_char))) {
+               s += char_len;
+               nbytes -= char_len;
+       }
+#else
        while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
                s++;
+#endif
 
        /* compact repeated control characters and spaces into a single space */
-       while((c = (unsigned char) *s++) && *start < end)
-               if (!iscntrl(c) && !isspace(c))
+#ifdef MULTIBYTE_SUPPORT
+       while ((char_len = mbtowc(&wide_char, s, nbytes)) > 0 && *start < end) {
+               if (!iswcntrl(wide_char) && !iswspace(wide_char)) {
+                       strncpy(*start, s, char_len);
+                       s += char_len;
+                       *start += char_len;
+                       nbytes -= char_len;
+               } else {
+                       while ((char_len = mbtowc(&wide_char, s, nbytes)) > 0 && (iswcntrl(wide_char) || iswspace(wide_char))) {
+                               s += char_len;
+                               nbytes -= char_len;
+                       }
+                       *(*start)++ = ' ';
+               }
+       }
+#else
+       while((c = (unsigned char) *s++) && *start < end) {
+               if (!iscntrl(c) && !isspace(c)) {
                        *(*start)++ = c;
-               else {
+               } else {
                        while ((c = (unsigned char) *s) &&
                                        (iscntrl(c) || isspace(c)))
                                s++;
                        *(*start)++ = ' ';
                }
+       }
+#endif
 }
 
 static char *lmonth[] = {
@@ -882,17 +915,4 @@ finished:;
                *cp++ = '\n';
        *cp = '\0';
        return ((struct format *)0);
-
-#ifdef JLR
-       /* I'll remove this as soon as I understand what it does. --meillo */
-       if (cp[-1] != '\n')
-               *cp++ = '\n';
-       while (fmt->f_type != FT_DONE)
-               fmt++;
-
-       finished:;
-       *cp = '\0';
-       return (fmt->f_value ? ++fmt : (struct format *) 0);
-#endif /* JLR */
-
 }