From f907f1e3dcf7d6922cee4433297c3b4249d899e6 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Thu, 9 Jul 2015 00:10:06 +0200
Subject: Add str_cut_until()
---
common.c | 6 ++++++
degesch.c | 10 ++++------
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/common.c b/common.c
index 94d5aa4..12e1eff 100644
--- a/common.c
+++ b/common.c
@@ -65,6 +65,12 @@ transform_str (char *s, int (*tolower) (int c))
*s = tolower (*s);
}
+static char *
+str_cut_until (const char *s, const char *alphabet)
+{
+ return xstrndup (s, strcspn (s, alphabet));
+}
+
static void
split_str (const char *s, char delimiter, struct str_vector *out)
{
diff --git a/degesch.c b/degesch.c
index 6294316..6e3d6a6 100644
--- a/degesch.c
+++ b/degesch.c
@@ -2139,7 +2139,7 @@ irc_server_strncmp (struct server *s, const char *a, const char *b, size_t n)
static char *
irc_cut_nickname (const char *prefix)
{
- return xstrndup (prefix, strcspn (prefix, "!@"));
+ return str_cut_until (prefix, "!@");
}
static const char *
@@ -6251,8 +6251,7 @@ dump_matching_options
for (size_t i = 0; i < output->len; i++)
{
// Yeah, I know
- const char *line = output->vector[i];
- char *key = xstrndup (line, strcspn (line, " "));
+ char *key = str_cut_until (output->vector[i], " ");
if (fnmatch (mask, key, 0))
str_vector_remove (output, i--);
free (key);
@@ -6702,7 +6701,7 @@ handle_command_set_assign
}
for (size_t i = 0; i < all->len; i++)
{
- char *key = xstrndup (all->vector[i], strcspn (all->vector[i], " "));
+ char *key = str_cut_until (all->vector[i], " ");
handle_command_set_assign_item (ctx, key, new_, add, remove);
free (key);
}
@@ -7837,8 +7836,7 @@ complete_option (struct app_context *ctx, struct completion *data,
char *mask = xstrdup_printf ("%s*", word);
for (size_t i = 0; i < options.len; i++)
{
- const char *line = options.vector[i];
- char *key = xstrndup (line, strcspn (line, " "));
+ char *key = str_cut_until (options.vector[i], " ");
if (!fnmatch (mask, key, 0))
str_vector_add_owned (output, key);
else
--
cgit v1.2.3-70-g09d2