1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 21:19:24 +00:00

Small simplifications.

This commit is contained in:
Bruno Haible
2003-04-08 10:55:59 +00:00
parent 2535f34494
commit 3bdad49e79
3 changed files with 41 additions and 21 deletions

View File

@@ -1,5 +1,11 @@
2002-12-12 Bruno Haible <bruno@clisp.org> 2002-12-12 Bruno Haible <bruno@clisp.org>
* src/output.h (Output::output_asso_values_ref): New declaration.
* src/output.cc (char_to_index): Remove variable.
(Output::output_asso_values_ref): New function.
(Output::output_hash_function): Use it.
(Output::output): Update.
* src/positions.h (Positions::is_useall, Positions::set_useall, * src/positions.h (Positions::is_useall, Positions::set_useall,
Positions::iterator, Positions::reviterator): New method declarations. Positions::iterator, Positions::reviterator): New method declarations.
(Positions::_useall): New field. (Positions::_useall): New field.

View File

@@ -63,9 +63,6 @@ smallest_integral_type (int min, int max)
return "int"; return "int";
} }
/* A cast from `char' to a valid array index. */
static const char *char_to_index;
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* Constructor. /* Constructor.
@@ -561,6 +558,26 @@ void Output_Compare_Memcmp::output_comparison (const Output_Expr& expr1,
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* Generates a C expression for an asso_values[] reference. */
void
Output::output_asso_values_ref (int pos) const
{
printf ("asso_values[");
/* Always cast to unsigned char. This is necessary when the alpha_inc
is nonzero, and also avoids a gcc warning "subscript has type 'char'". */
printf ("(unsigned char)");
if (pos == Positions::LASTCHAR)
printf ("str[len - 1]");
else
{
printf ("str[%d]", pos);
if (_alpha_inc[pos])
printf ("+%u", _alpha_inc[pos]);
}
printf ("]");
}
/* Generates C code for the hash function that returns the /* Generates C code for the hash function that returns the
proper encoding for each keyword. proper encoding for each keyword.
The hash function has the signature The hash function has the signature
@@ -665,20 +682,15 @@ Output::output_hash_function () const
&& _key_positions[1] == Positions::LASTCHAR) && _key_positions[1] == Positions::LASTCHAR)
/* Optimize special case of "-k 1,$". */ /* Optimize special case of "-k 1,$". */
{ {
printf ("asso_values[%sstr[len - 1]] + asso_values[%sstr[0]", output_asso_values_ref (Positions::LASTCHAR);
char_to_index, char_to_index); printf (" + ");
if (_alpha_inc[0]) output_asso_values_ref (0);
printf ("+%u", _alpha_inc[0]);
printf ("]");
} }
else else
{ {
for (; key_pos != Positions::LASTCHAR; ) for (; key_pos != Positions::LASTCHAR; )
{ {
printf ("asso_values[%sstr[%d]", char_to_index, key_pos); output_asso_values_ref (key_pos);
if (_alpha_inc[key_pos])
printf ("+%u", _alpha_inc[key_pos]);
printf ("]");
if ((key_pos = iter.next ()) != PositionIterator::EOS) if ((key_pos = iter.next ()) != PositionIterator::EOS)
printf (" + "); printf (" + ");
else else
@@ -686,7 +698,7 @@ Output::output_hash_function () const
} }
if (key_pos == Positions::LASTCHAR) if (key_pos == Positions::LASTCHAR)
printf ("asso_values[%sstr[len - 1]]", char_to_index); output_asso_values_ref (Positions::LASTCHAR);
} }
printf (";\n"); printf (";\n");
@@ -713,11 +725,9 @@ Output::output_hash_function () const
for ( ; i > key_pos; i--) for ( ; i > key_pos; i--)
printf (" case %d:\n", i); printf (" case %d:\n", i);
printf (" hval += asso_values[%sstr[%d]", printf (" hval += ");
char_to_index, key_pos); output_asso_values_ref (key_pos);
if (_alpha_inc[key_pos]) printf (";\n");
printf ("+%u", _alpha_inc[key_pos]);
printf ("];\n");
key_pos = iter.next (); key_pos = iter.next ();
} }
@@ -731,7 +741,10 @@ Output::output_hash_function () const
" }\n" " }\n"
" return hval"); " return hval");
if (key_pos == Positions::LASTCHAR) if (key_pos == Positions::LASTCHAR)
printf (" + asso_values[%sstr[len - 1]]", char_to_index); {
printf (" + ");
output_asso_values_ref (Positions::LASTCHAR);
}
printf (";\n"); printf (";\n");
} }
} }
@@ -1577,8 +1590,6 @@ Output::output ()
_struct_tag = (const_always[0] ? "const char *" : "char *"); _struct_tag = (const_always[0] ? "const char *" : "char *");
} }
char_to_index = "(unsigned char)";
printf ("/* "); printf ("/* ");
if (option[KRC]) if (option[KRC])
printf ("KR-C"); printf ("KR-C");

View File

@@ -71,6 +71,9 @@ private:
/* Outputs the maximum and minimum hash values etc. */ /* Outputs the maximum and minimum hash values etc. */
void output_constants (struct Output_Constants&) const; void output_constants (struct Output_Constants&) const;
/* Generates a C expression for an asso_values[] reference. */
void output_asso_values_ref (int pos) const;
/* Generates C code for the hash function that returns the /* Generates C code for the hash function that returns the
proper encoding for each keyword. */ proper encoding for each keyword. */
void output_hash_function () const; void output_hash_function () const;