]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
util: Split string functions into an extra file
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 10:04:54 +0000 (10:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 10:04:54 +0000 (10:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
38 files changed:
.gitignore
Makefile.am
src/libpakfire/arch.c
src/libpakfire/archive.c
src/libpakfire/build.c
src/libpakfire/cgroup.c
src/libpakfire/config.c
src/libpakfire/db.c
src/libpakfire/dependencies.c
src/libpakfire/dist.c
src/libpakfire/downloader.c
src/libpakfire/file.c
src/libpakfire/filelist.c
src/libpakfire/include/pakfire/string.h [new file with mode: 0644]
src/libpakfire/include/pakfire/util.h
src/libpakfire/jail.c
src/libpakfire/key.c
src/libpakfire/keystore.c
src/libpakfire/mount.c
src/libpakfire/package.c
src/libpakfire/packager.c
src/libpakfire/pakfire.c
src/libpakfire/parser.c
src/libpakfire/parser/grammar.y
src/libpakfire/parser/scanner.l
src/libpakfire/progressbar.c
src/libpakfire/pwd.c
src/libpakfire/repo.c
src/libpakfire/request.c
src/libpakfire/scriptlet.c
src/libpakfire/solution.c
src/libpakfire/string.c [new file with mode: 0644]
src/libpakfire/transaction.c
src/libpakfire/util.c
tests/libpakfire/compress.c
tests/libpakfire/repo.c
tests/libpakfire/string.c [new file with mode: 0644]
tests/libpakfire/util.c

index 0945ff6adcf3bd8b8fd66ca45779fa07fd958e16..2baa3d327d7a522fcc4b7c526aade490b153954e 100644 (file)
@@ -32,6 +32,7 @@
 /tests/libpakfire/progressbar
 /tests/libpakfire/repo
 /tests/libpakfire/snapshot
+/tests/libpakfire/string
 /tests/libpakfire/util
 /tmp
 *.py[co]
index 79735f6a1db3703e5ced0c7d30f605ebc3c1813c..4290355450085c7fa6530db7df63bd9300255509 100644 (file)
@@ -248,6 +248,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/scriptlet.c \
        src/libpakfire/snapshot.c \
        src/libpakfire/solution.c \
+       src/libpakfire/string.c \
        src/libpakfire/transaction.c \
        src/libpakfire/ui.c \
        src/libpakfire/util.c
@@ -288,6 +289,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/scriptlet.h \
        src/libpakfire/include/pakfire/snapshot.h \
        src/libpakfire/include/pakfire/solution.h \
+       src/libpakfire/include/pakfire/string.h \
        src/libpakfire/include/pakfire/transaction.h \
        src/libpakfire/include/pakfire/ui.h \
        src/libpakfire/include/pakfire/util.h
@@ -387,6 +389,7 @@ check_PROGRAMS += \
        tests/libpakfire/progressbar \
        tests/libpakfire/repo \
        tests/libpakfire/snapshot \
+       tests/libpakfire/string \
        tests/libpakfire/util
 
 dist_tests_libpakfire_main_SOURCES = \
@@ -606,6 +609,18 @@ tests_libpakfire_snapshot_CFLAGS = \
 tests_libpakfire_snapshot_LDADD = \
        $(TESTSUITE_LDADD)
 
+dist_tests_libpakfire_string_SOURCES = \
+       tests/libpakfire/string.c
+
+tests_libpakfire_string_CPPFLAGS = \
+       $(TESTSUITE_CPPFLAGS)
+
+tests_libpakfire_string_CFLAGS = \
+       $(TESTSUITE_CFLAGS)
+
+tests_libpakfire_string_LDADD = \
+       $(TESTSUITE_LDADD)
+
 dist_tests_libpakfire_util_SOURCES = \
        tests/libpakfire/util.c
 
index e3a5a20ae5ef96d849b3811c68dce886ea024e70..55978a584b69119b180edf608fbb443f087a8622 100644 (file)
@@ -32,6 +32,7 @@
 #include <pakfire/arch.h>
 #include <pakfire/constants.h>
 #include <pakfire/private.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_arch {
index 9a77fc407cb47dd9305ea801f07a501a18695ccc..3a0c8531b4f56091b67e91fe0fa69df5e1c1714e 100644 (file)
@@ -56,6 +56,7 @@
 #include <pakfire/pwd.h>
 #include <pakfire/repo.h>
 #include <pakfire/scriptlet.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 enum pakfire_archive_verify_flags {
index 377d1a19f6873ce80dbd80da5271402f77bb4dc6..044bce819136872536040bbe0b01bb4bed43b7a2 100644 (file)
@@ -41,6 +41,7 @@
 #include <pakfire/request.h>
 #include <pakfire/scriptlet.h>
 #include <pakfire/snapshot.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define CCACHE_DIR "/var/cache/ccache"
@@ -1036,7 +1037,7 @@ static int pakfire_build_install_packages(struct pakfire_build* build,
        }
 
        // Split requirements into packages
-       packages = pakfire_split_string(requires, ',');
+       packages = pakfire_string_split(requires, ',');
        if (!packages)
                goto ERROR;
 
index 2497bd21abc81be170c424c814ed58e4f760cef9..14062241a4148105fee4ca176cda8f9d9437c1ba 100644 (file)
@@ -27,6 +27,7 @@
 #include <pakfire/cgroup.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define ROOT                           "/sys/fs/cgroup"
index 92091971ccb864234b0c834c428b1250bc9033c3..c9b3b6c785628b80d6451407f67b551c8d84da47 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/queue.h>
 
 #include <pakfire/config.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define KEY_MAX_LENGTH         32
index 1a92d7dcf2c3f5d1be153e02b29ca3df4eeabf03..587d62b585194abb971e8abb4d18e3a007c96f73 100644 (file)
@@ -35,6 +35,7 @@
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define DATABASE_PATH PAKFIRE_PRIVATE_DIR "/packages.db"
index 904c4c3b7b2080db3727fd4051c186cb2a7c632f..a2b243c95d3cf90995cba7e2f3233e80498f9d6c 100644 (file)
@@ -29,6 +29,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 static const struct pakfire_rich_operation {
index 03160a8d1f75327942d8abc98fca9bdbe8cc056a..dd9ac18df327b06c4167b46ceb2771f90d790421 100644 (file)
@@ -35,6 +35,7 @@
 #include <pakfire/parser.h>
 #include <pakfire/private.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 // XXX for now
index 6954cc2a5a90200d07ad5405a0e5ce0199800bcd..70605950fe0cef2e16ba7f17792391bfa8391d1a 100644 (file)
@@ -34,6 +34,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/progressbar.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 // Retry a mirror up to N times before marking it as broken
index 29723197ef0ceeb95f64f0d154e222f39b5ecea2..7120ef44c559ff1628fc2ab2ca71970d6c8896c1 100644 (file)
@@ -34,6 +34,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define MAX_DIGESTS 4
index caad275d53a623033c8fdc1e49e70029b7eccc61..bef56c03b7f18e6dbb3cd4644f64af2618489e02 100644 (file)
@@ -31,6 +31,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_filelist {
diff --git a/src/libpakfire/include/pakfire/string.h b/src/libpakfire/include/pakfire/string.h
new file mode 100644 (file)
index 0000000..6baeaee
--- /dev/null
@@ -0,0 +1,51 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2022 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_STRING_H
+#define PAKFIRE_STRING_H
+
+#ifdef PAKFIRE_PRIVATE
+
+#define pakfire_string_format(s, fmt, ...) snprintf(s, sizeof(s) - 1, fmt, __VA_ARGS__)
+#define pakfire_string_set(s, value) pakfire_string_format(s, "%s", value)
+
+int pakfire_string_startswith(const char* s, const char* prefix);
+int pakfire_string_endswith(const char* s, const char* suffix);
+int pakfire_string_matches(const char* s, const char* pattern);
+
+int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2);
+char* pakfire_string_replace(const char* s, const char* pattern, const char* repl);
+char** pakfire_string_split(const char* s, char delim);
+char* pakfire_string_join(char** list, const char* delim);
+
+#define pakfire_format_size(dst, value) \
+       __pakfire_format_size(dst, sizeof(dst) - 1, value)
+int __pakfire_format_size(char* dst, size_t length, double value);
+int pakfire_format_speed(char* dst, size_t length, double value);
+char* pakfire_format_date(time_t t);
+
+#define pakfire_strftime_now(dest, format) \
+       __pakfire_strftime_now(dest, sizeof(dest) - 1, format)
+int __pakfire_strftime_now(char* dest, size_t length, const char* format)
+       __attribute__((format(strftime, 3, 0)));;
+
+#endif
+
+#endif /* PAKFIRE_STRING_H */
index d8cbc2cbc01fad4fdae731c766b0e1845a21dcf6..8150f085d5f0c4a89181102778f8511f5427f4af 100644 (file)
 
 #include <pakfire/pakfire.h>
 
-#define pakfire_string_format(s, fmt, ...) snprintf(s, sizeof(s) - 1, fmt, __VA_ARGS__)
-#define pakfire_string_set(s, value) pakfire_string_format(s, "%s", value)
-
-int pakfire_string_startswith(const char* s, const char* prefix);
-int pakfire_string_endswith(const char* s, const char* suffix);
-int pakfire_string_matches(const char* s, const char* pattern);
 char* pakfire_unquote_in_place(char* s);
-int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2);
-char* pakfire_string_replace(const char* s, const char* pattern, const char* repl);
-char** pakfire_split_string(const char* s, char delim);
-char* pakfire_string_join(char** list, const char* delim);
-
-#define pakfire_format_size(dst, value) \
-       __pakfire_format_size(dst, sizeof(dst) - 1, value)
-int __pakfire_format_size(char* dst, size_t length, double value);
-int pakfire_format_speed(char* dst, size_t length, double value);
-char* pakfire_format_date(time_t t);
-
-#define pakfire_strftime_now(dest, format) \
-       __pakfire_strftime_now(dest, sizeof(dest) - 1, format)
-int __pakfire_strftime_now(char* dest, size_t length, const char* format)
-       __attribute__((format(strftime, 3, 0)));;
 
 int pakfire_path_exists(const char* path);
 time_t pakfire_path_age(const char* path);
index 4321512b036db8ec69ebe9330876e4a4437a1cf3..f2aac5fd677fa8d218ff2854cfe154ef111e5a76 100644 (file)
@@ -50,6 +50,7 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
 #include <pakfire/pwd.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define BUFFER_SIZE      1024 * 64
index cb81057b6502d0f6e9aa6d4e3aaf56fa17d75c34..66f52ac03aa14eddbb35666e454f4fe81e9c4cfe 100644 (file)
@@ -32,6 +32,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_key {
index cab2f53fc160e5c43d88cccc16851ebecdfbf9f2..38247b5b8d00764ca2ec6506c4e5261001468ac0 100644 (file)
@@ -27,6 +27,7 @@
 #include <pakfire/keystore.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 static int pakfire_init_gpgme(struct pakfire* pakfire) {
index c7e0191687ebe825bca7cb16b62947a85d151b02..025c41d160ca30994e642d43154f348d253141c9 100644 (file)
@@ -33,6 +33,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/mount.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 static const struct pakfire_mountpoint {
index 24a9be74f53e537bdf37b541a3d642b3c4b0a485..2671c5ebcbe430aaa69c2199e52fa4bf2f33b3c9 100644 (file)
@@ -43,6 +43,7 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_package {
@@ -550,7 +551,7 @@ PAKFIRE_EXPORT char* pakfire_package_get_groups(struct pakfire_package* pkg) {
 }
 
 PAKFIRE_EXPORT void pakfire_package_set_groups(struct pakfire_package* pkg, const char* groups) {
-       char** list = pakfire_split_string(groups, ' ');
+       char** list = pakfire_string_split(groups, ' ');
        if (!list)
                return;
 
@@ -1011,7 +1012,7 @@ static void pakfire_package_dump_add_line(char** str, const char* key, const cha
 }
 
 static void pakfire_package_dump_add_lines(char** str, const char* key, const char* val) {
-       char** lines = pakfire_split_string(val, '\n');
+       char** lines = pakfire_string_split(val, '\n');
        if (!lines)
                return;
 
index fc98f4b0c6cd177a218cac65c6b0980d898c1ba4..ad10c1afe91fcf2cae61d1c635ce2a88c9b5d620 100644 (file)
@@ -39,6 +39,7 @@
 #include <pakfire/packager.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/pwd.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_packager {
index d0ecb100b40414ee48ef534fec180fe5f62d7236..a13a861e6adb3091a8827e45d6c6291dcd0e84e8 100644 (file)
@@ -57,6 +57,7 @@
 #include <pakfire/pwd.h>
 #include <pakfire/repo.h>
 #include <pakfire/request.h>
+#include <pakfire/string.h>
 #include <pakfire/transaction.h>
 #include <pakfire/ui.h>
 #include <pakfire/util.h>
index fbea82c1c55ca39d307a4a6b40d81a99591b9134..ee1509cd1cc18ac5a945656b0a0250806988eca3 100644 (file)
@@ -36,6 +36,7 @@
 #include <pakfire/package.h>
 #include <pakfire/parser.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_parser {
@@ -663,7 +664,7 @@ char** pakfire_parser_get_split(struct pakfire_parser* parser,
                return NULL;
 
        // Split the string
-       char** list = pakfire_split_string(value, delim);
+       char** list = pakfire_string_split(value, delim);
        free(value);
 
        return list;
index ff156bf296d9efad67a48276cdb003f17f930d0e..d22317e2621b16ea64ed6c1e5247be9332b21f30 100644 (file)
@@ -44,6 +44,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/parser.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define YYERROR_VERBOSE 1
index cf1fbfedcec4768102913973a1d7eec270fac06f..d92570673c1c8608b4bc4948ade84449c6d31db2 100644 (file)
@@ -38,6 +38,7 @@ int num_lines;
 #include <stdlib.h>
 
 #include <pakfire/parser.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #include "grammar.h"
index a7d2c1e531c9204307a492062570c0fd8bd24d34..01edfcb34575f197ba5efe6105c1ff29d83c9a02 100644 (file)
@@ -32,6 +32,7 @@
 #include <pakfire/i18n.h>
 #include <pakfire/private.h>
 #include <pakfire/progressbar.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define DRAWS_PER_SECOND 20
index 33d4b5f47b4eee999c7ec5b04b4fdbfcdd9e0e61..b3ce65635f42ddc225c2a09567216f792a738dc2 100644 (file)
@@ -27,6 +27,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/pwd.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define ETC_SUBUID "/etc/subuid"
index f12ead7277caf88f0845bf4fa3a17945c53c8fe1..138aa5427a297eb23987abccd86c1a6b3bb75d26 100644 (file)
@@ -45,6 +45,7 @@
 #include <pakfire/private.h>
 #include <pakfire/progressbar.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 // Refresh mirrorlists once a day
index 79b618603642495e9bc6fc723dbb4f0947fe7d57..4642db6578561885f58961ee1a8791a426bc55ef 100644 (file)
@@ -41,6 +41,7 @@
 #include <pakfire/private.h>
 #include <pakfire/problem.h>
 #include <pakfire/request.h>
+#include <pakfire/string.h>
 #include <pakfire/transaction.h>
 #include <pakfire/util.h>
 
index 8b41646f6391378725ac0e8eda8eb79f86e7fc10..e9c2aae977ac99b67b3b32d5405de7742504ec76 100644 (file)
@@ -25,6 +25,7 @@
 #include <pakfire/jail.h>
 #include <pakfire/logging.h>
 #include <pakfire/scriptlet.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 const char* pakfire_scriptlet_types[] = {
index 3eb825bd290a176362e236bdd023307f1bb4da0e..cad749a34988134ad24fb643613958bc1b2e3374 100644 (file)
@@ -29,6 +29,7 @@
 #include <pakfire/problem.h>
 #include <pakfire/request.h>
 #include <pakfire/solution.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 struct pakfire_solution {
diff --git a/src/libpakfire/string.c b/src/libpakfire/string.c
new file mode 100644 (file)
index 0000000..60b78ab
--- /dev/null
@@ -0,0 +1,362 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2022 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include <pakfire/string.h>
+
+int pakfire_string_startswith(const char* s, const char* prefix) {
+       // Validate input
+       if (!s || !prefix) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       return !strncmp(s, prefix, strlen(prefix));
+}
+
+int pakfire_string_endswith(const char* s, const char* suffix) {
+       // Validate input
+       if (!s || !suffix) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       return !strcmp(s + strlen(s) - strlen(suffix), suffix);
+}
+
+int pakfire_string_matches(const char* s, const char* pattern) {
+       // Validate input
+       if (!s || !pattern) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       return !!strstr(s, pattern);
+}
+
+int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2) {
+       char* p = strstr(s, delim);
+
+       // Delim was not found
+       if (!p) {
+               *s1 = NULL;
+               *s2 = NULL;
+               return 1;
+       }
+
+       // Length of string before delim
+       size_t l = p - s;
+
+       char* buffer = malloc(l + 1);
+       if (!buffer)
+               return 1;
+
+       // Copy first part
+       *s1 = memcpy(buffer, s, l);
+       buffer[l] = '\0';
+
+       // Copy second part
+       *s2 = strdup(p + strlen(delim));
+
+       return 0;
+}
+
+char* pakfire_string_replace(const char* s, const char* pattern, const char* repl) {
+       // Return NULL on no input or no pattern
+       if (!s || !pattern) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       // Replace with nothing when repl is NULL
+       if (!repl)
+               repl = "";
+
+       char* result = NULL;
+       const char** cache = NULL;
+       unsigned int count = 0;
+
+       const size_t pattern_length = strlen(pattern);
+
+       // Working pointer
+       const char* p = s;
+
+       // Find all occurrences of pattern and store their location
+       while (1) {
+               const char* needle = strstr(p, pattern);
+               if (!needle)
+                       break;
+
+               // Make space in the cache
+               cache = reallocarray(cache, sizeof(*cache), count + 1);
+               cache[count++] = needle;
+
+               // Move p forward
+               p = needle + pattern_length;
+       }
+
+       // Copy the string if no occurence was found
+       if (count == 0) {
+               result = strdup(s);
+               goto ERROR;
+       }
+
+       // Store the end pointer
+       cache = reallocarray(cache, sizeof(*cache), count + 1);
+       cache[count] = s + strlen(s);
+
+       const size_t repl_length = strlen(repl);
+
+       // Determine the length of the final string
+       const size_t length = strlen(s) + ((repl_length - pattern_length) * count);
+
+       // Allocate enough memory for the result
+       result = malloc(length + 1);
+       if (!result)
+               goto ERROR;
+
+       // Reset p
+       p = s;
+
+       // Working pointer for the result
+       char* r = result;
+
+       // Copy everything up to the first match
+       ssize_t l = cache[0] - s;
+       memcpy(r, p, l);
+       r += l;
+       p += l;
+
+       for (unsigned int i = 0; i < count; i++) {
+               // Put replacement here
+               memcpy(r, repl, repl_length);
+               r += repl_length;
+               p += pattern_length;
+
+               // Determine the length between two matches
+               l = cache[i+1] - (cache[i] + pattern_length);
+
+               memcpy(r, p, l);
+               r += l;
+               p += l;
+       }
+
+       // Terminate the string
+       result[length] = '\0';
+
+ERROR:
+       if (cache)
+               free(cache);
+
+       return result;
+}
+
+static unsigned int pakfire_chrcnt(const char* s, char delim) {
+       size_t length = strlen(s);
+
+       unsigned int count = 0;
+
+       for (unsigned int i = 0; i < length; i++)
+               if (s[i] == delim)
+                       count++;
+
+       return count;
+}
+
+char** pakfire_string_split(const char* s, char delim) {
+       char** array = NULL;
+
+       if (!s) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       // Count how often we need to split
+       unsigned int count = pakfire_chrcnt(s, delim) + 1;
+
+       // Allocate array
+       array = calloc(count + 1, sizeof(*array));
+       if (!array)
+               return NULL;
+
+       // Copy string to stack
+       char* p = strdupa(s);
+       if (!p)
+               return NULL;
+
+       unsigned int i = 0;
+       while (*p) {
+               char* e = strchr(p, delim);
+
+               // Terminate the string
+               if (e)
+                       *e = '\0';
+
+               // Add string to the array
+               array[i++] = strdup(p);
+
+               // End loop when we reached the end
+               if (!e)
+                       break;
+
+               // Or continue at the next line
+               p = e + 1;
+       }
+
+       return array;
+}
+
+char* pakfire_string_join(char** list, const char* delim) {
+       // Validate input
+       if (!list || !delim) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       size_t length = 0;
+       unsigned int elements = 0;
+
+       // Count the number of elements and the total length
+       for (char** item = list; *item; item++) {
+               length += strlen(*item);
+               elements++;
+       }
+
+       // Empty list?
+       if (!elements)
+               return NULL;
+
+       // Add the delimiters
+       length += strlen(delim) * (elements - 1);
+
+       // Allocate the result string
+       char* string = malloc(length + 1);
+       if (!string)
+               return NULL;
+
+       // Pointer to where we are writing
+       char* p = string;
+
+       size_t bytes_left = length + 1;
+       size_t bytes_written;
+
+       for (char** item = list; *item; item++) {
+               bytes_written = snprintf(p, bytes_left, "%s", *item);
+
+               bytes_left -= bytes_written;
+               p += bytes_written;
+
+               // Write the delimiter
+               if (bytes_left) {
+                       bytes_written = snprintf(p, bytes_left, "%s", delim);
+
+                       bytes_left -= bytes_written;
+                       p += bytes_written;
+               }
+       }
+
+       return string;
+}
+
+int __pakfire_format_size(char* dst, size_t length, double value) {
+       const char* units[] = {
+               "%.0f ",
+               "%.0fk",
+               "%.1fM",
+               "%.1fG",
+               "%.1fT",
+               NULL
+       };
+       const char** unit = units;
+
+       while (*(unit + 1) && value >= 1024.0) {
+               value /= 1024.0;
+               unit++;
+       }
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+       return snprintf(dst, length, *unit, value);
+#pragma GCC diagnostic pop
+}
+
+int pakfire_format_speed(char* dst, size_t length, double value) {
+       const char* units[] = {
+               "%4.0fB/s",
+               "%4.0fkB/s",
+               "%4.1fMB/s",
+               "%4.1fGB/s",
+               "%4.1fTB/s",
+               NULL
+       };
+       const char** unit = units;
+
+       while (*(unit + 1) && value >= 1024.0) {
+               value /= 1024.0;
+               unit++;
+       }
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+       return snprintf(dst, length, *unit, value);
+#pragma GCC diagnostic pop
+}
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+static char* pakfire_strftime(const char* format, time_t t) {
+       char string[128];
+       struct tm* tm = gmtime(&t);
+
+       strftime(string, sizeof(string) - 1, format, tm);
+
+       return strdup(string);
+}
+#pragma GCC diagnostic pop
+
+char* pakfire_format_date(time_t t) {
+       return pakfire_strftime("%Y-%m-%d", t);
+}
+
+int __pakfire_strftime_now(char* dest, size_t length, const char* format) {
+       struct tm tm;
+
+       // Fetch the current time
+       const time_t t = time(NULL);
+       if (t < 0)
+               return 1;
+
+       // Convert to struct tm
+       struct tm* now = gmtime_r(&t, &tm);
+       if (!now)
+               return 1;
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+       strftime(dest, length, format, now);
+#pragma GCC diagnostic pop
+
+       return 0;
+}
index 373efe4515fed5485e853ad2d7ad06c76279baf7..fb47e5a47cebfadab8fb2713c40f22744bdf12b2 100644 (file)
@@ -39,6 +39,7 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/transaction.h>
 #include <pakfire/ui.h>
 #include <pakfire/util.h>
index 0ff1107bdc7e877c763e14c78d1084536993e9a0..436b31623aaec7c9b1c135ba03fa181b0aac2ef4 100644 (file)
 #include <pakfire/constants.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #define BUFFER_SIZE 64 * 1024
 #define NSEC_PER_SEC 1000000000
 
-int pakfire_string_startswith(const char* s, const char* prefix) {
-       // Validate input
-       if (!s || !prefix) {
-               errno = EINVAL;
-               return 1;
-       }
-
-       return !strncmp(s, prefix, strlen(prefix));
-}
-
-int pakfire_string_endswith(const char* s, const char* suffix) {
-       // Validate input
-       if (!s || !suffix) {
-               errno = EINVAL;
-               return 1;
-       }
-
-       return !strcmp(s + strlen(s) - strlen(suffix), suffix);
-}
-
-int pakfire_string_matches(const char* s, const char* pattern) {
-       // Validate input
-       if (!s || !pattern) {
-               errno = EINVAL;
-               return 1;
-       }
-
-       return !!strstr(s, pattern);
-}
-
 char* pakfire_unquote_in_place(char* s) {
        if (!s || !*s)
                return s;
@@ -100,311 +71,6 @@ char* pakfire_unquote_in_place(char* s) {
        return s;
 }
 
-int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2) {
-       char* p = strstr(s, delim);
-
-       // Delim was not found
-       if (!p) {
-               *s1 = NULL;
-               *s2 = NULL;
-               return 1;
-       }
-
-       // Length of string before delim
-       size_t l = p - s;
-
-       char* buffer = malloc(l + 1);
-       if (!buffer)
-               return -ENOMEM;
-
-       // Copy first part
-       *s1 = memcpy(buffer, s, l);
-       buffer[l] = '\0';
-
-       // Copy second part
-       *s2 = strdup(p + strlen(delim));
-
-       return 0;
-}
-
-char* pakfire_string_replace(const char* s, const char* pattern, const char* repl) {
-       // Return NULL on no input or no pattern
-       if (!s || !pattern) {
-               errno = EINVAL;
-               return NULL;
-       }
-
-       // Replace with nothing when repl is NULL
-       if (!repl)
-               repl = "";
-
-       char* result = NULL;
-       const char** cache = NULL;
-       unsigned int count = 0;
-
-       const size_t pattern_length = strlen(pattern);
-
-       // Working pointer
-       const char* p = s;
-
-       // Find all occurrences of pattern and store their location
-       while (1) {
-               const char* needle = strstr(p, pattern);
-               if (!needle)
-                       break;
-
-               // Make space in the cache
-               cache = reallocarray(cache, sizeof(*cache), count + 1);
-               cache[count++] = needle;
-
-               // Move p forward
-               p = needle + pattern_length;
-       }
-
-       // Copy the string if no occurence was found
-       if (count == 0) {
-               result = strdup(s);
-               goto ERROR;
-       }
-
-       // Store the end pointer
-       cache = reallocarray(cache, sizeof(*cache), count + 1);
-       cache[count] = s + strlen(s);
-
-       const size_t repl_length = strlen(repl);
-
-       // Determine the length of the final string
-       const size_t length = strlen(s) + ((repl_length - pattern_length) * count);
-
-       // Allocate enough memory for the result
-       result = malloc(length + 1);
-       if (!result)
-               goto ERROR;
-
-       // Reset p
-       p = s;
-
-       // Working pointer for the result
-       char* r = result;
-
-       // Copy everything up to the first match
-       ssize_t l = cache[0] - s;
-       memcpy(r, p, l);
-       r += l;
-       p += l;
-
-       for (unsigned int i = 0; i < count; i++) {
-               // Put replacement here
-               memcpy(r, repl, repl_length);
-               r += repl_length;
-               p += pattern_length;
-
-               // Determine the length between two matches
-               l = cache[i+1] - (cache[i] + pattern_length);
-
-               memcpy(r, p, l);
-               r += l;
-               p += l;
-       }
-
-       // Terminate the string
-       result[length] = '\0';
-
-ERROR:
-       if (cache)
-               free(cache);
-
-       return result;
-}
-
-static unsigned int pakfire_chrcnt(const char* s, char delim) {
-       size_t length = strlen(s);
-
-       unsigned int count = 0;
-
-       for (unsigned int i = 0; i < length; i++)
-               if (s[i] == delim)
-                       count++;
-
-       return count;
-}
-
-char** pakfire_split_string(const char* s, char delim) {
-       char** array = NULL;
-
-       if (!s) {
-               errno = EINVAL;
-               return NULL;
-       }
-
-       // Count how often we need to split
-       unsigned int count = pakfire_chrcnt(s, delim) + 1;
-
-       // Allocate array
-       array = calloc(count + 1, sizeof(*array));
-       if (!array)
-               return NULL;
-
-       // Copy string to stack
-       char* p = strdupa(s);
-       if (!p)
-               return NULL;
-
-       unsigned int i = 0;
-       while (*p) {
-               char* e = strchr(p, delim);
-
-               // Terminate the string
-               if (e)
-                       *e = '\0';
-
-               // Add string to the array
-               array[i++] = strdup(p);
-
-               // End loop when we reached the end
-               if (!e)
-                       break;
-
-               // Or continue at the next line
-               p = e + 1;
-       }
-
-       return array;
-}
-
-char* pakfire_string_join(char** list, const char* delim) {
-       // Validate input
-       if (!list || !delim) {
-               errno = EINVAL;
-               return NULL;
-       }
-
-       size_t length = 0;
-       unsigned int elements = 0;
-
-       // Count the number of elements and the total length
-       for (char** item = list; *item; item++) {
-               length += strlen(*item);
-               elements++;
-       }
-
-       // Empty list?
-       if (!elements)
-               return NULL;
-
-       // Add the delimiters
-       length += strlen(delim) * (elements - 1);
-
-       // Allocate the result string
-       char* string = malloc(length + 1);
-       if (!string)
-               return NULL;
-
-       // Pointer to where we are writing
-       char* p = string;
-
-       size_t bytes_left = length + 1;
-       size_t bytes_written;
-
-       for (char** item = list; *item; item++) {
-               bytes_written = snprintf(p, bytes_left, "%s", *item);
-
-               bytes_left -= bytes_written;
-               p += bytes_written;
-
-               // Write the delimiter
-               if (bytes_left) {
-                       bytes_written = snprintf(p, bytes_left, "%s", delim);
-
-                       bytes_left -= bytes_written;
-                       p += bytes_written;
-               }
-       }
-
-       return string;
-}
-
-int __pakfire_format_size(char* dst, size_t length, double value) {
-       const char* units[] = {
-               "%.0f ",
-               "%.0fk",
-               "%.1fM",
-               "%.1fG",
-               "%.1fT",
-               NULL
-       };
-       const char** unit = units;
-
-       while (*(unit + 1) && value >= 1024.0) {
-               value /= 1024.0;
-               unit++;
-       }
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-       return snprintf(dst, length, *unit, value);
-#pragma GCC diagnostic pop
-}
-
-int pakfire_format_speed(char* dst, size_t length, double value) {
-       const char* units[] = {
-               "%4.0fB/s",
-               "%4.0fkB/s",
-               "%4.1fMB/s",
-               "%4.1fGB/s",
-               "%4.1fTB/s",
-               NULL
-       };
-       const char** unit = units;
-
-       while (*(unit + 1) && value >= 1024.0) {
-               value /= 1024.0;
-               unit++;
-       }
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-       return snprintf(dst, length, *unit, value);
-#pragma GCC diagnostic pop
-}
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-static char* pakfire_strftime(const char* format, time_t t) {
-       char string[128];
-       struct tm* tm = gmtime(&t);
-
-       strftime(string, sizeof(string) - 1, format, tm);
-
-       return strdup(string);
-}
-#pragma GCC diagnostic pop
-
-char* pakfire_format_date(time_t t) {
-       return pakfire_strftime("%Y-%m-%d", t);
-}
-
-int __pakfire_strftime_now(char* dest, size_t length, const char* format) {
-       struct tm tm;
-
-       // Fetch the current time
-       const time_t t = time(NULL);
-       if (t < 0)
-               return 1;
-
-       // Convert to struct tm
-       struct tm* now = gmtime_r(&t, &tm);
-       if (!now)
-               return 1;
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-       strftime(dest, length, format, now);
-#pragma GCC diagnostic pop
-
-       return 0;
-}
-
 int __pakfire_path_join(char* dest, size_t length,
                const char* first, const char* second) {
        if (!first)
index a55c6aaaac75c2364cb12f41bc7112230f329abe..880f085d366091455671cc10dd4f84820c4b4193 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include <pakfire/compress.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #include "../testsuite.h"
index 0582d71c6576d8baff16145a2aa7663456f67c79..b536e1da07a3216ece0c2447efe836e1eec54756 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #include "../testsuite.h"
diff --git a/tests/libpakfire/string.c b/tests/libpakfire/string.c
new file mode 100644 (file)
index 0000000..238e666
--- /dev/null
@@ -0,0 +1,287 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2022 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <pakfire/string.h>
+
+#include "../testsuite.h"
+
+static int test_string_startswith(const struct test* t) {
+       ASSERT_TRUE(pakfire_string_startswith("ABC", "A"));
+       ASSERT_FALSE(pakfire_string_startswith("ABC", "B"));
+
+       // Check for invalid inputs
+       ASSERT_ERRNO(pakfire_string_startswith("ABC", NULL), EINVAL);
+       ASSERT_ERRNO(pakfire_string_startswith(NULL, "ABC"), EINVAL);
+       ASSERT_ERRNO(pakfire_string_startswith(NULL, NULL), EINVAL);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+static int test_string_endswith(const struct test* t) {
+       ASSERT_TRUE(pakfire_string_endswith("ABC", "C"));
+       ASSERT_FALSE(pakfire_string_endswith("ABC", "B"));
+
+       // Check for invalid inputs
+       ASSERT_ERRNO(pakfire_string_endswith("ABC", NULL), EINVAL);
+       ASSERT_ERRNO(pakfire_string_endswith(NULL, "ABC"), EINVAL);
+       ASSERT_ERRNO(pakfire_string_endswith(NULL, NULL), EINVAL);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+static int test_string_matches(const struct test* t) {
+       ASSERT_TRUE(pakfire_string_matches("ABC", "A"));
+       ASSERT_TRUE(pakfire_string_matches("ABC", "B"));
+       ASSERT_TRUE(pakfire_string_matches("ABC", "C"));
+       ASSERT_TRUE(pakfire_string_matches("ABC", "AB"));
+       ASSERT_TRUE(pakfire_string_matches("ABC", "BC"));
+       ASSERT_TRUE(pakfire_string_matches("ABC", "ABC"));
+       ASSERT_FALSE(pakfire_string_matches("ABC", "D"));
+       ASSERT_FALSE(pakfire_string_matches("ABC", "ABCD"));
+
+       // Check for invalid inputs
+       ASSERT_ERRNO(pakfire_string_matches("ABC", NULL), EINVAL);
+       ASSERT_ERRNO(pakfire_string_matches(NULL, "ABC"), EINVAL);
+       ASSERT_ERRNO(pakfire_string_matches(NULL, NULL), EINVAL);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+static int test_string_partition(const struct test* t) {
+       char* part1;
+       char* part2;
+
+       // Regular case
+       int r = pakfire_string_partition("ABC:DEF", ":", &part1, &part2);
+       ASSERT(r == 0);
+       ASSERT_STRING_EQUALS(part1, "ABC");
+       ASSERT_STRING_EQUALS(part2, "DEF");
+
+       free(part1);
+       free(part2);
+
+       // No delimiter
+       r = pakfire_string_partition("ABCDEF", ":", &part1, &part2);
+       ASSERT(r == 1);
+       ASSERT(part1 == NULL);
+       ASSERT(part2 == NULL);
+
+       // Nothing after the delimiter
+       r = pakfire_string_partition("ABC:", ":", &part1, &part2);
+       ASSERT(r == 0);
+       ASSERT_STRING_EQUALS(part1, "ABC");
+       ASSERT_STRING_EQUALS(part2, "");
+
+       free(part1);
+       free(part2);
+
+       // Nothing before the delimiter
+       r = pakfire_string_partition(":ABC", ":", &part1, &part2);
+       ASSERT(r == 0);
+       ASSERT_STRING_EQUALS(part1, "");
+       ASSERT_STRING_EQUALS(part2, "ABC");
+
+       free(part1);
+       free(part2);
+
+       // Multi-character delimiter
+       r = pakfire_string_partition("ABC:-:DEF", ":-:", &part1, &part2);
+       ASSERT(r == 0);
+       ASSERT_STRING_EQUALS(part1, "ABC");
+       ASSERT_STRING_EQUALS(part2, "DEF");
+
+       free(part1);
+       free(part2);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+static int test_string_replace(const struct test* t) {
+       const char* result = pakfire_string_replace(
+               "ABCABCABCABC", "AB", "CC"
+       );
+       ASSERT_STRING_EQUALS(result, "CCCCCCCCCCCC");
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+static int test_string_split(const struct test* t) {
+       char** result = pakfire_string_split(NULL, 'X');
+
+       // Must return on invalid input
+       ASSERT_ERRNO(!result, EINVAL);
+
+       // Split a string
+       result = pakfire_string_split("ABCXABCXABC", 'X');
+       ASSERT(result);
+
+       ASSERT_STRING_EQUALS(result[0], "ABC");
+       ASSERT_STRING_EQUALS(result[1], "ABC");
+       ASSERT_STRING_EQUALS(result[2], "ABC");
+       ASSERT_NULL(result[3]);
+
+       // Split a string withtout the delimiter
+       result = pakfire_string_split("ABCABC", 'X');
+       ASSERT(result);
+
+       ASSERT_STRING_EQUALS(result[0], "ABCABC");
+       ASSERT_NULL(result[1]);
+
+       // String with only delimiters
+       result = pakfire_string_split("XXXX", 'X');
+       ASSERT(result);
+
+       ASSERT_STRING_EQUALS(result[0], "");
+       ASSERT_STRING_EQUALS(result[1], "");
+       ASSERT_STRING_EQUALS(result[2], "");
+       ASSERT_STRING_EQUALS(result[3], "");
+       ASSERT_NULL(result[4]);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+static int test_string_join(const struct test* t) {
+       char* s = NULL;
+
+       // Some test elements
+       char* elements1[] = {
+               "A",
+               "B",
+               "C",
+               NULL,
+       };
+
+       // Join with newline
+       s = pakfire_string_join(elements1, "\n");
+       ASSERT_STRING_EQUALS(s, "A\nB\nC");
+
+       if (s) {
+               free(s);
+               s = NULL;
+       }
+
+       // Join with empty delimiter
+       s = pakfire_string_join(elements1, "");
+       ASSERT_STRING_EQUALS(s, "ABC");
+
+       if (s) {
+               free(s);
+               s = NULL;
+       }
+
+       char* elements2[] = {
+               "",
+               "",
+               "",
+               NULL,
+       };
+
+       // Join list with empty elements
+       s = pakfire_string_join(elements2, "X");
+       ASSERT_STRING_EQUALS(s, "XX");
+
+       if (s) {
+               free(s);
+               s = NULL;
+       }
+
+       // Invalid inputs
+       s = pakfire_string_join(NULL, NULL);
+       ASSERT_ERRNO(!s, EINVAL);
+
+       s = pakfire_string_join(elements1, NULL);
+       ASSERT_ERRNO(!s, EINVAL);
+
+       s = pakfire_string_join(NULL, "\n");
+       ASSERT_ERRNO(!s, EINVAL);
+
+       char* elements3[] = {
+               NULL,
+       };
+
+       // Join empty elements
+       ASSERT_NULL(pakfire_string_join(elements3, "\n"));
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       if (s)
+               free(s);
+
+       return EXIT_FAILURE;
+}
+
+static int test_format_size(const struct test* t) {
+       char buffer[128];
+       char small_buffer[2];
+       int r;
+
+       ASSERT(pakfire_format_size(buffer, 0) == 2);
+       ASSERT_STRING_EQUALS(buffer, "0 ");
+
+       ASSERT(pakfire_format_size(buffer, 1024) == 2);
+       ASSERT_STRING_EQUALS(buffer, "1k");
+
+       ASSERT(pakfire_format_size(buffer, 1024 * 1024) == 4);
+       ASSERT_STRING_EQUALS(buffer, "1.0M");
+
+       ASSERT(pakfire_format_size(small_buffer, 0) == 2);
+       ASSERT_STRING_EQUALS(small_buffer, "");
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
+int main(int argc, const char* argv[]) {
+       testsuite_add_test(test_string_startswith);
+       testsuite_add_test(test_string_endswith);
+       testsuite_add_test(test_string_matches);
+       testsuite_add_test(test_string_partition);
+       testsuite_add_test(test_string_replace);
+       testsuite_add_test(test_string_split);
+       testsuite_add_test(test_string_join);
+       testsuite_add_test(test_format_size);
+
+       return testsuite_run(argc, argv);
+}
index 8f8ec1f2a73a6bc8b024f6fc42ca270eb74d9d29..d6543ecb8fc9cb317c2993c20331e697598b354c 100644 (file)
 #                                                                             #
 #############################################################################*/
 
-#include <errno.h>
 #include <stdlib.h>
-#include <string.h>
 
-#include <pakfire/parser.h>
 #include <pakfire/util.h>
 
 #include "../testsuite.h"
@@ -53,264 +50,9 @@ FAIL:
        return EXIT_FAILURE;
 }
 
-static int test_string_startswith(const struct test* t) {
-       ASSERT_TRUE(pakfire_string_startswith("ABC", "A"));
-       ASSERT_FALSE(pakfire_string_startswith("ABC", "B"));
-
-       // Check for invalid inputs
-       ASSERT_ERRNO(pakfire_string_startswith("ABC", NULL), EINVAL);
-       ASSERT_ERRNO(pakfire_string_startswith(NULL, "ABC"), EINVAL);
-       ASSERT_ERRNO(pakfire_string_startswith(NULL, NULL), EINVAL);
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
-static int test_string_endswith(const struct test* t) {
-       ASSERT_TRUE(pakfire_string_endswith("ABC", "C"));
-       ASSERT_FALSE(pakfire_string_endswith("ABC", "B"));
-
-       // Check for invalid inputs
-       ASSERT_ERRNO(pakfire_string_endswith("ABC", NULL), EINVAL);
-       ASSERT_ERRNO(pakfire_string_endswith(NULL, "ABC"), EINVAL);
-       ASSERT_ERRNO(pakfire_string_endswith(NULL, NULL), EINVAL);
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
-static int test_string_matches(const struct test* t) {
-       ASSERT_TRUE(pakfire_string_matches("ABC", "A"));
-       ASSERT_TRUE(pakfire_string_matches("ABC", "B"));
-       ASSERT_TRUE(pakfire_string_matches("ABC", "C"));
-       ASSERT_TRUE(pakfire_string_matches("ABC", "AB"));
-       ASSERT_TRUE(pakfire_string_matches("ABC", "BC"));
-       ASSERT_TRUE(pakfire_string_matches("ABC", "ABC"));
-       ASSERT_FALSE(pakfire_string_matches("ABC", "D"));
-       ASSERT_FALSE(pakfire_string_matches("ABC", "ABCD"));
-
-       // Check for invalid inputs
-       ASSERT_ERRNO(pakfire_string_matches("ABC", NULL), EINVAL);
-       ASSERT_ERRNO(pakfire_string_matches(NULL, "ABC"), EINVAL);
-       ASSERT_ERRNO(pakfire_string_matches(NULL, NULL), EINVAL);
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
-static int test_string_partition(const struct test* t) {
-       char* part1;
-       char* part2;
-
-       // Regular case
-       int r = pakfire_string_partition("ABC:DEF", ":", &part1, &part2);
-       ASSERT(r == 0);
-       ASSERT_STRING_EQUALS(part1, "ABC");
-       ASSERT_STRING_EQUALS(part2, "DEF");
-
-       free(part1);
-       free(part2);
-
-       // No delimiter
-       r = pakfire_string_partition("ABCDEF", ":", &part1, &part2);
-       ASSERT(r == 1);
-       ASSERT(part1 == NULL);
-       ASSERT(part2 == NULL);
-
-       // Nothing after the delimiter
-       r = pakfire_string_partition("ABC:", ":", &part1, &part2);
-       ASSERT(r == 0);
-       ASSERT_STRING_EQUALS(part1, "ABC");
-       ASSERT_STRING_EQUALS(part2, "");
-
-       free(part1);
-       free(part2);
-
-       // Nothing before the delimiter
-       r = pakfire_string_partition(":ABC", ":", &part1, &part2);
-       ASSERT(r == 0);
-       ASSERT_STRING_EQUALS(part1, "");
-       ASSERT_STRING_EQUALS(part2, "ABC");
-
-       free(part1);
-       free(part2);
-
-       // Multi-character delimiter
-       r = pakfire_string_partition("ABC:-:DEF", ":-:", &part1, &part2);
-       ASSERT(r == 0);
-       ASSERT_STRING_EQUALS(part1, "ABC");
-       ASSERT_STRING_EQUALS(part2, "DEF");
-
-       free(part1);
-       free(part2);
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
-static int test_string_replace(const struct test* t) {
-       const char* result = pakfire_string_replace(
-               "ABCABCABCABC", "AB", "CC"
-       );
-       ASSERT_STRING_EQUALS(result, "CCCCCCCCCCCC");
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
-static int test_string_split(const struct test* t) {
-       char** result = pakfire_split_string(NULL, 'X');
-
-       // Must return on invalid input
-       ASSERT_ERRNO(!result, EINVAL);
-
-       // Split a string
-       result = pakfire_split_string("ABCXABCXABC", 'X');
-       ASSERT(result);
-
-       ASSERT_STRING_EQUALS(result[0], "ABC");
-       ASSERT_STRING_EQUALS(result[1], "ABC");
-       ASSERT_STRING_EQUALS(result[2], "ABC");
-       ASSERT_NULL(result[3]);
-
-       // Split a string withtout the delimiter
-       result = pakfire_split_string("ABCABC", 'X');
-       ASSERT(result);
-
-       ASSERT_STRING_EQUALS(result[0], "ABCABC");
-       ASSERT_NULL(result[1]);
-
-       // String with only delimiters
-       result = pakfire_split_string("XXXX", 'X');
-       ASSERT(result);
-
-       ASSERT_STRING_EQUALS(result[0], "");
-       ASSERT_STRING_EQUALS(result[1], "");
-       ASSERT_STRING_EQUALS(result[2], "");
-       ASSERT_STRING_EQUALS(result[3], "");
-       ASSERT_NULL(result[4]);
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
-static int test_string_join(const struct test* t) {
-       char* s = NULL;
-
-       // Some test elements
-       char* elements1[] = {
-               "A",
-               "B",
-               "C",
-               NULL,
-       };
-
-       // Join with newline
-       s = pakfire_string_join(elements1, "\n");
-       ASSERT_STRING_EQUALS(s, "A\nB\nC");
-
-       if (s) {
-               free(s);
-               s = NULL;
-       }
-
-       // Join with empty delimiter
-       s = pakfire_string_join(elements1, "");
-       ASSERT_STRING_EQUALS(s, "ABC");
-
-       if (s) {
-               free(s);
-               s = NULL;
-       }
-
-       char* elements2[] = {
-               "",
-               "",
-               "",
-               NULL,
-       };
-
-       // Join list with empty elements
-       s = pakfire_string_join(elements2, "X");
-       ASSERT_STRING_EQUALS(s, "XX");
-
-       if (s) {
-               free(s);
-               s = NULL;
-       }
-
-       // Invalid inputs
-       s = pakfire_string_join(NULL, NULL);
-       ASSERT_ERRNO(!s, EINVAL);
-
-       s = pakfire_string_join(elements1, NULL);
-       ASSERT_ERRNO(!s, EINVAL);
-
-       s = pakfire_string_join(NULL, "\n");
-       ASSERT_ERRNO(!s, EINVAL);
-
-       char* elements3[] = {
-               NULL,
-       };
-
-       // Join empty elements
-       ASSERT_NULL(pakfire_string_join(elements3, "\n"));
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       if (s)
-               free(s);
-
-       return EXIT_FAILURE;
-}
-
-static int test_format_size(const struct test* t) {
-       char buffer[128];
-       char small_buffer[2];
-       int r;
-
-       ASSERT(pakfire_format_size(buffer, 0) == 2);
-       ASSERT_STRING_EQUALS(buffer, "0 ");
-
-       ASSERT(pakfire_format_size(buffer, 1024) == 2);
-       ASSERT_STRING_EQUALS(buffer, "1k");
-
-       ASSERT(pakfire_format_size(buffer, 1024 * 1024) == 4);
-       ASSERT_STRING_EQUALS(buffer, "1.0M");
-
-       ASSERT(pakfire_format_size(small_buffer, 0) == 2);
-       ASSERT_STRING_EQUALS(small_buffer, "");
-
-       return EXIT_SUCCESS;
-
-FAIL:
-       return EXIT_FAILURE;
-}
-
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_basename);
        testsuite_add_test(test_dirname);
-       testsuite_add_test(test_string_startswith);
-       testsuite_add_test(test_string_endswith);
-       testsuite_add_test(test_string_matches);
-       testsuite_add_test(test_string_partition);
-       testsuite_add_test(test_string_replace);
-       testsuite_add_test(test_string_split);
-       testsuite_add_test(test_string_join);
-       testsuite_add_test(test_format_size);
 
        return testsuite_run(argc, argv);
 }