]> git.ipfire.org Git - pakfire.git/commitdiff
util: Refactor pakfire_string_partition
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Feb 2021 11:13:26 +0000 (11:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Feb 2021 11:15:04 +0000 (11:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/libpakfire.sym
src/libpakfire/parser/grammar.y
src/libpakfire/relation.c
src/libpakfire/util.c
tests/libpakfire/util.c

index 4022190e8fec46883cf8e73ba7783087d871a46b..5e107b03dd0b94202f4cca83ae44169aa75dbd42 100644 (file)
@@ -29,6 +29,7 @@
 #include <pakfire/types.h>
 
 int pakfire_string_startswith(const char* s, const char* prefix);
+int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2);
 
 char* pakfire_format_size(double size);
 char* pakfire_format_date(time_t t);
@@ -51,7 +52,6 @@ int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len);
 
 size_t pakfire_string_to_size(const char* s);
 char** pakfire_split_string(const char* s, char delim);
-void pakfire_partition_string(const char* s, const char* delim, char** s1, char** s2);
 
 #ifdef PAKFIRE_PRIVATE
 
index 60df1b74236d7e3d97b49a941609d36b508a35a6..fbfe71b27cc5e01712427a1157dd87532cbcc262 100644 (file)
@@ -395,12 +395,12 @@ global:
        pakfire_basename;
        pakfire_dirname;
        pakfire_get_errno;
-       pakfire_partition_string;
        pakfire_path_isdir;
        pakfire_path_join;
        pakfire_path_relpath;
        pakfire_read_file_into_buffer;
        pakfire_split_string;
+       pakfire_string_partition;
        pakfire_string_startswith;
        pakfire_string_to_size;
 
index 1e4c75cab085441bb16a441b709680b6ddf426bc..45d76b7d28ac921da4a6b9a3c881e0a3d5f13976 100644 (file)
@@ -260,10 +260,11 @@ subparser                                 : subparser_name T_EOL subgrammar T_END T_EOL
                                                                if (!$$)
                                                                        ABORT;
 
-                                                               pakfire_partition_string($1, ":", &key, &value);
+                                                               int r = pakfire_string_partition($1, ":", &key, &value);
+                                                               if (r)
+                                                                       ABORT;
 
-                                                               if (key && value)
-                                                                       pakfire_parser_set($$, key, value);
+                                                               pakfire_parser_set($$, key, value);
 
                                                                if (key)
                                                                        free(key);
index 382080bcc85bd55858aa49a196294417c215c773..2228c8dd25f4f6746f92ac0dbebda1fbc69e67c3 100644 (file)
@@ -115,7 +115,7 @@ PAKFIRE_EXPORT PakfireRelation pakfire_relation_create_from_string(Pakfire pakfi
 
        const char** delim = delimiters;
        while (*delim) {
-               pakfire_partition_string(s, *delim, &name, &evr);
+               pakfire_string_partition(s, *delim, &name, &evr);
 
                // Nothing to do for no match
                if (!name && !evr) {
index e4eac58fb7782f5e6fc16381897290112db3e3c0..b894de5e10af30be0e16f5bf53d4d0f65d2b28a3 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <ctype.h>
 #include <errno.h>
 #include <libgen.h>
 #include <math.h>
@@ -43,6 +44,34 @@ int pakfire_string_endswith(const char* s, const char* suffix) {
        return !strcmp(s + strlen(s) - strlen(suffix), suffix);
 }
 
+PAKFIRE_EXPORT 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_lstrip(const char* s) {
        while (*s && isspace(*s))
                s++;
@@ -335,22 +364,3 @@ PAKFIRE_EXPORT char** pakfire_split_string(const char* s, char delim) {
 
        return ret;
 }
-
-PAKFIRE_EXPORT void pakfire_partition_string(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;
-       }
-
-       // Length of string before delim
-       size_t l = p - s;
-
-       *s1 = malloc(l);
-       snprintf(*s1, l, "%s", s);
-
-       *s2 = strdup(p + strlen(delim));
-}
index b8ee61d41a4dbee92969eaf651329b4cef073cdf..942641c1b0719b6b437dd0eb859893632aeca065 100644 (file)
@@ -58,10 +58,60 @@ static int test_string_startswith(const struct test* t) {
        return EXIT_SUCCESS;
 }
 
+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;
+}
+
 int main(int argc, char** argv) {
        testsuite_add_test(test_basename);
        testsuite_add_test(test_dirname);
        testsuite_add_test(test_string_startswith);
+       testsuite_add_test(test_string_partition);
 
        return testsuite_run();
 }