From: Michael Tremer Date: Fri, 26 Feb 2021 11:13:26 +0000 (+0000) Subject: util: Refactor pakfire_string_partition X-Git-Tag: 0.9.28~1285^2~685 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5837952f295f0b64f7c7e90cc8f70bdc1e105591;p=pakfire.git util: Refactor pakfire_string_partition Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 4022190e8..5e107b03d 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -29,6 +29,7 @@ #include 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 diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 60df1b742..fbfe71b27 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -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; diff --git a/src/libpakfire/parser/grammar.y b/src/libpakfire/parser/grammar.y index 1e4c75cab..45d76b7d2 100644 --- a/src/libpakfire/parser/grammar.y +++ b/src/libpakfire/parser/grammar.y @@ -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); diff --git a/src/libpakfire/relation.c b/src/libpakfire/relation.c index 382080bcc..2228c8dd2 100644 --- a/src/libpakfire/relation.c +++ b/src/libpakfire/relation.c @@ -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) { diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index e4eac58fb..b894de5e1 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include #include #include #include @@ -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)); -} diff --git a/tests/libpakfire/util.c b/tests/libpakfire/util.c index b8ee61d41..942641c1b 100644 --- a/tests/libpakfire/util.c +++ b/tests/libpakfire/util.c @@ -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(); }