From: Michael D. Lowis Date: Wed, 17 Apr 2019 12:36:42 +0000 (-0400) Subject: fixed double char bug X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=84fab72cb877ae8c8c3d740c9d54bbcc74f6b4d0;p=proto%2Fpick.git fixed double char bug --- diff --git a/build.sh b/build.sh index 7e679cb..319adf2 100755 --- a/build.sh +++ b/build.sh @@ -1,2 +1,2 @@ #!/bin/sh -gcc -D_XOPEN_SOURCE=700 *.c -I . -o pick -g -fsanitize=address,undefined -lasan -lm +gcc -D_XOPEN_SOURCE=700 *.c -I . -o pick -O3 -Wno-unused-result diff --git a/pick.c b/pick.c index 6ad9f43..5e94549 100644 --- a/pick.c +++ b/pick.c @@ -70,26 +70,36 @@ static void load_choices(void) { } } -static char* find_match_start(char *str, int ch) { +static char* find_char(char *str, int ch) { for (; *str; str++) if (tolower(*str) == tolower(ch)) return str; return NULL; } -static bool match(char *string, size_t offset, size_t *start, size_t *end) { - char* q = Query; - char* s = find_match_start(&string[offset], *q); - char* e = s; - /* bail if no match for first char */ - if (s == NULL) return 0; +static bool find_match(char *string, char* query, size_t offset, char **start, char **end) { + char *s, *e; + /* find first match char or bail */ + s = e = find_char(&string[offset], *query); + if (s == NULL) return false; + /* find the end of the match */ - for (q++, e++; *q; q++) - if ((e = find_match_start(e, *q)) == NULL) + for (query++; *query; query++) + if ((e = find_char(e+1, *query)) == NULL) return false; - /* make note of the matching range */ + + /* if we made it this far, we found a match */ + *start = s, *end = e; + return true; +} + +static bool match(char *string, size_t offset, size_t *start, size_t *end) { + char *s, *e; + if (!find_match(string, Query, offset, &s, &e)) + return false; *start = s - string; *end = e - string; + /* Less than or equal is used in order to obtain the left-most match. */ if (match(string, offset + 1, start, end) && (size_t)(e - s) <= *end - *start) { *start = s - string;