]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Introduce string_vprintf
authorTom Tromey <tom@tromey.com>
Thu, 28 Sep 2017 13:44:50 +0000 (07:44 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 30 Sep 2017 02:46:42 +0000 (20:46 -0600)
This adds string_vprintf, a va_list variant of string_printf.  This
will be used in later patches.

gdb/ChangeLog
2017-09-29  Tom Tromey  <tom@tromey.com>

* unittests/common-utils-selftests.c (format): New function.
(string_vprintf_tests): New function.
(_initialize_common_utils_selftests): Register new tests.
* common/common-utils.c (string_vprintf): New function.
* common/common-utils.h (string_vprintf): Declare.

gdb/ChangeLog
gdb/common/common-utils.c
gdb/common/common-utils.h
gdb/unittests/common-utils-selftests.c

index aaa0f14d9f3fbd1c9b5292ab4fa43ec34ef5643a..b112b02acb68d95f9fac47c0bfb8fb1797fdfaa1 100644 (file)
@@ -1,3 +1,11 @@
+2017-09-29  Tom Tromey  <tom@tromey.com>
+
+       * unittests/common-utils-selftests.c (format): New function.
+       (string_vprintf_tests): New function.
+       (_initialize_common_utils_selftests): Register new tests.
+       * common/common-utils.c (string_vprintf): New function.
+       * common/common-utils.h (string_vprintf): Declare.
+
 2017-09-29  Pedro Alves  <palves@redhat.com>
 
        * common/rsp-low.c (unpack_varlen_hex): Constify.
index 6b10d11ad4da98d854d41a943b029ad0935c4d9e..d8c546a3d939a14ee6fc3555c39273eaf707d2d8 100644 (file)
@@ -174,6 +174,27 @@ string_printf (const char* fmt, ...)
   return str;
 }
 
+/* See documentation in common-utils.h.  */
+
+std::string
+string_vprintf (const char* fmt, va_list args)
+{
+  va_list vp;
+  size_t size;
+
+  va_copy (vp, args);
+  size = vsnprintf (NULL, 0, fmt, vp);
+  va_end (vp);
+
+  std::string str (size, '\0');
+
+  /* C++11 and later guarantee std::string uses contiguous memory and
+     always includes the terminating '\0'.  */
+  vsprintf (&str[0], fmt, args);
+
+  return str;
+}
+
 char *
 savestring (const char *ptr, size_t len)
 {
index 6475c2874371f1a5222c2f429feb1408fa5d4ae6..19724f9782ceec4f756ed4183d23fa354168c427 100644 (file)
@@ -63,6 +63,10 @@ int xsnprintf (char *str, size_t size, const char *format, ...)
 std::string string_printf (const char* fmt, ...)
   ATTRIBUTE_PRINTF (1, 2);
 
+/* Like string_printf, but takes a va_list.  */
+std::string string_vprintf (const char* fmt, va_list args)
+  ATTRIBUTE_PRINTF (1, 0);
+
 /* Make a copy of the string at PTR with LEN characters
    (and add a null character at the end in the copy).
    Uses malloc to get the space.  Returns the address of the copy.  */
index 71bc2dff7f664160670b1daa4f7ed27997fa585a..0f26b219af446dee299156ecf8d60e18a3189710 100644 (file)
@@ -41,10 +41,33 @@ string_printf_tests ()
   SELF_CHECK (string_printf ("%s", X100000) == X100000);
 }
 
+static std::string
+format (const char *fmt, ...)
+{
+  va_list vp;
+
+  va_start (vp, fmt);
+  std::string result = string_vprintf (fmt, vp);
+  va_end (vp);
+  return result;
+}
+
+static void
+string_vprintf_tests ()
+{
+  /* Basic smoke tests.  */
+  SELF_CHECK (format ("%s", "test") == "test");
+  SELF_CHECK (format ("%d", 23) == "23");
+  SELF_CHECK (format ("%s %d %s", "test", 23, "done")
+             == "test 23 done");
+  SELF_CHECK (format ("nothing") == "nothing");
+}
+
 } /* namespace selftests */
 
 void
 _initialize_common_utils_selftests ()
 {
   selftests::register_test ("string_printf", selftests::string_printf_tests);
+  selftests::register_test ("string_vprintf", selftests::string_vprintf_tests);
 }