Re: g_strjoinv() is very slow



On Tue, Sep 26, 2000 at 07:50:42AM +0200, Tim Janik wrote:
>
> ok, one extra round, mostly for stylistic issues,
> and i think we'll be done with it ;)
>
> [ ... ]
>
> the rest of the patch seems pretty ok to me, just align stuff,
> add missing spaces, glib-ify the above strpcpy version, and
> supply a ChangeLog entry.
>
  Ok. Here it is. I hope everything is right now.

      Regards,

            DindinX

--
David Odin bigfoot com
diff -Nrudb glib.orig/ChangeLog glib/ChangeLog
--- glib.orig/ChangeLog	Mon Sep 25 23:28:13 2000
+++ glib/ChangeLog	Tue Sep 26 21:54:07 2000
@@ -1,3 +1,12 @@
+Tue Sep 26 21:43:44 2000  DindinX <David Odin bigfoot com>
+
+	* gstrfuncs.c, glib.h: Add g_stpcpy () for platform that don't
+          have stpcpy ().
+	* gstrfuncs.c (g_strjoin, g_strjoinv and g_strconcat): use
+	  g_stpcpy () so these functions run much faster.
+	* config.h.win32.in, glib.def: add reference to g_stpcpy ()
+	* configure.in: Add a test for the stpcpy () function.
+
 Mon Sep 25 2000 Elliot Lee <sopwith redhat com>
 	* garray.c, glib.h, gmem.c: Add a few missing G_GNUC_CONST's.
 
diff -Nrudb glib.orig/config.h.win32.in glib/config.h.win32.in
--- glib.orig/config.h.win32.in	Thu Sep 21 18:17:31 2000
+++ glib/config.h.win32.in	Tue Sep 26 21:31:12 2000
@@ -102,6 +102,9 @@
 /* Define if you have the on_exit function.  */
 /* #undef HAVE_ON_EXIT */
 
+/* Define if you have the stpcpy function. */
+/* #undef HAVE_STPCPY */
+
 /* Define if you have the strcasecmp function.  */
 /* #undef HAVE_STRCASECMP */
 
diff -Nrudb glib.orig/configure.in glib/configure.in
--- glib.orig/configure.in	Thu Sep 21 14:49:10 2000
+++ glib/configure.in	Tue Sep 26 21:31:12 2000
@@ -357,7 +357,7 @@
 GLIB_SIZEOF([$size_includes], intmax_t, intmax_t)
 
 # Check for some functions
-AC_CHECK_FUNCS(lstat strerror strsignal memmove vsnprintf strcasecmp strncasecmp poll getcwd)
+AC_CHECK_FUNCS(lstat strerror strsignal memmove vsnprintf stpcpy strcasecmp strncasecmp poll getcwd)
 
 # Check if bcopy can be used for overlapping copies, if memmove isn't found.
 # The check is borrowed from the PERL Configure script.
diff -Nrudb glib.orig/glib.def glib/glib.def
--- glib.orig/glib.def	Thu Sep 21 18:17:31 2000
+++ glib/glib.def	Tue Sep 26 21:31:12 2000
@@ -391,6 +391,7 @@
 	g_static_rw_lock_writer_lock
 	g_static_rw_lock_writer_trylock
 	g_static_rw_lock_writer_unlock
+	g_stpcpy
 	g_str_equal
 	g_str_hash
 	g_strcanon
diff -Nrudb glib.orig/glib.h glib/glib.h
--- glib.orig/glib.h	Mon Sep 25 23:28:13 2000
+++ glib/glib.h	Tue Sep 26 21:35:04 2000
@@ -1761,8 +1761,8 @@
 gchar*   g_strjoinv		(const gchar  *separator,
 				 gchar       **str_array);
 void     g_strfreev		(gchar       **str_array);
-
-
+gchar*   g_stpcpy		(gchar        *dest,
+				 const char   *src);
 
 /* calculate a string size, guarranteed to fit format + args.
  */
