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;
_______________________________________________
mpd-devel mailing list
[email protected]
http://mailman.blarg.de/listinfo/mpd-devel