-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcwidth-test.c
231 lines (210 loc) · 5.06 KB
/
wcwidth-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* gcc -std=c99 -o wcwidth-test wcwidth-test.c
*/
#define _XOPEN_SOURCE // wcwidth
#include <features.h>
#include <stdint.h>
#include <endian.h>
#include <wchar.h> // wcwidth
#include <locale.h>
#include <stdio.h>
#include <stdlib.h> // strtoul
#include <memory.h>
typedef uint32_t u32;
typedef uint8_t u8;
typedef uint16_t u16;
#define VT0 "\033[0m"
#define VTRR "\033[31m"
#define VTGG "\033[32m"
///////////////////////////////////////////////////////////////
static uint16_t load_le16 (const void *src)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
return *(const uint16_t *)src;
#else // __BIG_ENDIAN
const uint8_t *p;
p = (const uint8_t *)src;
return p[1]<<8 | p[0];
#endif
}
static void *store_le16 (void *dst, unsigned val)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
*(u16 *)dst = (u16)val;
return (u16 *)dst +1;
#else // __BIG_ENDIAN
u8 *p;
p = (u8 *)dst;
*p++ = (u8)val;
*p++ = (u8)(0xFF & val >> 8);
return p;
#endif
}
///////////////////////////////////////////////////////////////
static unsigned utf16to8 (void *retval, wchar_t w)
{
u8 *dst;
dst = (u8 *)retval;
u16 u;
memcpy (&u, &w, 2);
if (u < 0x80)
*dst++ = (u8)u;
else if (u < 0x800) { // C0..DF 80..BF
*dst++ = (u8)(0xC0 | 0x1F & u >> 6);
*dst++ = (u8)(0x80 | 0x3F & u);
}
else { // E0..EF 80..BF 80..BF
*dst++ = (u8)(0xE0 | 0x0F & u >> 12);
*dst++ = (u8)(0x80 | 0x3F & u >> 6);
*dst++ = (u8)(0x80 | 0x3F & u);
}
return dst - (u8 *)retval;
}
static unsigned utf8len (const void *utf8)
{
if (!utf8) return 0;
u8 c;
c = *(const u8 *)utf8;
return (c < 0xC0) ? 1 : (c < 0xE0) ? 2 : (c < 0xF0) ? 3 : 1;
}
static unsigned utf8to16 (wchar_t *retval, const void *utf8, unsigned len)
{
const u8 *src, *end;
u8 *dst;
u16 w;
u8 c;
src = (const u8 *)utf8;
end = src + len;
dst = (u8 *)retval;
for (; src < end && !('\0' == (c = src[0])); ++src) {
if (c < 0x80)
w = c;
else if (c < 0xC0)
w = 0x003F; // '?'
else if (c < 0xE0) {
if (src[1] < 0x80 || 0xBF < src[1])
w = 0xFF1F; // '?'
else
w = (u16)(0xFC0 & c << 6 | 0x3F & src[1]);
--len;
++src;
}
else if (c < 0xF0) {
if (src[1] < 0x80 || 0xBF < src[1] || src[2] < 0x80 || 0xBF < src[2])
w = 0xFF1F; // '?'
else
w = (u16)(0xF000 & c << 12 | 0xFC0 & src[1] << 6 | 0x3F & src[2]);
len -= 2;
src += 2;
}
else {
w = 0xFF1F; // '?'
len -= 2;
src += 2;
}
store_le16 (dst, w);
dst += 2;
}
return (wchar_t *)dst - retval;
}
///////////////////////////////////////////////////////////////
static void test_wcwidth_utf16 (wchar_t w)
{
u16 unicode;
memcpy (&unicode, &w, 2);
u8 utf8[3];
unsigned len, i;
len = utf16to8 (utf8, w);
for (i = 0; i < len; ++i)
printf ("%02X ", *(const u8 *)(utf8 +i));
for (; i < 3; ++i)
printf (" ");
printf ("-> %04X -> " VTRR "%d" VT0 " : '%s'" "\n", unicode, wcwidth (w), utf8);
}
enum {
OPT_HEX = 1,
OPT_UTF16 = 2,
};
int main (int argc, char *argv[])
{
if (argc < 2) {
printf ("usage: [-h|--hex] [-w|--utf16] [-l|--locale <locale-name>] [-|<char> <char>...]" "\n");
return -1;
}
FILE *fp; unsigned flags, errcount;
fp = NULL, flags = errcount = 0;
const char *locale; int i;
for (locale = "", i = 1; i < argc; ++i) {
const char *opt;
if (! ('-' == (opt = argv[i])[0]))
continue;
if ('\0' == opt[1])
fp = stdin;
else if (0 == strcmp ("-h", opt) || 0 == strcmp ("--hex", opt))
flags |= OPT_HEX;
else if (0 == strcmp ("-w", opt) || 0 == strcmp ("--utf16", opt))
flags |= OPT_UTF16;
else if (i +1 < argc && (0 == strcmp ("-l", opt) || 0 == strcmp ("--locale", opt)))
locale = argv[++i];
else {
fprintf (stderr, "'%s': " VTRR "invalid option" VT0 "\n", opt);
++errcount;
}
}
if (0 < errcount)
return -1;
setlocale (LC_ALL, locale);
printf (VTGG "UTF-8 UTF-16 wcwidth()" VT0 "\n");
i = 1;
while (1) {
const char *src; char linebuf[128];
if (!fp) {
if (! (i < argc))
break;
src = argv[i++];
if ('-' == src[0])
continue;
}
else {
if (feof (fp))
break;
*linebuf = '\0';
fgets (linebuf, sizeof(linebuf), fp);
linebuf[sizeof(linebuf) -1] = '\0';
char *tail;
tail = strchr (linebuf, '\0');
while (linebuf < tail && strchr (" \t\r\n", tail[-1]))
*--tail = '\0';
if (! (linebuf < tail))
continue;
src = linebuf;
while (src < tail && strchr (" \t", *src))
++src;
}
wchar_t w;
if (OPT_HEX & flags && OPT_UTF16 & flags) {
char *endptr;
w = (wchar_t)strtoul (src, &endptr, 16);
}
else if (OPT_HEX & flags && !(OPT_UTF16 & flags)) {
u32 utf8; char *endptr;
utf8 = (u32)strtoul (src, &endptr, 16);
unsigned len; u8 tmp[3];
if (! (0xFFFF & utf8))
len = 1, tmp[0] = (u8)(0xFF & utf8);
else if (! (0xFF & utf8))
len = 2, tmp[0] = (u8)(0xFF & utf8 >> 8), tmp[1] = (u8)(0xFF & utf8);
else
len = 3, tmp[0] = (u8)(0xFF & utf8 >> 16), tmp[1] = (u8)(0xFF & utf8 >> 8), tmp[2] = (u8)(0xFF & utf8);
utf8to16 (&w, tmp, len);
}
else if (!(OPT_HEX & flags) && OPT_UTF16 & flags)
w = (wchar_t)load_le16 (src);
else /*if (!(OPT_HEX & flags) && !(OPT_UTF16 & flags))*/ {
unsigned len;
len = utf8len (src);
utf8to16 (&w, src, len);
}
test_wcwidth_utf16 (w);
}
return 0;
}