Minor cleanup in ld-ctype.c

This commit is contained in:
Roland McGrath 2014-09-12 15:59:10 -07:00
parent c079afb772
commit d7e49b19d3
2 changed files with 127 additions and 122 deletions

View File

@ -1,5 +1,12 @@
2014-09-12 Roland McGrath <roland@hack.frob.com> 2014-09-12 Roland McGrath <roland@hack.frob.com>
* locale/programs/ld-ctype.c (set_one_default): New function, broken
out of ...
(set_class_defaults): ... local function set_default here.
Define set_default as a macro locally to pass constant parameters.
(allocate_arrays, set_class_defaults): Use C99 for scope for each CNT,
rather than a shared local.
* stdlib/rpmatch.c (try): New function, broken out of ... * stdlib/rpmatch.c (try): New function, broken out of ...
(rpmatch): ... local function here. Also, prototypify definition. (rpmatch): ... local function here. Also, prototypify definition.

View File

@ -2928,61 +2928,64 @@ previous definition was here")));
} }
/* Subroutine of set_class_defaults, below. */
static void
set_one_default (struct locale_ctype_t *ctype,
const struct charmap_t *charmap,
int bitpos, int from, int to)
{
char tmp[2];
int ch;
int bit = _ISbit (bitpos);
int bitw = _ISwbit (bitpos);
/* Define string. */
strcpy (tmp, "?");
for (ch = from; ch <= to; ++ch)
{
struct charseq *seq;
tmp[0] = ch;
seq = charmap_find_value (charmap, tmp, 1);
if (seq == NULL)
{
char buf[10];
sprintf (buf, "U%08X", ch);
seq = charmap_find_value (charmap, buf, 9);
}
if (seq == NULL)
{
if (!be_quiet)
WITH_CUR_LOCALE (error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
"LC_CTYPE", tmp));
}
else if (seq->nbytes != 1)
WITH_CUR_LOCALE (error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
"LC_CTYPE", tmp));
else
ctype->class256_collection[seq->bytes[0]] |= bit;
/* No need to search here, the ASCII value is also the Unicode
value. */
ELEM (ctype, class_collection, , ch) |= bitw;
}
}
static void static void
set_class_defaults (struct locale_ctype_t *ctype, set_class_defaults (struct locale_ctype_t *ctype,
const struct charmap_t *charmap, const struct charmap_t *charmap,
struct repertoire_t *repertoire) struct repertoire_t *repertoire)
{ {
size_t cnt; #define set_default(bitpos, from, to) \
set_one_default (ctype, charmap, bitpos, from, to)
/* These function defines the default values for the classes and conversions /* These function defines the default values for the classes and conversions
according to POSIX.2 2.5.2.1. according to POSIX.2 2.5.2.1.
It may seem that the order of these if-blocks is arbitrary but it is NOT. It may seem that the order of these if-blocks is arbitrary but it is NOT.
Don't move them unless you know what you do! */ Don't move them unless you know what you do! */
auto void set_default (int bitpos, int from, int to);
void set_default (int bitpos, int from, int to)
{
char tmp[2];
int ch;
int bit = _ISbit (bitpos);
int bitw = _ISwbit (bitpos);
/* Define string. */
strcpy (tmp, "?");
for (ch = from; ch <= to; ++ch)
{
struct charseq *seq;
tmp[0] = ch;
seq = charmap_find_value (charmap, tmp, 1);
if (seq == NULL)
{
char buf[10];
sprintf (buf, "U%08X", ch);
seq = charmap_find_value (charmap, buf, 9);
}
if (seq == NULL)
{
if (!be_quiet)
WITH_CUR_LOCALE (error (0, 0, _("\
%s: character `%s' not defined while needed as default value"),
"LC_CTYPE", tmp));
}
else if (seq->nbytes != 1)
WITH_CUR_LOCALE (error (0, 0, _("\
%s: character `%s' in charmap not representable with one byte"),
"LC_CTYPE", tmp));
else
ctype->class256_collection[seq->bytes[0]] |= bit;
/* No need to search here, the ASCII value is also the Unicode
value. */
ELEM (ctype, class_collection, , ch) |= bitw;
}
}
/* Set default values if keyword was not present. */ /* Set default values if keyword was not present. */
if ((ctype->class_done & BITw (tok_upper)) == 0) if ((ctype->class_done & BITw (tok_upper)) == 0)
/* "If this keyword [lower] is not specified, the lowercase letters /* "If this keyword [lower] is not specified, the lowercase letters
@ -3003,11 +3006,11 @@ set_class_defaults (struct locale_ctype_t *ctype,
unsigned long int mask = BIT (tok_upper) | BIT (tok_lower); unsigned long int mask = BIT (tok_upper) | BIT (tok_lower);
unsigned long int maskw = BITw (tok_upper) | BITw (tok_lower); unsigned long int maskw = BITw (tok_upper) | BITw (tok_lower);
for (cnt = 0; cnt < 256; ++cnt) for (size_t cnt = 0; cnt < 256; ++cnt)
if ((ctype->class256_collection[cnt] & mask) != 0) if ((ctype->class256_collection[cnt] & mask) != 0)
ctype->class256_collection[cnt] |= BIT (tok_alpha); ctype->class256_collection[cnt] |= BIT (tok_alpha);
for (cnt = 0; cnt < ctype->class_collection_act; ++cnt) for (size_t cnt = 0; cnt < ctype->class_collection_act; ++cnt)
if ((ctype->class_collection[cnt] & maskw) != 0) if ((ctype->class_collection[cnt] & maskw) != 0)
ctype->class_collection[cnt] |= BITw (tok_alpha); ctype->class_collection[cnt] |= BITw (tok_alpha);
} }
@ -3025,11 +3028,11 @@ set_class_defaults (struct locale_ctype_t *ctype,
unsigned long int mask = BIT (tok_alpha) | BIT (tok_digit); unsigned long int mask = BIT (tok_alpha) | BIT (tok_digit);
unsigned long int maskw = BITw (tok_alpha) | BITw (tok_digit); unsigned long int maskw = BITw (tok_alpha) | BITw (tok_digit);
for (cnt = 0; cnt < 256; ++cnt) for (size_t cnt = 0; cnt < 256; ++cnt)
if ((ctype->class256_collection[cnt] & mask) != 0) if ((ctype->class256_collection[cnt] & mask) != 0)
ctype->class256_collection[cnt] |= BIT (tok_alnum); ctype->class256_collection[cnt] |= BIT (tok_alnum);
for (cnt = 0; cnt < ctype->class_collection_act; ++cnt) for (size_t cnt = 0; cnt < ctype->class_collection_act; ++cnt)
if ((ctype->class_collection[cnt] & maskw) != 0) if ((ctype->class_collection[cnt] & maskw) != 0)
ctype->class_collection[cnt] |= BITw (tok_alnum); ctype->class_collection[cnt] |= BITw (tok_alnum);
} }
@ -3240,13 +3243,12 @@ set_class_defaults (struct locale_ctype_t *ctype,
unsigned long int maskw = BITw (tok_upper) | BITw (tok_lower) | unsigned long int maskw = BITw (tok_upper) | BITw (tok_lower) |
BITw (tok_alpha) | BITw (tok_digit) | BITw (tok_xdigit) | BITw (tok_alpha) | BITw (tok_digit) | BITw (tok_xdigit) |
BITw (tok_punct); BITw (tok_punct);
size_t cnt;
for (cnt = 0; cnt < ctype->class_collection_act; ++cnt) for (size_t cnt = 0; cnt < ctype->class_collection_act; ++cnt)
if ((ctype->class_collection[cnt] & maskw) != 0) if ((ctype->class_collection[cnt] & maskw) != 0)
ctype->class_collection[cnt] |= BITw (tok_graph); ctype->class_collection[cnt] |= BITw (tok_graph);
for (cnt = 0; cnt < 256; ++cnt) for (size_t cnt = 0; cnt < 256; ++cnt)
if ((ctype->class256_collection[cnt] & mask) != 0) if ((ctype->class256_collection[cnt] & mask) != 0)
ctype->class256_collection[cnt] |= BIT (tok_graph); ctype->class256_collection[cnt] |= BIT (tok_graph);
} }
@ -3262,14 +3264,13 @@ set_class_defaults (struct locale_ctype_t *ctype,
unsigned long int maskw = BITw (tok_upper) | BITw (tok_lower) | unsigned long int maskw = BITw (tok_upper) | BITw (tok_lower) |
BITw (tok_alpha) | BITw (tok_digit) | BITw (tok_xdigit) | BITw (tok_alpha) | BITw (tok_digit) | BITw (tok_xdigit) |
BITw (tok_punct); BITw (tok_punct);
size_t cnt;
struct charseq *seq; struct charseq *seq;
for (cnt = 0; cnt < ctype->class_collection_act; ++cnt) for (size_t cnt = 0; cnt < ctype->class_collection_act; ++cnt)
if ((ctype->class_collection[cnt] & maskw) != 0) if ((ctype->class_collection[cnt] & maskw) != 0)
ctype->class_collection[cnt] |= BITw (tok_print); ctype->class_collection[cnt] |= BITw (tok_print);
for (cnt = 0; cnt < 256; ++cnt) for (size_t cnt = 0; cnt < 256; ++cnt)
if ((ctype->class256_collection[cnt] & mask) != 0) if ((ctype->class256_collection[cnt] & mask) != 0)
ctype->class256_collection[cnt] |= BIT (tok_print); ctype->class256_collection[cnt] |= BIT (tok_print);
@ -3376,13 +3377,13 @@ set_class_defaults (struct locale_ctype_t *ctype,
/* "If this keyword [tolower] is not specified, the mapping shall be /* "If this keyword [tolower] is not specified, the mapping shall be
the reverse mapping of the one specified to `toupper'." [P1003.2] */ the reverse mapping of the one specified to `toupper'." [P1003.2] */
{ {
for (cnt = 0; cnt < ctype->map_collection_act[0]; ++cnt) for (size_t cnt = 0; cnt < ctype->map_collection_act[0]; ++cnt)
if (ctype->map_collection[0][cnt] != 0) if (ctype->map_collection[0][cnt] != 0)
ELEM (ctype, map_collection, [1], ELEM (ctype, map_collection, [1],
ctype->map_collection[0][cnt]) ctype->map_collection[0][cnt])
= ctype->charnames[cnt]; = ctype->charnames[cnt];
for (cnt = 0; cnt < 256; ++cnt) for (size_t cnt = 0; cnt < 256; ++cnt)
if (ctype->map256_collection[0][cnt] != 0) if (ctype->map256_collection[0][cnt] != 0)
ctype->map256_collection[1][ctype->map256_collection[0][cnt]] = cnt; ctype->map256_collection[1][ctype->map256_collection[0][cnt]] = cnt;
} }
@ -3394,7 +3395,7 @@ set_class_defaults (struct locale_ctype_t *ctype,
%s: field `%s' does not contain exactly ten entries"), %s: field `%s' does not contain exactly ten entries"),
"LC_CTYPE", "outdigit")); "LC_CTYPE", "outdigit"));
for (cnt = ctype->outdigits_act; cnt < 10; ++cnt) for (size_t cnt = ctype->outdigits_act; cnt < 10; ++cnt)
{ {
ctype->mboutdigits[cnt] = charmap_find_symbol (charmap, ctype->mboutdigits[cnt] = charmap_find_symbol (charmap,
(char *) digits + cnt, (char *) digits + cnt,
@ -3429,6 +3430,8 @@ no output digits defined and none of the standard names in the charmap")));
ctype->outdigits_act = 10; ctype->outdigits_act = 10;
} }
#undef set_default
} }
@ -3878,76 +3881,72 @@ allocate_arrays (struct locale_ctype_t *ctype, const struct charmap_t *charmap,
/* Now add the explicitly specified widths. */ /* Now add the explicitly specified widths. */
if (charmap->width_rules != NULL) if (charmap->width_rules != NULL)
{ for (size_t cnt = 0; cnt < charmap->nwidth_rules; ++cnt)
size_t cnt; {
unsigned char bytes[charmap->mb_cur_max];
int nbytes = charmap->width_rules[cnt].from->nbytes;
for (cnt = 0; cnt < charmap->nwidth_rules; ++cnt) /* We have the range of character for which the width is
{ specified described using byte sequences of the multibyte
unsigned char bytes[charmap->mb_cur_max]; charset. We have to convert this to UCS4 now. And we
int nbytes = charmap->width_rules[cnt].from->nbytes; cannot simply convert the beginning and the end of the
sequence, we have to iterate over the byte sequence and
convert it for every single character. */
memcpy (bytes, charmap->width_rules[cnt].from->bytes, nbytes);
/* We have the range of character for which the width is while (nbytes < charmap->width_rules[cnt].to->nbytes
specified described using byte sequences of the multibyte || memcmp (bytes, charmap->width_rules[cnt].to->bytes,
charset. We have to convert this to UCS4 now. And we nbytes) <= 0)
cannot simply convert the beginning and the end of the {
sequence, we have to iterate over the byte sequence and /* Find the UCS value for `bytes'. */
convert it for every single character. */ int inner;
memcpy (bytes, charmap->width_rules[cnt].from->bytes, nbytes); uint32_t wch;
struct charseq *seq =
charmap_find_symbol (charmap, (char *) bytes, nbytes);
while (nbytes < charmap->width_rules[cnt].to->nbytes if (seq == NULL)
|| memcmp (bytes, charmap->width_rules[cnt].to->bytes, wch = ILLEGAL_CHAR_VALUE;
nbytes) <= 0) else if (seq->ucs4 != UNINITIALIZED_CHAR_VALUE)
{ wch = seq->ucs4;
/* Find the UCS value for `bytes'. */ else
int inner; wch = repertoire_find_value (ctype->repertoire, seq->name,
uint32_t wch; strlen (seq->name));
struct charseq *seq =
charmap_find_symbol (charmap, (char *) bytes, nbytes);
if (seq == NULL) if (wch != ILLEGAL_CHAR_VALUE)
wch = ILLEGAL_CHAR_VALUE; {
else if (seq->ucs4 != UNINITIALIZED_CHAR_VALUE) /* Store the value. */
wch = seq->ucs4; uint32_t *class_bits =
else find_idx (ctype, &ctype->class_collection, NULL,
wch = repertoire_find_value (ctype->repertoire, seq->name, &ctype->class_collection_act, wch);
strlen (seq->name));
if (wch != ILLEGAL_CHAR_VALUE) if (class_bits != NULL && (*class_bits & BITw (tok_print)))
{ wcwidth_table_add (t, wch,
/* Store the value. */ charmap->width_rules[cnt].width);
uint32_t *class_bits = }
find_idx (ctype, &ctype->class_collection, NULL,
&ctype->class_collection_act, wch);
if (class_bits != NULL && (*class_bits & BITw (tok_print))) /* "Increment" the bytes sequence. */
wcwidth_table_add (t, wch, inner = nbytes - 1;
charmap->width_rules[cnt].width); while (inner >= 0 && bytes[inner] == 0xff)
} --inner;
/* "Increment" the bytes sequence. */ if (inner < 0)
inner = nbytes - 1; {
while (inner >= 0 && bytes[inner] == 0xff) /* We have to extend the byte sequence. */
--inner; if (nbytes >= charmap->width_rules[cnt].to->nbytes)
break;
if (inner < 0) bytes[0] = 1;
{ memset (&bytes[1], 0, nbytes);
/* We have to extend the byte sequence. */ ++nbytes;
if (nbytes >= charmap->width_rules[cnt].to->nbytes) }
break; else
{
bytes[0] = 1; ++bytes[inner];
memset (&bytes[1], 0, nbytes); while (++inner < nbytes)
++nbytes; bytes[inner] = 0;
} }
else }
{ }
++bytes[inner];
while (++inner < nbytes)
bytes[inner] = 0;
}
}
}
}
/* Set the width of L'\0' to 0. */ /* Set the width of L'\0' to 0. */
wcwidth_table_add (t, 0, 0); wcwidth_table_add (t, 0, 0);
@ -3976,7 +3975,6 @@ allocate_arrays (struct locale_ctype_t *ctype, const struct charmap_t *charmap,
/* First count how many entries we have. This is the upper limit /* First count how many entries we have. This is the upper limit
since some entries from the included files might be overwritten. */ since some entries from the included files might be overwritten. */
size_t number = 0; size_t number = 0;
size_t cnt;
struct translit_t *runp = ctype->translit; struct translit_t *runp = ctype->translit;
struct translit_t **sorted; struct translit_t **sorted;
size_t from_len, to_len; size_t from_len, to_len;
@ -4036,7 +4034,7 @@ allocate_arrays (struct locale_ctype_t *ctype, const struct charmap_t *charmap,
- to-string array. - to-string array.
*/ */
from_len = to_len = 0; from_len = to_len = 0;
for (cnt = 0; cnt < number; ++cnt) for (size_t cnt = 0; cnt < number; ++cnt)
{ {
struct translit_to_t *srunp; struct translit_to_t *srunp;
from_len += wcslen ((const wchar_t *) sorted[cnt]->from) + 1; from_len += wcslen ((const wchar_t *) sorted[cnt]->from) + 1;
@ -4059,7 +4057,7 @@ allocate_arrays (struct locale_ctype_t *ctype, const struct charmap_t *charmap,
from_len = 0; from_len = 0;
to_len = 0; to_len = 0;
for (cnt = 0; cnt < number; ++cnt) for (size_t cnt = 0; cnt < number; ++cnt)
{ {
size_t len; size_t len;
struct translit_to_t *srunp; struct translit_to_t *srunp;