Skip to content

Commit a9374ce

Browse files
committed
utf8.c: Replace elseif ...elseif by switch()
The previous two commits paved the way for this, which also helps with future commits.
1 parent 551ac51 commit a9374ce

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

utf8.c

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,12 +1690,18 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
16901690
char * message = NULL;
16911691
U32 this_flag_bit = 0;
16921692

1693-
/* Each 'if' clause handles one problem. They are ordered so that
1694-
* the first ones' messages will be displayed before the later
1695-
* ones; this is kinda in decreasing severity order. But the
1696-
* overlong must come last, as it changes 'uv' looked at by the
1697-
* others */
1698-
if (possible_problems & UTF8_GOT_OVERFLOW) {
1693+
/* Each 'case' handles one problem given by a bit in
1694+
* 'possible_problems'. The lowest bit positions, as #defined in
1695+
* utf8.h, are are handled first. Some of the ordering is
1696+
* important so that higher priority items are done before lower
1697+
* ones; some of which may depend on earlier actions. Also the
1698+
* ordering tries to cause any messages to be displayed in kind of
1699+
* decreasing severity order. But the overlong must come last, as
1700+
* it changes 'uv' looked at by the others */
1701+
1702+
U32 this_problem = 1U << lsbit_pos32(possible_problems);
1703+
switch (this_problem) {
1704+
case UTF8_GOT_OVERFLOW:
16991705

17001706
/* Overflow means also got a super and are using Perl's
17011707
* extended UTF-8, but we handle all three cases here */
@@ -1747,8 +1753,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
17471753
}
17481754
}
17491755
}
1750-
}
1751-
else if (possible_problems & UTF8_GOT_EMPTY) {
1756+
1757+
break;
1758+
1759+
case UTF8_GOT_EMPTY:
17521760
possible_problems &= ~UTF8_GOT_EMPTY;
17531761
*errors |= UTF8_GOT_EMPTY;
17541762

@@ -1769,8 +1777,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
17691777
this_flag_bit = UTF8_GOT_EMPTY;
17701778
}
17711779
}
1772-
}
1773-
else if (possible_problems & UTF8_GOT_CONTINUATION) {
1780+
1781+
break;
1782+
1783+
case UTF8_GOT_CONTINUATION:
17741784
possible_problems &= ~UTF8_GOT_CONTINUATION;
17751785
*errors |= UTF8_GOT_CONTINUATION;
17761786

@@ -1788,8 +1798,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
17881798
this_flag_bit = UTF8_GOT_CONTINUATION;
17891799
}
17901800
}
1791-
}
1792-
else if (possible_problems & UTF8_GOT_SHORT) {
1801+
1802+
break;
1803+
1804+
case UTF8_GOT_SHORT:
17931805
possible_problems &= ~UTF8_GOT_SHORT;
17941806
*errors |= UTF8_GOT_SHORT;
17951807

@@ -1810,8 +1822,9 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
18101822
}
18111823
}
18121824

1813-
}
1814-
else if (possible_problems & UTF8_GOT_NON_CONTINUATION) {
1825+
break;
1826+
1827+
case UTF8_GOT_NON_CONTINUATION:
18151828
possible_problems &= ~UTF8_GOT_NON_CONTINUATION;
18161829
*errors |= UTF8_GOT_NON_CONTINUATION;
18171830

@@ -1836,8 +1849,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
18361849
this_flag_bit = UTF8_GOT_NON_CONTINUATION;
18371850
}
18381851
}
1839-
}
1840-
else if (possible_problems & UTF8_GOT_SURROGATE) {
1852+
1853+
break;
1854+
1855+
case UTF8_GOT_SURROGATE:
18411856
possible_problems &= ~UTF8_GOT_SURROGATE;
18421857

18431858
if (flags & UTF8_WARN_SURROGATE) {
@@ -1867,8 +1882,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
18671882
disallowed = TRUE;
18681883
*errors |= UTF8_GOT_SURROGATE;
18691884
}
1870-
}
1871-
else if (possible_problems & UTF8_GOT_SUPER) {
1885+
1886+
break;
1887+
1888+
case UTF8_GOT_SUPER:
18721889
possible_problems &= ~UTF8_GOT_SUPER;
18731890

18741891
if (flags & UTF8_WARN_SUPER) {
@@ -1942,8 +1959,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
19421959
*errors |= UTF8_GOT_SUPER;
19431960
disallowed = TRUE;
19441961
}
1945-
}
1946-
else if (possible_problems & UTF8_GOT_NONCHAR) {
1962+
1963+
break;
1964+
1965+
case UTF8_GOT_NONCHAR:
19471966
possible_problems &= ~UTF8_GOT_NONCHAR;
19481967

19491968
if (flags & UTF8_WARN_NONCHAR) {
@@ -1967,8 +1986,10 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
19671986
disallowed = TRUE;
19681987
*errors |= UTF8_GOT_NONCHAR;
19691988
}
1970-
}
1971-
else if (possible_problems & UTF8_GOT_LONG) {
1989+
1990+
break;
1991+
1992+
case UTF8_GOT_LONG:
19721993
possible_problems &= ~UTF8_GOT_LONG;
19731994
*errors |= UTF8_GOT_LONG;
19741995

@@ -2033,7 +2054,15 @@ Perl__utf8n_to_uvchr_msgs_helper(const U8 *s,
20332054
this_flag_bit = UTF8_GOT_LONG;
20342055
}
20352056
}
2036-
} /* End of looking through the possible flags */
2057+
2058+
break;
2059+
2060+
default:
2061+
Perl_croak(aTHX_ "panic: Unexpected case value in "
2062+
" utf8n_to_uvchr_msgs() %d", this_problem);
2063+
/* NOTREACHED */
2064+
2065+
} /* End of switch() on the possible problems */
20372066

20382067
/* Display the message (if any) for the problem being handled in
20392068
* this iteration of the loop */

utf8.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,8 @@ point's representation.
11281128
/* Largest code point we accept from external sources */
11291129
#define MAX_LEGAL_CP ((UV)IV_MAX)
11301130

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

0 commit comments

Comments
 (0)