From 0b77bdeaf9d28c85d8c558bfe7bcd14117a3f943 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C5=99emysl=20Janouch?=
Date: Sun, 9 Oct 2016 09:45:27 +0200
Subject: [PATCH] Merge cstr_split() and cstr_split_ignore_empty()
---
liberty-proto.c | 2 +-
liberty.c | 27 +++++++--------------------
2 files changed, 8 insertions(+), 21 deletions(-)
diff --git a/liberty-proto.c b/liberty-proto.c
index 5e84b21..9ecd656 100644
--- a/liberty-proto.c
+++ b/liberty-proto.c
@@ -66,7 +66,7 @@ irc_parse_message_tags (const char *tags, struct str_map *out)
{
struct str_vector v;
str_vector_init (&v);
- cstr_split_ignore_empty (tags, ';', &v);
+ cstr_split (tags, ";", true, &v);
for (size_t i = 0; i < v.len; i++)
{
diff --git a/liberty.c b/liberty.c
index 4867fdb..997fae7 100644
--- a/liberty.c
+++ b/liberty.c
@@ -2899,30 +2899,17 @@ base64_encode (const void *data, size_t len, struct str *output)
// --- Utilities ---------------------------------------------------------------
static void
-cstr_split (const char *s, const char *delimiters, struct str_vector *out)
+cstr_split (const char *s, const char *delimiters, bool ignore_empty,
+ struct str_vector *out)
{
const char *begin = s, *end;
while ((end = strpbrk (begin, delimiters)))
{
- str_vector_add_owned (out, xstrndup (begin, end - begin));
- begin = ++end;
- }
- str_vector_add (out, begin);
-}
-
-static void
-cstr_split_ignore_empty (const char *s, char delimiter, struct str_vector *out)
-{
- const char *begin = s, *end;
-
- while ((end = strchr (begin, delimiter)))
- {
- if (begin != end)
+ if (!ignore_empty || begin != end)
str_vector_add_owned (out, xstrndup (begin, end - begin));
begin = ++end;
}
-
- if (*begin)
+ if (!ignore_empty || *begin)
str_vector_add (out, begin);
}
@@ -3219,7 +3206,7 @@ get_xdg_config_dirs (struct str_vector *out)
const char *xdg_config_dirs;
if (!(xdg_config_dirs = getenv ("XDG_CONFIG_DIRS")))
xdg_config_dirs = "/etc/xdg";
- cstr_split_ignore_empty (xdg_config_dirs, ':', out);
+ cstr_split (xdg_config_dirs, ":", true, out);
}
static char *
@@ -3246,7 +3233,7 @@ get_xdg_data_dirs (struct str_vector *out)
const char *xdg_data_dirs;
if (!(xdg_data_dirs = getenv ("XDG_DATA_DIRS")))
xdg_data_dirs = "/usr/local/share/:/usr/share/";
- cstr_split_ignore_empty (xdg_data_dirs, ':', out);
+ cstr_split (xdg_data_dirs, ":", true, out);
}
static char *
@@ -4635,7 +4622,7 @@ config_item_get (struct config_item *self, const char *path, struct error **e)
struct str_vector v;
str_vector_init (&v);
- cstr_split (path, ".", &v);
+ cstr_split (path, ".", false, &v);
struct config_item *result = NULL;
size_t i = 0;