Re: g_snprintf() implementation



Havoc Pennington <rhp@zirx.pair.com> writes:
> On Tue, 20 Apr 1999, Miguel de Icaza wrote:
> > Please make sure this is well documented (ie, contribute to the glib
> > documentation) and point out specifically that this is different from
> > vsnprintf().  Seasoned developers might assume the standard snprintf
> > behaviour. 
> > 
> 
> Are the www.gtk.org/rdp docs in CVS?
> 
> Anyway, a "diff" for g_snprintf() and g_vsnprintf():
> 
> - The output is guaranteed to not exceed n characters,
> + The output is guaranteed to be '\0'-terminated and fit within 
> + n characters, including the '\0'. Unlike GNU snprintf(), never
> + fails to '\0'-terminate. 
> 
> 
> - Returns: the length of the output string (some
> - implementations return -1 if the output was truncated). 
> + Returns: the length of the output string not including '\0'.
> + Unlike GNU snprintf(), never returns -1. If n-1 is returned,
> + the string may have been truncated.

Actually, I would like it to return the length that would have been
formatted if there had been enough space -- this is what the snprintf
in GLIBC and Solaris 2.6 are documented as doing. 

The code for glib's own implementation would look like

        printed = g_strdup_vprintf (format, args);
        retval = strlen (printed);
        strcpy (str, printed, n-1);
        str[n-1] = '\0';
        g_free (printed);
        return retval;

- Hari
-- 
Raja R Harinath ------------------------------ harinath@cs.umn.edu
"When all else fails, read the instructions."      -- Cahn's Axiom
"Our policy is, when in doubt, do the right thing."   -- Roy L Ash



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