]>
diplodocus.org Git - nmh/blob - test/getcwidth.c
f2a2ac69e5207dfd9375644f402d42f282613f44
2 * getcwidth - Get the OS's idea of the width of a combining character
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
18 #ifdef MULTIBYTE_SUPPORT
23 static void usage(char *);
24 static void dumpwidth(void);
27 main(int argc
, char *argv
[])
34 * This is the UTF-8 for "n" + U+0308 (Combining Diaeresis)
37 unsigned char string
[] = "n\xcc\x88";
39 setlocale(LC_ALL
, "");
45 if (strcmp(argv
[1], "--dump") == 0) {
53 #ifndef MULTIBYTE_SUPPORT
54 fprintf(stderr
, "Nmh was not configured with multibyte support\n");
56 #else /* MULTIBYTE_SUPPORT */
58 * It's not clear to me that we can just call mbtowc() with a
59 * combining character; just to be safe, feed it in a base
63 mbtowc(NULL
, NULL
, 0);
65 charlen
= mbtowc(&c
, (char *) string
, strlen((char *) string
));
68 fprintf(stderr
, "We expected a beginning character length "
69 "of 1, got %d instead\n", charlen
);
73 p
= (char *) (string
+ charlen
);
75 charlen
= mbtowc(&c
, p
, strlen(p
));
78 fprintf(stderr
, "We expected a multibyte character length "
79 "of 2, got %d instead\n", charlen
);
80 fprintf(stderr
, "Are you using a UTF-8 locale?\n");
84 printf("%d\n", wcwidth(c
));
87 #endif /* MULTIBYTE_SUPPORT */
93 fprintf(stderr
, "Usage: %s [--dump]\n", argv0
);
94 fprintf(stderr
, "Returns the column width of a UTF-8 combining "
95 "multibyte character\n");
96 fprintf(stderr
, "\t--dump\tDump complete width table\n");
104 #ifndef MULTIBYTE_SUPPORT
105 fprintf(stderr
, "Nmh was not configured with multibyte support\n");
107 #else /* MULTIBYTE_SUPPORT */
109 int width
, lastwidth
;
111 for (wc
= low
= 1, lastwidth
= wcwidth(wc
); wc
<= 0xffff; wc
++) {
113 if (width
!= lastwidth
) {
114 printf("%04X - %04X = %d\n", low
, wc
- 1, lastwidth
);
120 width
= wcwidth(wc
- 1);
121 if (width
== lastwidth
)
122 printf("%04X - %04X = %d\n", low
, wc
- 1, width
);
123 #endif /* MULTIBYTE_SUPPORT */