added g_strndup_a macro
authorJeff Garzik <jgarzik@src.gnome.org>
Sat, 2 Jan 1999 01:51:08 +0000 (01:51 +0000)
committerJeff Garzik <jgarzik@src.gnome.org>
Sat, 2 Jan 1999 01:51:08 +0000 (01:51 +0000)
        * glib.h: added g_strndup_a macro

        * testglib.c:
        Added tests for new alloca-based string routines.
        Reformatted a couple strings.

12 files changed:
ChangeLog
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib.h
glib/glib.h
testglib.c
tests/testglib.c

index 8e4b69d..bb033e9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
index 8e4b69d..bb033e9 100644 (file)
@@ -1,3 +1,11 @@
+Fri Jan  1 20:43:19 EST 1999  Jeff Garzik  <jgarzik@pobox.com>
+
+       * glib.h: added g_strndup_a macro
+
+       * testglib.c:
+       Added tests for new alloca-based string routines.
+       Reformatted a couple strings.
+
 Sat Jan  2 02:20:59 1999  Tim Janik  <timj@gtk.org>
 
        * ghook.c:
diff --git a/glib.h b/glib.h
index 3e91c9b..a818844 100644 (file)
--- a/glib.h
+++ b/glib.h
@@ -1469,6 +1469,17 @@ gpointer g_memdup                (gconstpointer mem,
          (newstr) = __new;                     \
    } G_STMT_END
 
+#  define g_strndup_a(newstr,str,n) G_STMT_START { \
+         const char *__old = (str);            \
+         char *__new;                          \
+         size_t __len = strlen (__old);        \
+         if (__len > (n)) __len = (n);         \
+         __new = alloca (__len + 1);           \
+         memcpy (__new, __old, __len);         \
+         __new[__len] = 0;                     \
+         (newstr) = __new;                     \
+   } G_STMT_END
+
 #  define g_strconcat_a(newstr,str1,str2,str3) G_STMT_START { \
          size_t __len1 = ((str1) == (gchar*)NULL) ? 0 : strlen((str1)); \
          size_t __len2 = ((str2) == (gchar*)NULL) ? 0 : strlen((str2)); \
index 3e91c9b..a818844 100644 (file)
@@ -1469,6 +1469,17 @@ gpointer g_memdup                (gconstpointer mem,
          (newstr) = __new;                     \
    } G_STMT_END
 
+#  define g_strndup_a(newstr,str,n) G_STMT_START { \
+         const char *__old = (str);            \
+         char *__new;                          \
+         size_t __len = strlen (__old);        \
+         if (__len > (n)) __len = (n);         \
+         __new = alloca (__len + 1);           \
+         memcpy (__new, __old, __len);         \
+         __new[__len] = 0;                     \
+         (newstr) = __new;                     \
+   } G_STMT_END
+
 #  define g_strconcat_a(newstr,str1,str2,str3) G_STMT_START { \
          size_t __len1 = ((str1) == (gchar*)NULL) ? 0 : strlen((str1)); \
          size_t __len2 = ((str2) == (gchar*)NULL) ? 0 : strlen((str2)); \
index e443a85..3eb2513 100644 (file)
@@ -39,6 +39,9 @@ else \
 #define        C2P(c)          ((gpointer) ((long) (c)))
 #define        P2C(p)          ((gchar) ((long) (p)))
 
+#define GLIB_TEST_STRING "el dorado "
+#define GLIB_TEST_STRING_5 "el do"
+
 static gboolean
 node_build_string (GNode    *node,
                   gpointer  data)
@@ -859,16 +862,35 @@ main (int   argc,
 
   g_print ("endian macro tests...");
 #if G_BYTE_ORDER == G_BIG_ENDIAN
-  g_print ("big endian...");
+  g_print ("big endian...\n");
 #else
-  g_print ("little endian...");
+  g_print ("little endian...\n");
 #endif
   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
 #ifdef G_HAVE_GINT64
   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
 #endif
+
+#ifdef G_HAVE_ALLOCA
+  /* test alloca()-based string duplication routines */
+  g_strdup_a(string, GLIB_TEST_STRING);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
+
+  g_strndup_a(string, GLIB_TEST_STRING, 5);
+  g_assert(string != NULL);
+  g_assert(strlen(string) == 5);
+  g_assert(strcmp(string, GLIB_TEST_STRING_5) == 0);
+
+  g_strconcat_a(string, GLIB_TEST_STRING, GLIB_TEST_STRING, GLIB_TEST_STRING);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
+                         GLIB_TEST_STRING) == 0);
+#endif
+
   g_print ("ok\n");
 
   return 0;
 }
+
index e443a85..3eb2513 100644 (file)
@@ -39,6 +39,9 @@ else \
 #define        C2P(c)          ((gpointer) ((long) (c)))
 #define        P2C(p)          ((gchar) ((long) (p)))
 
+#define GLIB_TEST_STRING "el dorado "
+#define GLIB_TEST_STRING_5 "el do"
+
 static gboolean
 node_build_string (GNode    *node,
                   gpointer  data)
@@ -859,16 +862,35 @@ main (int   argc,
 
   g_print ("endian macro tests...");
 #if G_BYTE_ORDER == G_BIG_ENDIAN
-  g_print ("big endian...");
+  g_print ("big endian...\n");
 #else
-  g_print ("little endian...");
+  g_print ("little endian...\n");
 #endif
   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
 #ifdef G_HAVE_GINT64
   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
 #endif
+
+#ifdef G_HAVE_ALLOCA
+  /* test alloca()-based string duplication routines */
+  g_strdup_a(string, GLIB_TEST_STRING);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
+
+  g_strndup_a(string, GLIB_TEST_STRING, 5);
+  g_assert(string != NULL);
+  g_assert(strlen(string) == 5);
+  g_assert(strcmp(string, GLIB_TEST_STRING_5) == 0);
+
+  g_strconcat_a(string, GLIB_TEST_STRING, GLIB_TEST_STRING, GLIB_TEST_STRING);
+  g_assert(string != NULL);
+  g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
+                         GLIB_TEST_STRING) == 0);
+#endif
+
   g_print ("ok\n");
 
   return 0;
 }
+