aboutsummaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-07-09 22:32:55 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-07-09 22:34:42 +0200
commit445837007d4d6000a322d7738f05035fcc9023a8 (patch)
treeecdd18b239afbd8688637e35223f6b9e58c74170 /degesch.c
parent567ce622571872cff9dfe92a43b8529072a3d55b (diff)
downloadxK-445837007d4d6000a322d7738f05035fcc9023a8.tar.gz
xK-445837007d4d6000a322d7738f05035fcc9023a8.tar.xz
xK-445837007d4d6000a322d7738f05035fcc9023a8.zip
degesch: get rid of partial command name matching
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c38
1 files changed, 8 insertions, 30 deletions
diff --git a/degesch.c b/degesch.c
index d35e106..3d0585d 100644
--- a/degesch.c
+++ b/degesch.c
@@ -7591,38 +7591,16 @@ handle_command_help (struct handler_args *a)
return true;
}
-static int
-command_handler_cmp_by_length (const void *a, const void *b)
-{
- const struct command_handler *first = a;
- const struct command_handler *second = b;
- return strlen (first->name) - strlen (second->name);
-}
-
static void
-init_partial_matching_user_command_map (struct str_map *partial)
+init_user_command_map (struct str_map *map)
{
- // Trivially create a partial matching map
- str_map_init (partial);
- partial->key_xfrm = tolower_ascii_strxfrm;
-
- // We process them from the longest to the shortest one,
- // so that common prefixes favor shorter entries
- struct command_handler *by_length[N_ELEMENTS (g_command_handlers)];
- for (size_t i = 0; i < N_ELEMENTS (by_length); i++)
- by_length[i] = &g_command_handlers[i];
- qsort (by_length, N_ELEMENTS (by_length), sizeof *by_length,
- command_handler_cmp_by_length);
+ str_map_init (map);
+ map->key_xfrm = tolower_ascii_strxfrm;
- for (size_t i = N_ELEMENTS (by_length); i--; )
+ for (size_t i = 0; i < N_ELEMENTS (g_command_handlers); i++)
{
- char *copy = xstrdup (by_length[i]->name);
- for (size_t part = strlen (copy); part; part--)
- {
- copy[part] = '\0';
- str_map_set (partial, copy, by_length[i]);
- }
- free (copy);
+ struct command_handler *handler = &g_command_handlers[i];
+ str_map_set (map, handler->name, handler);
}
}
@@ -7631,10 +7609,10 @@ process_user_command
(struct app_context *ctx, const char *command_name, char *input)
{
static bool initialized = false;
- static struct str_map partial;
+ static struct str_map map;
if (!initialized)
{
- init_partial_matching_user_command_map (&partial);
+ init_user_command_map (&map);
initialized = true;
}