[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 03/11] muscle: refactor
From: |
Akim Demaille |
Subject: |
[PATCH 03/11] muscle: refactor |
Date: |
Thu, 4 Apr 2013 17:36:20 +0200 |
* src/muscle-tab.c (muscle_lookup, muscle_entry_new): New.
(muscle_insert, muscle_grow, muscle_find_const, muscle_find): Use them.
---
src/muscle-tab.c | 93 +++++++++++++++++++++++++++-----------------------------
1 file changed, 45 insertions(+), 48 deletions(-)
diff --git a/src/muscle-tab.c b/src/muscle-tab.c
index c4f302c..e6df47f 100644
--- a/src/muscle-tab.c
+++ b/src/muscle-tab.c
@@ -60,6 +60,19 @@ hash_muscle (const void *x, size_t tablesize)
return hash_string (m->key, tablesize);
}
+/* Create a fresh muscle name KEY, and insert in the hash table. */
+static void *
+muscle_entry_new (char const *key)
+{
+ muscle_entry *res = xmalloc (sizeof *res);
+ res->key = key;
+ res->value = NULL;
+ res->storage = NULL;
+ if (!hash_insert (muscle_table, res))
+ xalloc_die ();
+ return res;
+}
+
static void
muscle_entry_free (void *entry)
{
@@ -89,26 +102,26 @@ muscle_free (void)
obstack_free (&muscle_obstack, NULL);
}
-
-void
-muscle_insert (char const *key, char const *value)
+/* Look for the muscle named KEY. Return NULL if does not exist. */
+static
+muscle_entry *
+muscle_lookup (char const *key)
{
muscle_entry probe;
- muscle_entry *entry;
-
probe.key = key;
- entry = hash_lookup (muscle_table, &probe);
+ return hash_lookup (muscle_table, &probe);
+}
- if (!entry)
- {
- /* First insertion in the hash. */
- entry = xmalloc (sizeof *entry);
- entry->key = key;
- if (!hash_insert (muscle_table, entry))
- xalloc_die ();
- }
- else
+
+void
+muscle_insert (char const *key, char const *value)
+{
+ muscle_entry *entry = muscle_lookup (key);
+ if (entry)
free (entry->storage);
+ else
+ /* First insertion in the hash. */
+ entry = muscle_entry_new (key);
entry->value = value;
entry->storage = NULL;
}
@@ -123,22 +136,9 @@ muscle_insert (char const *key, char const *value)
void
muscle_grow (const char *key, const char *val, const char *separator)
{
- muscle_entry probe;
- muscle_entry *entry = NULL;
+ muscle_entry *entry = muscle_lookup (key);
- probe.key = key;
- entry = hash_lookup (muscle_table, &probe);
-
- if (!entry)
- {
- /* First insertion in the hash. */
- entry = xmalloc (sizeof *entry);
- entry->key = key;
- if (!hash_insert (muscle_table, entry))
- xalloc_die ();
- entry->value = entry->storage = xstrdup (val);
- }
- else
+ if (entry)
{
/* Grow the current value. */
char *new_val;
@@ -148,6 +148,12 @@ muscle_grow (const char *key, const char *val, const char
*separator)
entry->value = entry->storage = xstrdup (new_val);
obstack_free (&muscle_obstack, new_val);
}
+ else
+ {
+ /* First insertion in the hash. */
+ entry = muscle_entry_new (key);
+ entry->value = entry->storage = xstrdup (val);
+ }
}
/*------------------------------------------------------------------.
@@ -182,8 +188,9 @@ muscle_code_grow (const char *key, const char *val,
location loc)
}
-void muscle_pair_list_grow (const char *muscle,
- const char *a1, const char *a2)
+void
+muscle_pair_list_grow (const char *muscle,
+ const char *a1, const char *a2)
{
char *pair;
obstack_sgrow (&muscle_obstack, "[");
@@ -200,29 +207,19 @@ void muscle_pair_list_grow (const char *muscle,
char const *
muscle_find_const (char const *key)
{
- muscle_entry probe;
- muscle_entry *result = NULL;
-
- probe.key = key;
- result = hash_lookup (muscle_table, &probe);
- if (result)
- return result->value;
- return NULL;
+ muscle_entry *entry = muscle_lookup (key);
+ return entry ? entry->value : NULL;
}
char *
muscle_find (char const *key)
{
- muscle_entry probe;
- muscle_entry *result = NULL;
-
- probe.key = key;
- result = hash_lookup (muscle_table, &probe);
- if (result)
+ muscle_entry *entry = muscle_lookup (key);
+ if (entry)
{
- aver (result->value == result->storage);
- return result->storage;
+ aver (entry->value == entry->storage);
+ return entry->storage;
}
return NULL;
}
--
1.8.2
- [PATCH 00/11] support "%define variable {value}", Akim Demaille, 2013/04/04
- [PATCH 05/11] grammar: style changes, Akim Demaille, 2013/04/04
- [PATCH 07/11] style: fix comments, Akim Demaille, 2013/04/04
- [PATCH 11/11] api.namespace: demonstrate and use {...} values instead of "..." values, Akim Demaille, 2013/04/04
- [PATCH 04/11] grammar: split %union to group together related aspects, Akim Demaille, 2013/04/04
- [PATCH 02/11] style: comment changes, Akim Demaille, 2013/04/04
- [PATCH 08/11] grammar: record the kind of %define variable values, Akim Demaille, 2013/04/04
- [PATCH 01/11] muscle: minor refactoring, Akim Demaille, 2013/04/04
- [PATCH 10/11] grammar: do not add a \n at the end of blocks of code, Akim Demaille, 2013/04/04
- [PATCH 03/11] muscle: refactor,
Akim Demaille <=
- [PATCH 09/11] regen, Akim Demaille, 2013/04/04
- [PATCH 06/11] regen, Akim Demaille, 2013/04/04
- Re: [PATCH 00/11] support "%define variable {value}", Joel E. Denny, 2013/04/07
- Re: [PATCH 00/11] support "%define variable {value}", Akim Demaille, 2013/04/09