1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:14:58 +00:00

LibC: Implement asprintf() and vasprintf()

These simply use StringBuilder::appendvf() internally which is not
optimal in terms of heap allocations, but simple enough and I don't
think they are performance sensitive functions anyway.
This commit is contained in:
Andreas Kling 2021-03-28 18:39:32 +02:00
parent 5f71bf0cc7
commit fdd01a0a07
2 changed files with 27 additions and 1 deletions

View file

@ -29,6 +29,7 @@
#include <AK/PrintfImplementation.h>
#include <AK/ScopedValueRollback.h>
#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
@ -880,6 +881,29 @@ int printf(const char* fmt, ...)
return ret;
}
int vasprintf(char** strp, const char* fmt, va_list ap)
{
StringBuilder builder;
builder.appendvf(fmt, ap);
VERIFY(builder.length() <= NumericLimits<int>::max());
int length = builder.length();
*strp = strdup(builder.to_string().characters());
return length;
}
int asprintf(char** strp, const char* fmt, ...)
{
StringBuilder builder;
va_list ap;
va_start(ap, fmt);
builder.appendvf(fmt, ap);
va_end(ap);
VERIFY(builder.length() <= NumericLimits<int>::max());
int length = builder.length();
*strp = strdup(builder.to_string().characters());
return length;
}
static void buffer_putch(char*& bufptr, char ch)
{
*bufptr++ = ch;