Would have thought the compiler would do that for me, but I've cached it anyway. Good point on the second, missed that when I changed the loops. Fixed attached.
Rory On 11 November 2014 19:57, Ben Boeckel <[email protected]> wrote: > On Tue, Nov 11, 2014 at 19:33:45 +0000, Rory McNamara wrote: >> I've removed the do while, but there is still two for loops. The first >> has a bail early for exact matches. > > Ah, indeed. > >> I think it now correctly fails in all cases. I also think it doesn't >> have problems based on ordering, as it only looks for the match if >> ther is only one. > > See comments inline. > > --Ben > >> diff --git a/src/main.c b/src/main.c >> index 7c30ff6..8664662 100644 >> --- a/src/main.c >> +++ b/src/main.c >> @@ -214,8 +214,18 @@ setup_connection(void) >> static struct command * >> find_command(const char *name) >> { >> + int matches = 0, len = 0; >> + for (unsigned i = 0; mpc_table[i].command != NULL; ++i) { >> + if (strncmp(name, mpc_table[i].command, strlen(name)) == 0) { >> + matches += 1; >> + if (strlen(name) == strlen(mpc_table[i].command)) >> + return &mpc_table[i]; //Exact match > > There are a *lot* of strlen(name) calls here, so a variable to cache it > would be good. > >> + } >> + } >> + if (matches != 1) //Ambiguous or nonexistent >> + return NULL; >> for (unsigned i = 0; mpc_table[i].command != NULL; ++i) >> - if (strcmp(name, mpc_table[i].command) == 0) >> + if (strncmp(name, mpc_table[i].command, len) == 0 && >> strncmp(name, mpc_table[i].command, strlen(name)) == 0) > > len is always 0 here (never written above). > >> return &mpc_table[i]; >> >> return NULL;
From ed6d9ad167a5a55d7f8989e7798e4955afb18cff Mon Sep 17 00:00:00 2001 From: PsychoMario <[email protected]> Date: Tue, 11 Nov 2014 13:05:15 +0000 Subject: [PATCH] added least unambiguous to find_command --- doc/mpc.1 | 1 + src/main.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/mpc.1 b/doc/mpc.1 index 2bea9a6..b119c81 100644 --- a/doc/mpc.1 +++ b/doc/mpc.1 @@ -96,6 +96,7 @@ If you specify an absolute path, mpc attempts a connection via Unix Domain Socke The port to connect to; if not given, the value of the environment variable MPD_PORT is checked before defaulting to 6600. This default can be changed at compile-time. .br .SH COMMANDS +Commands can be used from the least unambiguous prefix (e.g insert or ins) .TP .B add <file> Adds a song from the music database to the playlist. Can also read input from pipes. Use "mpc ls | mpc add" to add all files to the playlist. diff --git a/src/main.c b/src/main.c index 7c30ff6..e0f56ef 100644 --- a/src/main.c +++ b/src/main.c @@ -214,8 +214,18 @@ setup_connection(void) static struct command * find_command(const char *name) { + unsigned int matches = 0, len = strlen(name); + for (unsigned i = 0; mpc_table[i].command != NULL; ++i) { + if (strncmp(name, mpc_table[i].command, strlen(name)) == 0) { + matches += 1; + if (len == strlen(mpc_table[i].command)) + return &mpc_table[i]; //Exact match + } + } + if (matches != 1) //Ambiguous or nonexistent + return NULL; for (unsigned i = 0; mpc_table[i].command != NULL; ++i) - if (strcmp(name, mpc_table[i].command) == 0) + if (strncmp(name, mpc_table[i].command, len) == 0 && strncmp(name, mpc_table[i].command, len) == 0) return &mpc_table[i]; return NULL; -- 2.1.2
_______________________________________________ mpd-devel mailing list [email protected] http://mailman.blarg.de/listinfo/mpd-devel