diff -Nrudb glib.orig/gstrfuncs.c glib/gstrfuncs.c
--- glib.orig/gstrfuncs.c	Sun Sep 10 18:04:33 2000
+++ glib/gstrfuncs.c	Tue Sep 26 21:42:21 2000
@@ -118,6 +118,28 @@
   return str;
 }
 
+gchar *
+g_stpcpy (gchar       *dest,
+	  const gchar *src)
+{
+#ifdef HAVE_STPCPY
+  g_return_val_if_fail (dest != NULL, NULL);
+  g_return_val_if_fail (src != NULL, NULL);
+  return stpcpy (dest, src);
+#else
+  register gchar *d = dest;
+  register const gchar *s = src;
+
+  g_return_val_if_fail (dest != NULL, NULL);
+  g_return_val_if_fail (src != NULL, NULL);
+  do
+    *d++ = *s;
+  while (*s++ != '\0');
+
+  return d - 1;   
+#endif
+}
+
 gchar*
 g_strdup_vprintf (const gchar *format,
 		  va_list      args1)
@@ -156,6 +178,7 @@
   va_list args;
   gchar	  *s;
   gchar	  *concat;
+  gchar   *ptr;
 
   g_return_val_if_fail (string1 != NULL, NULL);
 
@@ -170,14 +193,14 @@
   va_end (args);
 
   concat = g_new (gchar, l);
-  concat[0] = 0;
+  ptr = concat;
 
-  strcat (concat, string1);
+  ptr = g_stpcpy (ptr, string1);
   va_start (args, string1);
   s = va_arg (args, gchar*);
   while (s)
     {
-      strcat (concat, s);
+      ptr = g_stpcpy (ptr, s);
       s = va_arg (args, gchar*);
     }
   va_end (args);
@@ -1554,6 +1577,7 @@
 	    gchar       **str_array)
 {
   gchar *string;
+  gchar *ptr;
 
   g_return_val_if_fail (str_array != NULL, NULL);
 
@@ -1566,17 +1590,19 @@
       guint separator_len;
 
       separator_len = strlen (separator);
+      /* First part, getting length */
       len = 1 + strlen (str_array[0]);
-      for(i = 1; str_array[i] != NULL; i++)
-	len += separator_len + strlen(str_array[i]);
+      for (i = 1; str_array[i] != NULL; i++)
+        len += strlen (str_array[i]);
+      len += separator_len * (i - 1);
 
+      /* Second part, building string */
       string = g_new (gchar, len);
-      *string = 0;
-      strcat (string, *str_array);
+      ptr = g_stpcpy (string, *str_array);
       for (i = 1; str_array[i] != NULL; i++)
 	{
-	  strcat (string, separator);
-	  strcat (string, str_array[i]);
+          ptr = g_stpcpy (ptr, separator);
+          ptr = g_stpcpy (ptr, str_array[i]);
 	}
       }
   else
@@ -1593,6 +1619,7 @@
   va_list args;
   guint len;
   guint separator_len;
+  gchar *ptr;
 
   if (separator == NULL)
     separator = "";
@@ -1605,7 +1632,8 @@
 
   if (s)
     {
-      len = strlen (s);
+      /* First part, getting length */
+      len = 1 + strlen (s);
 
       s = va_arg (args, gchar*);
       while (s)
@@ -1615,19 +1643,19 @@
 	}
       va_end (args);
 
-      string = g_new (gchar, len + 1);
-      *string = 0;
+      /* Second part, building string */
+      string = g_new (gchar, len);
 
       va_start (args, separator);
 
       s = va_arg (args, gchar*);
-      strcat (string, s);
+      ptr = g_stpcpy (string, s);
 
       s = va_arg (args, gchar*);
       while (s)
 	{
-	  strcat (string, separator);
-	  strcat (string, s);
+          ptr = g_stpcpy (ptr, separator);
+          ptr = g_stpcpy (ptr, s);
 	  s = va_arg (args, gchar*);
 	}
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]