From 18b8d1e8ad877eb1eaad5216b821c0fb8394c76b Mon Sep 17 00:00:00 2001
From: Timo Teräs <timo.teras@iki.fi>
Date: Sat, 4 Mar 2023 18:39:14 +0200
Subject: search: always use apk_db_foreach_sorted_name and fnmatch

To make the search output and experience more consistent.
---
 src/app_search.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

(limited to 'src')

diff --git a/src/app_search.c b/src/app_search.c
index 3651ec6..fd9bd40 100644
--- a/src/app_search.c
+++ b/src/app_search.c
@@ -92,7 +92,6 @@ static int option_parse_applet(void *ctx, struct apk_db_options *dbopts, int opt
 		break;
 	case OPT_SEARCH_description:
 		ictx->search_description = 1;
-		ictx->search_exact = 1;
 		ictx->show_all = 1;
 		break;
 	case OPT_SEARCH_exact:
@@ -126,15 +125,16 @@ static void print_result_pkg(struct search_ctx *ctx, struct apk_package *pkg)
 
 	if (ctx->search_description) {
 		foreach_array_item(pmatch, ctx->filter) {
-			if (strstr(pkg->description, *pmatch) != NULL ||
-			    strstr(pkg->name->name, *pmatch) != NULL)
+			if (fnmatch(*pmatch, pkg->description, 0) == 0 ||
+			    fnmatch(*pmatch, pkg->name->name, 0) == 0)
 				goto match;
 		}
 		return;
 	}
 	if (ctx->search_origin) {
 		foreach_array_item(pmatch, ctx->filter) {
-			if (pkg->origin && apk_blob_compare(APK_BLOB_STR(*pmatch), *pkg->origin) == 0)
+			if (!pkg->origin) continue;
+			if (apk_blob_compare(APK_BLOB_STR(*pmatch), *pkg->origin) == 0)
 				goto match;
 		}
 		return;
@@ -166,12 +166,6 @@ static int print_result(struct apk_database *db, const char *match, struct apk_n
 	return 0;
 }
 
-static int print_pkg(apk_hash_item item, void *pctx)
-{
-	print_result_pkg((struct search_ctx *) pctx, (struct apk_package *) item);
-	return 0;
-}
-
 static int search_main(void *pctx, struct apk_database *db, struct apk_string_array *args)
 {
 	struct search_ctx *ctx = (struct search_ctx *) pctx;
@@ -184,8 +178,11 @@ static int search_main(void *pctx, struct apk_database *db, struct apk_string_ar
 	if (ctx->print_result == NULL)
 		ctx->print_result = ctx->print_package;
 
-	if (ctx->search_description || ctx->search_origin)
-		return apk_hash_foreach(&db->available.packages, print_pkg, ctx);
+	if (ctx->search_description || ctx->search_origin) {
+		// Just enumerate all names in sorted order, and do the
+		// filtering in the callback.
+		args = NULL;
+	}
 
 	if (!ctx->search_exact) {
 		foreach_array_item(pmatch, ctx->filter) {
-- 
cgit v1.2.3-70-g09d2