Skip to content

Commit

Permalink
locale.c: Simplify S_category_name
Browse files Browse the repository at this point in the history
We can use the new function S_get_category_index() to simplify this.
Also, when I wrote it I didn't know about Perl_form(), and had
reimplemented a portion of it here; which is yanked as well.
  • Loading branch information
khwilliamson committed May 9, 2021
1 parent dad5d20 commit bcdbafb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 46 deletions.
51 changes: 13 additions & 38 deletions locale.c
Expand Up @@ -311,6 +311,7 @@ S_get_category_index(const int category, const char * locale)
* elements is so far at most 12 */

unsigned int i;
const char * conditional_warn_text = "; can't set it to ";

PERL_ARGS_ASSERT_GET_CATEGORY_INDEX;

Expand All @@ -330,12 +331,16 @@ S_get_category_index(const int category, const char * locale)
}

/* Here, we don't know about this category, so can't handle it. */

if (! locale) {
locale = "(unknown)";
locale = "";
conditional_warn_text = "";
}

/* diag_listed_as: Unknown locale category %d; can't set it to %s */
Perl_warner_nocontext(packWARN(WARN_LOCALE),
"Unknown locale category %d; can't set it to %s\n",
category, locale);
"Unknown locale category %d%s%s",
category, conditional_warn_text, locale);

/* Return an out-of-bounds value */
return NOMINAL_LC_ALL_INDEX + 1;
Expand All @@ -344,45 +349,15 @@ S_get_category_index(const int category, const char * locale)
STATIC const char *
S_category_name(const int category)
{
unsigned int i;

#ifdef LC_ALL
unsigned int index;

if (category == LC_ALL) {
return "LC_ALL";
}

#endif
index = get_category_index(category, NULL);

for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) {
if (category == categories[i]) {
return category_names[i];
}
if (index <= NOMINAL_LC_ALL_INDEX) {
return category_names[index];
}

{
const char suffix[] = " (unknown)";
int temp = category;
Size_t length = sizeof(suffix) + 1;
char * unknown;
dTHX;

if (temp < 0) {
length++;
temp = - temp;
}

/* Calculate the number of digits */
while (temp >= 10) {
temp /= 10;
length++;
}

Newx(unknown, length, char);
my_snprintf(unknown, length, "%d%s", category, suffix);
SAVEFREEPV(unknown);
return unknown;
}
return Perl_form_nocontext("%d (unknown)", category);
}

#endif /* ifdef USE_LOCALE */
Expand Down
12 changes: 4 additions & 8 deletions pod/perldelta.pod
Expand Up @@ -764,15 +764,11 @@ diagnostic messages, see L<perldiag>.

=item *

L<Bareword filehandle "%s" not allowed under 'no feature "bareword_filehandles"'|perldiag/"Bareword filehandle "%s" not allowed under 'no feature "bareword_filehandles"'">
L<Unknown locale category %d|perldiag/"Unknown locale category %d">

This accompanies the new L<bareword_filehandles|feature/"The 'bareword_filehandles' feature."> feature.

=item *

L<Multidimensional hash lookup is disabled|perldiag/"Multidimensional hash lookup is disabled">

This accompanies the new L<multidimensional|feature/"The 'multidimensional' feature"> feature.
This is a shortened form of an already existing diagnostic, for use when
there is no new locale being switched to. The previous diagnostic was
misleading in such circumstances.

=back

Expand Down
2 changes: 2 additions & 0 deletions pod/perldiag.pod
Expand Up @@ -6804,6 +6804,8 @@ compiler does not recognize. Check your spelling.
(P) Perl was about to print an error message in C<$@>, but the C<$@> variable
did not exist, even after an attempt to create it.

=item Unknown locale category %d

=item Unknown locale category %d; can't set it to %s

(W locale) You used a locale category that perl doesn't recognize, so it
Expand Down

0 comments on commit bcdbafb

Please sign in to comment.