Skip to content

Commit

Permalink
utf8.c: Replace elseif ...elseif by switch()
Browse files Browse the repository at this point in the history
The previous two commits paved the way for this, which also helps with
future commits.
  • Loading branch information
khwilliamson committed Nov 21, 2024
1 parent 551ac51 commit a9374ce
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
75 changes: 52 additions & 23 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,12 +1690,18 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
char * message = NULL;
U32 this_flag_bit = 0;

/* Each 'if' clause handles one problem. They are ordered so that
* the first ones' messages will be displayed before the later
* ones; this is kinda in decreasing severity order. But the
* overlong must come last, as it changes 'uv' looked at by the
* others */
if (possible_problems & UTF8_GOT_OVERFLOW) {
/* Each 'case' handles one problem given by a bit in
* 'possible_problems'. The lowest bit positions, as #defined in
* utf8.h, are are handled first. Some of the ordering is
* important so that higher priority items are done before lower
* ones; some of which may depend on earlier actions. Also the
* ordering tries to cause any messages to be displayed in kind of
* decreasing severity order. But the overlong must come last, as
* it changes 'uv' looked at by the others */

U32 this_problem = 1U << lsbit_pos32(possible_problems);
switch (this_problem) {
case UTF8_GOT_OVERFLOW:

/* Overflow means also got a super and are using Perl's
* extended UTF-8, but we handle all three cases here */
Expand Down Expand Up @@ -1747,8 +1753,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
}
}
}
}
else if (possible_problems & UTF8_GOT_EMPTY) {

break;

case UTF8_GOT_EMPTY:
possible_problems &= ~UTF8_GOT_EMPTY;
*errors |= UTF8_GOT_EMPTY;

Expand All @@ -1769,8 +1777,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
this_flag_bit = UTF8_GOT_EMPTY;
}
}
}
else if (possible_problems & UTF8_GOT_CONTINUATION) {

break;

case UTF8_GOT_CONTINUATION:
possible_problems &= ~UTF8_GOT_CONTINUATION;
*errors |= UTF8_GOT_CONTINUATION;

Expand All @@ -1788,8 +1798,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
this_flag_bit = UTF8_GOT_CONTINUATION;
}
}
}
else if (possible_problems & UTF8_GOT_SHORT) {

break;

case UTF8_GOT_SHORT:
possible_problems &= ~UTF8_GOT_SHORT;
*errors |= UTF8_GOT_SHORT;

Expand All @@ -1810,8 +1822,9 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
}
}

}
else if (possible_problems & UTF8_GOT_NON_CONTINUATION) {
break;

case UTF8_GOT_NON_CONTINUATION:
possible_problems &= ~UTF8_GOT_NON_CONTINUATION;
*errors |= UTF8_GOT_NON_CONTINUATION;

Expand All @@ -1836,8 +1849,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
this_flag_bit = UTF8_GOT_NON_CONTINUATION;
}
}
}
else if (possible_problems & UTF8_GOT_SURROGATE) {

break;

case UTF8_GOT_SURROGATE:
possible_problems &= ~UTF8_GOT_SURROGATE;

if (flags & UTF8_WARN_SURROGATE) {
Expand Down Expand Up @@ -1867,8 +1882,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
disallowed = TRUE;
*errors |= UTF8_GOT_SURROGATE;
}
}
else if (possible_problems & UTF8_GOT_SUPER) {

break;

case UTF8_GOT_SUPER:
possible_problems &= ~UTF8_GOT_SUPER;

if (flags & UTF8_WARN_SUPER) {
Expand Down Expand Up @@ -1942,8 +1959,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
*errors |= UTF8_GOT_SUPER;
disallowed = TRUE;
}
}
else if (possible_problems & UTF8_GOT_NONCHAR) {

break;

case UTF8_GOT_NONCHAR:
possible_problems &= ~UTF8_GOT_NONCHAR;

if (flags & UTF8_WARN_NONCHAR) {
Expand All @@ -1967,8 +1986,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
disallowed = TRUE;
*errors |= UTF8_GOT_NONCHAR;
}
}
else if (possible_problems & UTF8_GOT_LONG) {

break;

case UTF8_GOT_LONG:
possible_problems &= ~UTF8_GOT_LONG;
*errors |= UTF8_GOT_LONG;

Expand Down Expand Up @@ -2033,7 +2054,15 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
this_flag_bit = UTF8_GOT_LONG;
}
}
} /* End of looking through the possible flags */

break;

default:
Perl_croak(aTHX_ "panic: Unexpected case value in "
" utf8n_to_uvchr_msgs() %d", this_problem);
/* NOTREACHED */

} /* End of switch() on the possible problems */

/* Display the message (if any) for the problem being handled in
* this iteration of the loop */
Expand Down
2 changes: 2 additions & 0 deletions utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,8 @@ point's representation.
/* Largest code point we accept from external sources */
#define MAX_LEGAL_CP ((UV)IV_MAX)

/* The ordering of these bits is important to a switch() statement in utf8.c
* for handling problems in converting UTF-8 to a UV */
#define UTF8_ALLOW_OVERFLOW 0x0001
#define UTF8_GOT_OVERFLOW UTF8_ALLOW_OVERFLOW

Expand Down

0 comments on commit a9374ce

Please sign in to comment.