]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Optimize the use of xasprintf for string concatenation.
authorBruno Haible <bruno@clisp.org>
Wed, 3 May 2006 12:11:35 +0000 (12:11 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:13:13 +0000 (12:13 +0200)
gettext-tools/ChangeLog
gettext-tools/configure.ac
gettext-tools/lib/ChangeLog
gettext-tools/lib/xvasprintf.c
gettext-tools/m4/ChangeLog
gettext-tools/m4/Makefile.am

index 3cec66d52e840f56231b8b16542ef0754a5d7229..69a74f87a5f9724cb8cae8b19e4996314751c1de 100644 (file)
@@ -1,3 +1,7 @@
+2006-04-30  Bruno Haible  <bruno@clisp.org>
+
+       * configure.ac: Invoke gl_STDARG_H.
+
 2006-05-01  Bruno Haible  <bruno@clisp.org>
 
        * configure.ac: Pass a source-version to gt_JAVACOMP. Move gt_JAVAEXEC
index 044bfc3a1883141751b889ccb8aa001d6c475116..3b7dded009d5e1e1dd82a4ac4271bc64e56ad826 100644 (file)
@@ -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
 
index 554f41536da6bc3435d9ae73526e7f239182b313..9d9a94744f5450977af7730acaf5b1479ef140f9 100644 (file)
@@ -1,3 +1,10 @@
+2006-04-30  Bruno Haible  <bruno@clisp.org>
+
+       * 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  <bruno@clisp.org>
 
        * javaversion.h: New file.
index 9e8476642c188194ac7c4bcb99852c707f1eb7f3..4e95375dc3e75c4a3312613713f254e4d3dc495d 100644 (file)
@@ -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
 #include "xvasprintf.h"
 
 #include <errno.h>
+#include <limits.h>
+#include <string.h>
 
 #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)
index 662f685f3613f5aaaa6e70e85b58beea1fadb43d..03569d590f4faca157f0a7ff45c59a3d6b63a539 100644 (file)
@@ -1,3 +1,8 @@
+2006-04-30  Bruno Haible  <bruno@clisp.org>
+
+       * stdarg.m4: New file.
+       * Makefile.am (EXTRA_DIST): Add it.
+
 2006-05-01  Bruno Haible  <bruno@clisp.org>
 
        * javacomp.m4 (gt_JAVACOMP): Accept a source-version and an optional
index 9e455500aa6a4defea96298adbd4b80fb061e3ce..2f5c71dea20bcdf9082f47503adf37ed54189ca7 100644 (file)
@@ -91,6 +91,7 @@ sig_atomic_t.m4 \
 siginfo.m4 \
 signalblocking.m4 \
 ssize_t.m4 \
+stdarg.m4 \
 stdbool.m4 \
 stpncpy.m4 \
 strdup.m4 \