style: minor refactoring

* data/bison.m4: Formatting changes.
* src/scan-code.l: Avoid loops, prefer standard string functions.
(find_prefix_end): Be const correct.
Avoid useless intermediate variables.
(variant_add): Be const correct.
(parse_ref): Prefer variable definitions to assignments.
This commit is contained in:
Akim Demaille
2018-09-24 06:31:54 +02:00
parent e198269f40
commit 2eaa7ed946
2 changed files with 27 additions and 52 deletions

View File

@@ -856,9 +856,9 @@ m4_define([b4_percent_define_check_kind],
[b4_error([m4_default([$3], [complain])], [b4_error([m4_default([$3], [complain])],
b4_percent_define_get_loc([$1]), b4_percent_define_get_loc([$1]),
[m4_case([$2], [m4_case([$2],
[code], [[%%define variable '%s' requires '{...}' values]], [code], [[%%define variable '%s' requires '{...}' values]],
[keyword], [[%%define variable '%s' requires keyword values]], [keyword], [[%%define variable '%s' requires keyword values]],
[string], [[%%define variable '%s' requires '"..."' values]])], [string], [[%%define variable '%s' requires '"..."' values]])],
[$1])])])dnl [$1])])])dnl
]) ])

View File

@@ -215,10 +215,7 @@ is_dot_or_dash (char ch)
static inline bool static inline bool
contains_dot_or_dash (const char* p) contains_dot_or_dash (const char* p)
{ {
for (; *p; ++p) return strpbrk(p, ".-");
if (is_dot_or_dash (*p))
return true;
return false;
} }
/* Defines a variant of a symbolic name resolution. */ /* Defines a variant of a symbolic name resolution. */
@@ -277,26 +274,21 @@ variant_table_free (void)
variant_table_size = variant_count = 0; variant_table_size = variant_count = 0;
} }
static char * static char const *
find_prefix_end (const char *prefix, char *begin, char *end) find_prefix_end (char const *prefix, char const *cp, char const *end)
{ {
char *ptr = begin; for (; *prefix && cp != end; ++prefix, ++cp)
if (*prefix != *cp)
return NULL;
for (; *prefix && ptr != end; ++prefix, ++ptr) return *prefix ? NULL : cp;
if (*prefix != *ptr)
return 0;
if (*prefix)
return 0;
return ptr;
} }
static variant * static variant *
variant_add (uniqstr id, location id_loc, unsigned symbol_index, variant_add (uniqstr id, location id_loc, unsigned symbol_index,
char *cp, char *cp_end, bool explicit_bracketing) char const *cp, char const *cp_end, bool explicit_bracketing)
{ {
char *prefix_end = find_prefix_end (id, cp, cp_end); char const *prefix_end = find_prefix_end (id, cp, cp_end);
if (prefix_end && if (prefix_end &&
(prefix_end == cp_end || (prefix_end == cp_end ||
(!explicit_bracketing && is_dot_or_dash (*prefix_end)))) (!explicit_bracketing && is_dot_or_dash (*prefix_end))))
@@ -433,31 +425,14 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
} }
} }
char *cp_end; bool const explicit_bracketing = *cp == '[';
bool explicit_bracketing;
if ('[' == *cp) if (explicit_bracketing)
{ ++cp;
/* Ignore the brackets. */
for (cp_end = ++cp; *cp_end != ']'; ++cp_end)
continue;
explicit_bracketing = true;
}
else else
{ ref_tail_fields = strpbrk (cp, ".-");
/* Take all characters of the name. */
for (char* p = cp; *p; ++p)
if (is_dot_or_dash (*p))
{
ref_tail_fields = p;
break;
}
for (cp_end = cp; *cp_end; ++cp_end)
continue;
explicit_bracketing = false; char const *cp_end = strchr (cp, explicit_bracketing ? ']' : '\0');
}
/* Add all relevant variants. */ /* Add all relevant variants. */
{ {
@@ -837,17 +812,17 @@ code_props_translate_code (code_props *self)
{ {
switch (self->kind) switch (self->kind)
{ {
case CODE_PROPS_NONE: case CODE_PROPS_NONE:
break; break;
case CODE_PROPS_PLAIN: case CODE_PROPS_PLAIN:
self->code = translate_action (self, INITIAL); self->code = translate_action (self, INITIAL);
break; break;
case CODE_PROPS_SYMBOL_ACTION: case CODE_PROPS_SYMBOL_ACTION:
self->code = translate_action (self, SC_SYMBOL_ACTION); self->code = translate_action (self, SC_SYMBOL_ACTION);
break; break;
case CODE_PROPS_RULE_ACTION: case CODE_PROPS_RULE_ACTION:
self->code = translate_action (self, SC_RULE_ACTION); self->code = translate_action (self, SC_RULE_ACTION);
break; break;
} }
} }