From: Bruno Haible Date: Wed, 3 May 2006 12:11:35 +0000 (+0000) Subject: Optimize the use of xasprintf for string concatenation. X-Git-Tag: v0.15~197 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=25d7990e12e56643490f4c2133c8d4922cf95665;p=thirdparty%2Fgettext.git Optimize the use of xasprintf for string concatenation. --- diff --git a/gettext-tools/ChangeLog b/gettext-tools/ChangeLog index 3cec66d52..69a74f87a 100644 --- a/gettext-tools/ChangeLog +++ b/gettext-tools/ChangeLog @@ -1,3 +1,7 @@ +2006-04-30 Bruno Haible + + * configure.ac: Invoke gl_STDARG_H. + 2006-05-01 Bruno Haible * configure.ac: Pass a source-version to gt_JAVACOMP. Move gt_JAVAEXEC diff --git a/gettext-tools/configure.ac b/gettext-tools/configure.ac index 044bfc3a1..3b7dded00 100644 --- a/gettext-tools/configure.ac +++ b/gettext-tools/configure.ac @@ -134,6 +134,7 @@ AC_SUBST([MSGMERGE_LIBM]) dnl Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS(limits.h malloc.h pwd.h string.h unistd.h utime.h values.h) +gl_STDARG_H AM_STDBOOL_H gl_HEADER_UNISTD diff --git a/gettext-tools/lib/ChangeLog b/gettext-tools/lib/ChangeLog index 554f41536..9d9a94744 100644 --- a/gettext-tools/lib/ChangeLog +++ b/gettext-tools/lib/ChangeLog @@ -1,3 +1,10 @@ +2006-04-30 Bruno Haible + + * xvasprintf.c: Include limits.h, string.h, xsize.h. + (EOVERFLOW): Define fallback value. + (xstrcat): New function. + (xvasprintf): Recognize the special case of a string concatenation. + 2006-03-13 Bruno Haible * javaversion.h: New file. diff --git a/gettext-tools/lib/xvasprintf.c b/gettext-tools/lib/xvasprintf.c index 9e8476642..4e95375dc 100644 --- a/gettext-tools/lib/xvasprintf.c +++ b/gettext-tools/lib/xvasprintf.c @@ -1,5 +1,5 @@ /* vasprintf and asprintf with out-of-memory checking. - Copyright (C) 1999, 2002-2004 Free Software Foundation, Inc. + Copyright (C) 1999, 2002-2004, 2006 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,15 +23,89 @@ #include "xvasprintf.h" #include +#include +#include #include "vasprintf.h" #include "xalloc.h" +/* Checked size_t computations. */ +#include "xsize.h" + +/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */ +#ifndef EOVERFLOW +# define EOVERFLOW E2BIG +#endif + +static inline char * +xstrcat (size_t argcount, va_list args) +{ + char *result; + va_list ap; + size_t totalsize; + size_t i; + char *p; + + /* Determine the total size. */ + totalsize = 0; + va_copy (ap, args); + for (i = argcount; i > 0; i--) + { + const char *next = va_arg (ap, const char *); + totalsize = xsum (totalsize, strlen (next)); + } + + /* Don't return a string longer than INT_MAX, for consistency with + vasprintf(). */ + if (totalsize > INT_MAX) + { + errno = EOVERFLOW; + return NULL; + } + + /* Allocate and fill the result string. */ + result = (char *) xmalloc (totalsize + 1); + p = result; + va_copy (ap, args); + for (i = argcount; i > 0; i--) + { + const char *next = va_arg (ap, const char *); + size_t len = strlen (next); + memcpy (p, next, len); + p += len; + } + *p = '\0'; + + return result; +} + char * xvasprintf (const char *format, va_list args) { char *result; + /* Recognize the special case format = "%s...%s". It is a frequently used + idiom for string concatenation and needs to be fast. We don't want to + have a separate function xstrcat() for this purpose. */ + { + size_t argcount = 0; + const char *f; + + for (f = format;;) + { + if (*f == '\0') + /* Recognized the special case of string concatenation. */ + return xstrcat (argcount, args); + if (*f != '%') + break; + f++; + if (*f != 's') + break; + f++; + argcount++; + } + } + if (vasprintf (&result, format, args) < 0) { if (errno == ENOMEM) diff --git a/gettext-tools/m4/ChangeLog b/gettext-tools/m4/ChangeLog index 662f685f3..03569d590 100644 --- a/gettext-tools/m4/ChangeLog +++ b/gettext-tools/m4/ChangeLog @@ -1,3 +1,8 @@ +2006-04-30 Bruno Haible + + * stdarg.m4: New file. + * Makefile.am (EXTRA_DIST): Add it. + 2006-05-01 Bruno Haible * javacomp.m4 (gt_JAVACOMP): Accept a source-version and an optional diff --git a/gettext-tools/m4/Makefile.am b/gettext-tools/m4/Makefile.am index 9e455500a..2f5c71dea 100644 --- a/gettext-tools/m4/Makefile.am +++ b/gettext-tools/m4/Makefile.am @@ -91,6 +91,7 @@ sig_atomic_t.m4 \ siginfo.m4 \ signalblocking.m4 \ ssize_t.m4 \ +stdarg.m4 \ stdbool.m4 \ stpncpy.m4 \ strdup.m4 \