return ptr - start;
}
+bool t_split_key_value(const char *arg, char separator,
+ const char **key_r, const char **value_r)
+{
+ *value_r = arg == NULL ? NULL : strchr(arg, separator);
+ if (*value_r != NULL) {
+ *key_r = t_strdup_until(arg, (*value_r)++);
+ return TRUE;
+ }
+ *value_r = "";
+ *key_r = arg;
+ return FALSE;
+}
+
static char **
split_str_slow(pool_t pool, const char *data, const char *separators, bool spaces)
{
return tmp == NULL ? NULL : tmp+1;
}
+/* Split only on the first separator encountered.
+ Returns TRUE if the separator was found.
+ Returns FALSE and *value_r = "" otherwise. */
+bool t_split_key_value(const char *arg, char separator,
+ const char **key_r, const char **value_r);
+static inline bool
+t_split_key_value_eq(const char *arg, const char **key_r, const char **value_r) {
+ return t_split_key_value(arg, '=', key_r, value_r);
+}
+
/* separators is an array of separator characters, not a separator string.
an empty data string results in an array containing only NULL. */
char **p_strsplit(pool_t pool, const char *data, const char *separators)
test_end();
}
+static void test_t_split_key_value(void)
+{
+ const char *key, *value;
+
+ struct {
+ const char *input;
+ bool found;
+ const char *key;
+ const char *value;
+ } tests[] = {
+ { NULL, FALSE, NULL, "" },
+ { "", FALSE, "", "" },
+ { "=", TRUE, "", ""},
+ { "====", TRUE, "", "==="},
+
+ { "key", FALSE, "key", "" },
+ { "key=", TRUE, "key", "" },
+ { "key=value", TRUE, "key", "value" },
+ { "key=value=", TRUE, "key", "value=" },
+ { "key=v=w=x", TRUE, "key", "v=w=x" },
+ { "key=v=w=x=", TRUE, "key", "v=w=x=" },
+
+ { "=value", TRUE, "", "value" },
+ { "=v=w=x", TRUE, "", "v=w=x" },
+ { "=v=w=x=", TRUE, "", "v=w=x=" },
+ { "==v=w=x=", TRUE, "", "=v=w=x=" },
+ };
+
+ test_begin("t_split_key_value");
+ {
+ test_assert(t_split_key_value("k:v", ':', &key, &value));
+ test_assert_strcmp("k", key);
+ test_assert_strcmp("v", value);
+ }
+ for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
+ bool found = t_split_key_value_eq(tests[i].input, &key, &value);
+ test_assert_idx(tests[i].found == found, i);
+ test_assert_idx(null_strcmp(tests[i].key, key) == 0, i);
+ test_assert_idx(null_strcmp(tests[i].value, value) == 0, i);
+ }
+ test_end();
+}
+
static void test_t_strsplit(void)
{
struct {
test_p_strdup_empty();
test_p_strdup_until();
test_p_strarray_dup();
+ test_t_split_key_value();
test_t_strsplit();
test_t_strsplit_spaces();
test_t_str_replace();