#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);
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
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;
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);
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) {
# #
#############################################################################*/
+#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <math.h>
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++;
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));
-}
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();
}