-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-utf8.c
230 lines (198 loc) · 5.14 KB
/
test-utf8.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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/** @file test-utf8.c
*
* @author Juuso Alasuutari
*/
#include <errno.h>
#include <inttypes.h>
#define PROGNAME "test-utf8"
#define SYNOPSIS "[OPTION]... [--] [STRING]..."
#define PURPOSE "Inspect UTF-8 encoded strings"
#define OPTIONS(X) \
X(boolean, help, 'h', "help", \
"print this help text and exit") \
\
X(boolean, bytes, 'b', "bytes", \
"count bytes inside valid UTF-8") \
\
X(boolean, chars, 'c', "chars", \
"count valid unicode characters") \
\
X(boolean, join, 'j', "join", \
"treat arguments as one string") \
\
X(boolean, print, 'p', "print", \
"print strings even when counting") \
\
X(boolean, skip, 's', "skip", \
"skip invalid UTF-8, don't replace") \
\
X(boolean, quiet, 'q', "quiet", \
"report invalid UTF-8 via exit code")
#define DETAILS \
"The default behaviour without option arguments is to\n" \
"print input strings on different lines, substituting\n" \
"the replacement symbol U+FFFD for invalid sequences."
#include "letopt.h"
#undef DETAILS
#undef OPTIONS
#undef PURPOSE
#undef SYNOPSIS
#undef PROGNAME
#include "dbg.h"
#include "utf8.h"
static const_inline size_t
saturated_add_uz (size_t a,
size_t b)
{
if ((a += b) < b)
a = SIZE_MAX;
return a;
}
static struct uz2 {
size_t max;
size_t sum;
} arg_sizes (struct letopt const *opt)
{
struct uz2 ret = {0};
for (int i = 0; i < letopt_nargs(opt); ++i) {
size_t n = strlen(letopt_arg(opt, i));
ret.sum = saturated_add_uz(ret.sum, n);
if (n > ret.max)
ret.max = n;
}
return ret;
}
static int
arg_conflict (struct letopt *opt);
int
main (int c,
char **v)
{
struct letopt opt = letopt_init(c, v);
if (arg_conflict(&opt))
return letopt_fini(&opt);
if (letopt_nargs(&opt) < 1 || opt.m_help)
letopt_helpful_exit(&opt);
struct uz2 sz = arg_sizes(&opt);
size_t buf_sz = opt.m_join ? sz.sum : sz.max;
buf_sz = saturated_add_uz(
saturated_add_uz(buf_sz, 2U),
saturated_add_uz(buf_sz, buf_sz));
if (buf_sz == SIZE_MAX) {
pr_err_("%s", strerror(EOVERFLOW));
(void)letopt_fini(&opt);
return EXIT_FAILURE;
}
char *buf = malloc(buf_sz);
if (!buf) {
pr_errno_(errno, "malloc(%zu)", buf_sz);
(void)letopt_fini(&opt);
return EXIT_FAILURE;
}
const bool count = opt.m_bytes || opt.m_chars;
const bool print = !opt.m_quiet && (opt.m_print || !count);
size_t n_chars = 0, n_bytes = 0;
struct utf8 u8p = utf8();
char *out = buf;
for (int i = 0; i < letopt_nargs(&opt); ++i) {
uint8_t const *p = (uint8_t const *)letopt_arg(&opt, i);
for (uint8_t const *q = p; *q; q = p) {
p = utf8_parse_next_code_point(&u8p, q);
if (u8p.error) {
if (print &&
!opt.m_skip && (*p || !opt.m_join)) {
*out++ = (char)(unsigned char)0xefU;
*out++ = (char)(unsigned char)0xbfU;
*out++ = (char)(unsigned char)0xbdU;
}
if (!*p)
break;
if (p == q && utf8_expects_leading_byte(&u8p))
++p;
utf8_reset(&u8p);
continue;
}
if (opt.m_bytes)
n_bytes += utf8_size(&u8p);
if (opt.m_chars)
n_chars += 1U;
if (print) {
for (char const *s = utf8_result(&u8p); *s;) {
*out++ = *s++;
}
}
}
if (!opt.m_join) {
if (print) {
if (out == buf || *(out - 1) != '\n')
*out++ = '\n';
*out = '\0';
out = buf;
if (!count)
(void)fputs(buf, stdout);
else if (!opt.m_bytes || !opt.m_chars)
(void)printf("%zu\t%s", opt.m_bytes
? n_bytes : n_chars, buf);
else
(void)printf("%zu\t%zu\t%s", n_chars,
n_bytes, buf);
} else if (count) {
if (!opt.m_bytes || !opt.m_chars)
(void)printf("%zu\n", opt.m_bytes
? n_bytes : n_chars);
else
(void)printf("%zu\t%zu\n", n_chars,
n_bytes);
}
n_chars = 0;
n_bytes = 0;
utf8_reset(&u8p);
}
}
if (opt.m_join) {
if (print) {
if (out == buf || *(out - 1) != '\n')
*out++ = '\n';
*out = '\0';
if (!count)
(void)fputs(buf, stdout);
else if (!opt.m_bytes || !opt.m_chars)
(void)printf("%zu\t%s", opt.m_bytes
? n_bytes : n_chars, buf);
else
(void)printf("%zu\t%zu\t%s", n_chars,
n_bytes, buf);
} else if (count) {
if (!opt.m_bytes || !opt.m_chars)
(void)printf("%zu\n", opt.m_bytes
? n_bytes : n_chars);
else
(void)printf("%zu\t%zu\n", n_chars,
n_bytes);
}
}
free(buf);
return letopt_fini(&opt);
}
static int
arg_conflict (struct letopt *opt)
{
bool count = opt->m_bytes || opt->m_chars;
int e = 0;
if (opt->m_quiet) {
if (count) {
pr_err_("can't count quietly");
e = EINVAL;
}
if (opt->m_print) {
pr_err_("can't print quietly");
e = EINVAL;
}
}
if (opt->m_skip &&
(opt->m_quiet || (!opt->m_print && count)))
pr_wrn_("skip option ignored");
opt->p.e = e;
return e;
}