Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
utils: add charv_search
Baptiste Daroussin committed 1 year ago
commit b4c8d823b8e20336b25cce9338d57184328b5aab
parent 51fcd75
4 files changed +34 -26
modified libpkg/pkg.c
@@ -1702,11 +1702,6 @@ pkg_cf_cmp(struct pkg_config_file *a, struct pkg_config_file *b)
	return (STREQ(a->path, b->path));
}

-
static int
-
char_cmp(const void *a, const void *b) {
-
    return strcmp(*(char **)a, *(char **)b);
-
}
-

void
pkg_lists_sort(struct pkg *p)
{
modified libpkg/private/utils.h
@@ -1,30 +1,11 @@
/*-
-
 * Copyright (c) 2011-2022 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2025 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * Copyright (c) 2023 Serenity Cyber Security, LLC
 *                    Author: Gleb Popov <arrowd@FreeBSD.org>
 * All rights reserved.
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer
-
 *    in this position and unchanged.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
-
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
-
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 * SPDX-License-Identifier: BSD-2-Clause
 */

#ifndef _PKG_UTIL_H
@@ -121,5 +102,7 @@ const char *get_http_auth(void);
bool c_charv_contains(c_charv_t *, const char *, bool);
bool charv_contains(charv_t *, const char *, bool);
bool str_ends_with(const char *str, const char *end);
+
int char_cmp(const void *a, const void *b);
+
const char *charv_search(charv_t *, const char *);

#endif
modified libpkg/utils.c
@@ -1120,3 +1120,19 @@ str_ends_with(const char *str, const char *end)
		return (false);
	return (strncmp(str + (sl - el), end, (sl - el)) == 0);
}
+

+
int
+
char_cmp(const void *a, const void *b) {
+
	return strcmp(*(char **)a, *(char **)b);
+
}
+

+
const char *
+
charv_search(charv_t *v, const char *el)
+
{
+
	if (v->len == 0)
+
		return (NULL);
+
	const char **res = bsearch(&el, v->d, v->len, sizeof(char *), char_cmp);
+
	if (res == NULL)
+
		return (NULL);
+
	return *res;
+
}
modified tests/lib/vec.c
@@ -13,6 +13,7 @@ ATF_TC_WITHOUT_HEAD(c_charv_t);
ATF_TC_WITHOUT_HEAD(c_charv_contains);
ATF_TC_WITHOUT_HEAD(charv_t);
ATF_TC_WITHOUT_HEAD(vec_remove_and_free);
+
ATF_TC_WITHOUT_HEAD(charv_search);

ATF_TC_BODY(c_charv_t, tc)
{
@@ -144,12 +145,25 @@ ATF_TC_BODY(vec_remove_and_free, tc)
	ATF_REQUIRE_STREQ(list.d[1], "test4");
}

+
ATF_TC_BODY(charv_search, tc)
+
{
+
	charv_t list;
+
	vec_init(&list);
+

+
	ATF_REQUIRE(charv_search(&list, "key") == NULL);
+
	vec_push(&list, xstrdup("bla"));
+
	ATF_REQUIRE(charv_search(&list, "key") == NULL);
+
	ATF_REQUIRE_STREQ(charv_search(&list, "bla"), "bla");
+
	vec_free_and_free(&list, free);
+
}
+

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, c_charv_t);
	ATF_TP_ADD_TC(tp, charv_t);
	ATF_TP_ADD_TC(tp, c_charv_contains);
	ATF_TP_ADD_TC(tp, vec_remove_and_free);
+
	ATF_TP_ADD_TC(tp, charv_search);

	return (atf_no_error());
}