]> diplodocus.org Git - nmh/blobdiff - test/getcwidth.c
m_getfld.c: Remove Ungetc()'s `c' parameter.
[nmh] / test / getcwidth.c
index 264d5831de3f2b94d6732a5de0b6b94ab55750c3..d389cb4855c8056f50edd46c199fe8342ff9cc01 100644 (file)
 #ifdef MULTIBYTE_SUPPORT
 #include <locale.h>
 #include <wchar.h>
+#include <wctype.h>
 #endif
 
 #ifdef MULTIBYTE_SUPPORT
 static void usage(char *);
 static void dumpwidth(void);
+static void dumpctype(void);
 static void getwidth(const char *);
 #endif /* MULTIBYTE_SUPPORT */
 
@@ -57,6 +59,15 @@ main(int argc, char *argv[])
                }
        }
 
+       if (strcmp(argv[1], "--ctype") == 0) {
+               if (argc != 2) {
+                       fprintf(stderr, "--ctype cannot be combined with other arguments\n");
+                       exit(1);
+               }
+               dumpctype();
+               exit(0);
+       }
+
        /*
         * Process each argument.  If it begins with "U+", then try to
         * convert it to a Unicode codepoint.  Otherwise, take each
@@ -144,22 +155,26 @@ getwidth(const char *string)
        printf("%d\n", length);
 }
 
+typedef struct {
+       wchar_t min, max;
+} unicode_range;
+
+static unicode_range range[] = {
+       /* https://en.wikipedia.org/wiki/Unicode#Code_point_planes_and_blocks */
+       {  L'\x0000',  L'\xffff' },
+       { L'\x10000', L'\x14fff' },
+       { L'\x16000', L'\x18fff' },
+       { L'\x1b000', L'\x1bfff' },
+       { L'\x1d000', L'\x1ffff' },
+       { L'\x20000', L'\x2ffff' },
+       { L'\xe0000', L'\xe0fff' },
+       { L'\0', L'\0' }, /* Terminates list. */
+};
+
 static void
 dumpwidth(void)
 {
-       static struct {
-               wchar_t min, max;
-       } range[] = {
-               /* https://en.wikipedia.org/wiki/Unicode#Code_point_planes_and_blocks */
-               {  L'\x0000',  L'\xffff' },
-               { L'\x10000', L'\x14fff' },
-               { L'\x16000', L'\x18fff' },
-               { L'\x1b000', L'\x1bfff' },
-               { L'\x1d000', L'\x1ffff' },
-               { L'\x20000', L'\x2ffff' },
-               { L'\xe0000', L'\xe0fff' },
-               { L'\0', L'\0' }, /* Terminates list. */
-       }, *r;
+       unicode_range *r;
        int first;
        wchar_t wc, start;
        int width, lastwidth;
@@ -185,5 +200,31 @@ dumpwidth(void)
                                           (unsigned long int)wc, lastwidth);
                }
        }
+}
+
+static void
+dumpctype(void)
+{
+       unicode_range *r;
+       wchar_t wc;
+
+       for (r = range; r->max; r++) {
+               for (wc = r->min; wc <= r->max; wc++) {
+                       printf("%6x  %2d  %c%c%c%c%c%c%c%c%c%c%c%c\n",
+                               wc, wcwidth(wc),
+                               iswcntrl(wc) ? 'c' : '-',
+                               iswprint(wc) ? 'p' : '-',
+                               iswgraph(wc) ? 'g' : '-',
+                               iswalpha(wc) ? 'a' : '-',
+                               iswupper(wc) ? 'u' : '-',
+                               iswlower(wc) ? 'l' : '-',
+                               iswdigit(wc) ? 'd' : '-',
+                               iswxdigit(wc) ? 'x' : '-',
+                               iswalnum(wc) ? 'N' : '-',
+                               iswpunct(wc) ? '@' : '-',
+                               iswspace(wc) ? 's' : '-',
+                               iswblank(wc) ? 'b' : '-');
+               }
+       }
 #endif /* MULTIBYTE_SUPPORT */
 }