This fixes #60543:
authorMatthias Clasen <matthiasc@src.gnome.org>
Wed, 12 Dec 2001 22:05:55 +0000 (22:05 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Wed, 12 Dec 2001 22:05:55 +0000 (22:05 +0000)
        * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().

        * tests/strfunc-test.c: Add some tests for g_snprintf().

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
docs/reference/ChangeLog
docs/reference/glib/tmpl/string_utils.sgml
glib/gutils.c
tests/strfunc-test.c

index ddf8a47..9fb967d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index ddf8a47..9fb967d 100644 (file)
@@ -1,5 +1,13 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       This fixes #60543:
+
+       * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf().
+
+       * tests/strfunc-test.c: Add some tests for g_snprintf().
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/gconvert.c, glib/grand.c, glib/ghash.c,
        glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
 
index cc38554..191959f 100644 (file)
@@ -1,5 +1,9 @@
 2001-12-12  Matthias Clasen  <matthiasc@poet.de>
 
+       * glib/tmpl/string_utils.sgml: Correct docs for g_[v]snprintf.
+
+2001-12-12  Matthias Clasen  <matthiasc@poet.de>
+
        * glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml, 
        glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
        glib/tmpl/caches.sgml, glib/tmpl/completion.sgml, 
index 1955df8..16ebaac 100644 (file)
@@ -173,17 +173,28 @@ See also g_strdup_printf().
 <note>
 <para>
 In versions of GLib prior to 1.2.3, this function may return -1 if the output
-was truncated, and the truncated string may not be nul-terminated.
+was truncated, and the truncated string may not be nul-terminated. 
+In versions prior to 1.3.12, this function returns the length of the output 
+string.
+</para>
+</note>
+<note>
+<para>
+The return value of g_snprintf() conforms to the <function>snprintf()</function>
+function as standardized in ISO C99. Note that this is different from 
+traditional <function>snprintf()</function>, which returns the length of 
+the output string.
 </para>
 </note>
 
 @string: the buffer to hold the output.
-@n: the maximum number of characters to produce (including the terminating null
+@n: the maximum number of characters to produce (including the terminating nul
 character).
-@format: the format string. See the <function>sprintf()</function>
+@format: the format string. See the <function>sprintf()</function>.
 documentation.
 @Varargs: the arguments to insert in the output.
-@Returns: the length of the output string.
+@Returns: the number of characters which would be produced if the buffer was
+large enough.
 
 
 <!-- ##### FUNCTION g_vsnprintf ##### -->
@@ -200,16 +211,27 @@ See also g_strdup_vprintf().
 <para>
 In versions of GLib prior to 1.2.3, this function may return -1 if the output
 was truncated, and the truncated string may not be nul-terminated.
+In versions prior to 1.3.12, this function returns the length of the output 
+string.
+</para>
+</note>
+<note>
+<para>
+The return value of g_vsnprintf() conforms to the <function>vsnprintf()</function>
+function as standardized in ISO C99. Note that this is different from 
+traditional <function>vsnprintf()</function>, which returns the length of 
+the output string.
 </para>
 </note>
 
 @string: the buffer to hold the output.
-@n: the maximum number of characters to produce (including the terminating null
+@n: the maximum number of characters to produce (including the terminating nul
 character).
 @format: the format string. See the <function>sprintf()</function>
 documentation.
 @args: the list of arguments to insert in the output.
-@Returns: the length of the output string.
+@Returns: the number of characters which would be produced if the buffer was
+large enough.
 
 
 <!-- ##### FUNCTION g_printf_string_upper_bound ##### -->
index 6727181..66891a1 100644 (file)
@@ -341,44 +341,14 @@ g_snprintf (gchar *str,
            gchar const *fmt,
            ...)
 {
-#ifdef HAVE_VSNPRINTF
   va_list args;
   gint retval;
-  
-  g_return_val_if_fail (str != NULL, 0);
-  g_return_val_if_fail (n > 0, 0);
-  g_return_val_if_fail (fmt != NULL, 0);
-
-  va_start (args, fmt);
-  retval = vsnprintf (str, n, fmt, args);
-  va_end (args);
-
-  if (retval < 0)
-    {
-      str[n-1] = '\0';
-      retval = strlen (str);
-    }
-
-  return retval;
-#else  /* !HAVE_VSNPRINTF */
-  gchar *printed;
-  va_list args;
-  
-  g_return_val_if_fail (str != NULL, 0);
-  g_return_val_if_fail (n > 0, 0);
-  g_return_val_if_fail (fmt != NULL, 0);
 
   va_start (args, fmt);
-  printed = g_strdup_vprintf (fmt, args);
+  retval = g_vsnprintf (str, n, fmt, args);
   va_end (args);
   
-  strncpy (str, printed, n);
-  str[n-1] = '\0';
-
-  g_free (printed);
-  
-  return strlen (str);
-#endif /* !HAVE_VSNPRINTF */
+  return retval;
 }
 
 gint
@@ -387,37 +357,30 @@ g_vsnprintf (gchar         *str,
             gchar const *fmt,
             va_list      args)
 {
-#ifdef HAVE_VSNPRINTF
-  gint retval;
-  
-  g_return_val_if_fail (str != NULL, 0);
-  g_return_val_if_fail (n > 0, 0);
+#ifdef HAVE_VSNPRINTF_C99
+  g_return_val_if_fail (n == 0 || str != NULL, 0);
   g_return_val_if_fail (fmt != NULL, 0);
 
-  retval = vsnprintf (str, n, fmt, args);
-  
-  if (retval < 0)
-    {
-      str[n-1] = '\0';
-      retval = strlen (str);
-    }
-
-  return retval;
-#else  /* !HAVE_VSNPRINTF */
+  return vsnprintf (str, n, fmt, args);
+#else  /* !HAVE_VSNPRINTF_C99 */
   gchar *printed;
-  
-  g_return_val_if_fail (str != NULL, 0);
-  g_return_val_if_fail (n > 0, 0);
+  gint retval;
+
+  g_return_val_if_fail (n == 0 || str != NULL, 0);
   g_return_val_if_fail (fmt != NULL, 0);
 
   printed = g_strdup_vprintf (fmt, args);
-  strncpy (str, printed, n);
-  str[n-1] = '\0';
+  retval = strlen (printed);
+  if (n > 0)
+    {
+      strncpy (str, printed, n - 1);
+      str[n-1] = '\0';
+    }
 
   g_free (printed);
   
-  return strlen (str);
-#endif /* !HAVE_VSNPRINTF */
+  return retval;
+#endif /* !HAVE_VSNPRINTF_C99 */
 }
 
 guint       
index 0c7b5c2..73eb86a 100644 (file)
@@ -383,5 +383,15 @@ main (int   argc,
 
 #undef S
 
+  {
+    gchar buf[5];
+    
+    TEST (NULL, 3 == g_snprintf (buf, 0, "%s", "abc"));
+    TEST (NULL, 3 == g_snprintf (NULL,0, "%s", "abc"));
+    TEST (NULL, 3 == g_snprintf (buf, 5, "%s", "abc"));
+    TEST (NULL, 4 == g_snprintf (buf, 5, "%s", "abcd"));
+    TEST (NULL, 9 == g_snprintf (buf, 5, "%s", "abcdefghi"));
+  }
+
   return any_failed;
 }