]> git.mdlowis.com Git - proto/pick.git/commitdiff
fixed double char bug
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 12:36:42 +0000 (08:36 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 17 Apr 2019 12:36:42 +0000 (08:36 -0400)
build.sh
pick.c

index 7e679cb9d01dbc1afc128d4bc20d96a976e739c5..319adf259e494dfdb3d4f5ca8b6695dfc9ca8370 100755 (executable)
--- 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 6ad9f436ca17fa4bf34add3d5aac9a92f2cc6ed6..5e9454916c63afbe0a5deeb05d78069f6b5a68c7 100644 (file)
--- 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;