repo: rxvt-unicode-sixel action: commit revision: path_from: revision_from: 7320485f408f71f999ca7ec9e79223a968f01822: path_to: revision_to:
commit 7320485f408f71f999ca7ec9e79223a968f01822 Author: Emanuele GiaquintaDate: Mon Jan 10 14:07:19 2011 +0000 Generalize rxvt_splitcommastring to accept any char as delimiter and optimize it to use a single memory area for the strings. diff --git a/src/main.C b/src/main.C
--- a/src/main.C
+++ b/src/main.C
@@ -1355,7 +1355,7 @@ rxvt_term::IM_get_IC (const char *modifiers)
if (!p)
continue;
- s = rxvt_splitcommastring (p);
+ s = rxvt_strsplit (',', p);
for (i = found = 0; !found && s[i]; i++)
{
@@ -1377,7 +1377,7 @@ rxvt_term::IM_get_IC (const char *modifiers)
for (j = 0; j < xim_styles->count_styles; j++)
if (input_style == xim_styles->supported_styles[j])
{
- rxvt_freecommastring (s);
+ rxvt_free_strsplit (s);
found = 1;
goto foundpet;
@@ -1385,7 +1385,7 @@ rxvt_term::IM_get_IC (const char *modifiers)
}
- rxvt_freecommastring (s);
+ rxvt_free_strsplit (s);
}
foundpet:
@@ -1551,7 +1551,7 @@ rxvt_term::im_cb ()
{
bool found = false;
- s = rxvt_splitcommastring (p);
+ s = rxvt_strsplit (',', p);
for (i = 0; s[i]; i++)
{
@@ -1567,7 +1567,7 @@ rxvt_term::im_cb ()
}
}
- rxvt_freecommastring (s);
+ rxvt_free_strsplit (s);
if (found)
goto done;
diff --git a/src/misc.C b/src/misc.C
--- a/src/misc.C
+++ b/src/misc.C
@@ -265,21 +265,20 @@ rxvt_strtrim (char *str) NOTHROW
}
/*
- * Split a comma-separated string into an array, stripping leading and
+ * Split a string into an array based on the given delimiter, stripping leading and
* trailing spaces from each entry. Empty strings are properly returned
*/
char **
-rxvt_splitcommastring (const char *cs) NOTHROW
+rxvt_strsplit (char delim, const char *str) NOTHROW
{
- int l, n, p;
- const char *s, *t;
- char **ret;
+ int l, n;
+ char *s, *t;
+ char **ret;
- if ((s = cs) == NULL)
- s = "";
+ s = strdup (str ? str : "");
for (n = 1, t = s; *t; t++)
- if (*t == ',')
+ if (*t == delim)
n++;
ret = (char **)malloc ((n + 1) * sizeof (char *));
@@ -287,11 +286,10 @@ rxvt_splitcommastring (const char *cs) NOTHROW
for (l = 0, t = s; l < n; l++)
{
- for ( ; *t && *t != ','; t++) ;
- p = t - s;
- ret[l] = (char *)malloc (p + 1);
- memcpy (ret[l], s, p);
- ret[l][p] = '\0';
+ for (; *t && *t != delim; t++)
+ ;
+ *t = '\0';
+ ret[l] = s;
rxvt_strtrim (ret[l]);
s = ++t;
}
@@ -299,15 +297,6 @@ rxvt_splitcommastring (const char *cs) NOTHROW
return ret;
}
-void
-rxvt_freecommastring (char **cs) NOTHROW
-{
- for (int i = 0; cs[i]; ++i)
- free (cs[i]);
-
- free (cs);
-}
-
void *
rxvt_malloc (size_t size)
{
diff --git a/src/rxvt.h b/src/rxvt.h
--- a/src/rxvt.h
+++ b/src/rxvt.h
@@ -155,8 +155,14 @@ void rxvt_fatal (const char *fmt, ...) THROW (
void rxvt_exit_failure () THROW ((class rxvt_failure_exception)) NORETURN;
char * rxvt_strtrim (char *str) NOTHROW;
-char ** rxvt_splitcommastring (const char *cs) NOTHROW;
-void rxvt_freecommastring (char **cs) NOTHROW;
+char ** rxvt_strsplit (char delim, const char *str) NOTHROW;
+
+static inline void
+rxvt_free_strsplit (char **ptr) NOTHROW
+{
+ free (ptr[0]);
+ free (ptr);
+}
void * rxvt_malloc (size_t size);
void * rxvt_calloc (size_t number, size_t size);
-----END OF PAGE-----