repo: rxvt-unicode-sixel
action: commit
revision: 
path_from: 
revision_from: 7320485f408f71f999ca7ec9e79223a968f01822:
path_to: 
revision_to: 
git.thebackupbox.net
rxvt-unicode-sixel
git clone git://git.thebackupbox.net/rxvt-unicode-sixel
commit 7320485f408f71f999ca7ec9e79223a968f01822
Author: Emanuele Giaquinta 
Date:   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
index 18e3a16da3d69173cf0ae3d24e4b9bae4bb2f289..
index ..c1f5598070e731dd2d261cd58bf6dbd3a4ed9c45 100644
--- 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
index 784d097d892a510f0ca95c51b29c78555aaee201..
index ..9327d69f40f577301814fe71307d03795b8a26ef 100644
--- 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
index 4d6380349606ccf59fcfef71879a33df3bee6052..
index ..6179424ce06d987af5d78eac5543cc53173fdf4f 100644
--- 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-----