From 52be46abfca9a9b458d7455e891c937437b1356e Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sat, 19 Nov 2022 17:55:24 +0000 Subject: Cleanup some potentially flaky logic. Some of the string vec code was still assuming that it was just a list of `char *`s, rather than `struct scored_string`s. Correcting that makes future changes a little less error prone, and allows significantly simpler history sorting for tofi-run. --- src/string_vec.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/string_vec.c') diff --git a/src/string_vec.c b/src/string_vec.c index a30a570..4607727 100644 --- a/src/string_vec.c +++ b/src/string_vec.c @@ -11,23 +11,19 @@ static int cmpstringp(const void *restrict a, const void *restrict b) { - /* - * For qsort we receive pointers to the array elements (which are - * pointers to char), so convert and dereference them for comparison. - */ - const char *restrict str1 = *(const char **)a; - const char *restrict str2 = *(const char **)b; + struct scored_string *restrict str1 = (struct scored_string *)a; + struct scored_string *restrict str2 = (struct scored_string *)b; /* * Ensure any NULL strings are shoved to the end. */ - if (str1 == NULL) { + if (str1->string == NULL) { return 1; } - if (str2 == NULL) { + if (str2->string == NULL) { return -1; } - return strcmp(str1, str2); + return strcmp(str1->string, str2->string); } static int cmpscorep(const void *restrict a, const void *restrict b) @@ -109,7 +105,7 @@ void string_vec_uniq(struct string_vec *restrict vec) vec->count = count; } -char **string_vec_find(struct string_vec *restrict vec, const char * str) +struct scored_string *string_vec_find(struct string_vec *restrict vec, const char * str) { return bsearch(&str, vec->buf, vec->count, sizeof(vec->buf[0]), cmpstringp); } -- cgit v1.2.3