Skip to content

Commit 2a00b11

Browse files
committed
utf8_to_uv_msgs: Create a common macro
Previous commits have allowed the beginning of several of the case statements in this switch() to have the same code. This commit creates a macro encapsulating that code and changes the cases to use it. The macro continues the enclosing loop if no message needs to be generated. This allows the removal of various conditional blocks. And it means that these conditions don't break to the bottom of the switch() if no message is needed. Braces are needed in one case: so as to not run afoul of C++ initialization crossing
1 parent 3aca733 commit 2a00b11

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

utf8.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,54 +2177,50 @@ Perl_utf8_to_uv_msgs_helper_(const U8 * const s0,
21772177
/* NOTREACHED */
21782178
break;
21792179

2180+
/* If this condition is allowed, no message is to be generated. Similarly, if
2181+
* warnings for it aren't enabled. All of these are controlled only by 'utf8'
2182+
* warnings. This macro relies on the GOT and ACCEPT flags being identical. */
2183+
#define COMMON_DEFAULT_REJECTS(p1, p2) \
2184+
if ( (! (this_problem & rejects)) \
2185+
|| ((pack_warn = PACK_WARN(WARN_UTF8,p1,p2)) == 0)) \
2186+
{ \
2187+
continue; \
2188+
} \
2189+
21802190
case UTF8_GOT_EMPTY:
2181-
if (! (flags & UTF8_ALLOW_EMPTY)) {
2191+
COMMON_DEFAULT_REJECTS(,);
21822192

21832193
/* This so-called malformation is now treated as a bug in
21842194
* the caller. If you have nothing to decode, skip calling
21852195
* this function */
21862196
assert(0);
2187-
2188-
if (PACK_WARN(WARN_UTF8,,)) {
21892197
message = Perl_form(aTHX_ "%s (empty string)",
21902198
malformed_text);
2191-
}
2192-
}
2193-
21942199
break;
21952200

21962201
case UTF8_GOT_CONTINUATION:
2197-
if (! (flags & UTF8_ALLOW_CONTINUATION)) {
2198-
if (PACK_WARN(WARN_UTF8,,)) {
2202+
COMMON_DEFAULT_REJECTS(,);
21992203
message = Perl_form(aTHX_
22002204
"%s: %s (unexpected continuation byte 0x%02x,"
22012205
" with no preceding start byte)",
22022206
malformed_text,
22032207
_byte_dump_string(s0, 1, 0), *s0);
2204-
}
2205-
}
2206-
22072208
break;
22082209

22092210
case UTF8_GOT_SHORT:
2210-
if (! (flags & UTF8_ALLOW_SHORT)) {
2211-
if (PACK_WARN(WARN_UTF8,,)) {
2211+
COMMON_DEFAULT_REJECTS(,);
22122212
message = Perl_form(aTHX_
22132213
"%s: %s (too short; %d byte%s available, need %d)",
22142214
malformed_text,
22152215
_byte_dump_string(s0, send - s0, 0),
22162216
(int)avail_len,
22172217
avail_len == 1 ? "" : "s",
22182218
(int)expectlen);
2219-
}
2220-
}
2221-
22222219
break;
22232220

22242221
case UTF8_GOT_NON_CONTINUATION:
2225-
if (! (flags & UTF8_ALLOW_NON_CONTINUATION)) {
2226-
if (PACK_WARN(WARN_UTF8,,)) {
2227-
2222+
{
2223+
COMMON_DEFAULT_REJECTS(,);
22282224
/* If we don't know for sure that the input length is
22292225
* valid, avoid as much as possible reading past the
22302226
* end of the buffer */
@@ -2236,10 +2232,8 @@ Perl_utf8_to_uv_msgs_helper_(const U8 * const s0,
22362232
printlen,
22372233
s - s0,
22382234
(int) expectlen));
2239-
}
2240-
}
2241-
22422235
break;
2236+
}
22432237

22442238
case UTF8_GOT_LONG:
22452239

0 commit comments

Comments
 (0)