forked from Imagelibrary/binutils-gdb
Introduce string_printf
This introduces the string_printf function. Like asprintf, but returns a std::string. gdb/ChangeLog: 2016-11-08 Pedro Alves <palves@redhat.com> * Makefile.in (COMMON_OBS): Add utils-selftests.o. * common/common-utils.c (string_printf): New function. * common/common-utils.h: Include <string>. (string_printf): Declare. * utils-selftests.c: New file.
This commit is contained in:
@@ -150,6 +150,29 @@ xsnprintf (char *str, size_t size, const char *format, ...)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* See documentation in common-utils.h. */
|
||||
|
||||
std::string
|
||||
string_printf (const char* fmt, ...)
|
||||
{
|
||||
va_list vp;
|
||||
int size;
|
||||
|
||||
va_start (vp, fmt);
|
||||
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'. */
|
||||
va_start (vp, fmt);
|
||||
vsprintf (&str[0], fmt, vp);
|
||||
va_end (vp);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
char *
|
||||
savestring (const char *ptr, size_t len)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user