1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:07:45 +00:00

Add a String::format() and use that in place of ksprintf() in the Kernel.

You're never gonna be right 100% of the time when guessing how much buffer
space you need. This avoids having to make that type of decision in a bunch
of cases. :^)
This commit is contained in:
Andreas Kling 2019-01-30 16:28:51 +01:00
parent e9b948103d
commit 027d26cd5d
11 changed files with 40 additions and 34 deletions

View file

@ -95,6 +95,8 @@ public:
ByteBuffer to_byte_buffer() const;
static String format(const char*, ...);
private:
RetainPtr<StringImpl> m_impl;
};

View file

@ -1,5 +1,7 @@
#include "AKString.h"
#include "StdLibExtras.h"
#include "StringBuilder.h"
#include <LibC/stdarg.h>
namespace AK {
@ -92,4 +94,14 @@ unsigned String::toUInt(bool& ok) const
return value;
}
String String::format(const char* fmt, ...)
{
StringBuilder builder;
va_list ap;
va_start(ap, fmt);
builder.appendvf(fmt, ap);
va_end(ap);
return builder.build();
}
}

View file

@ -41,13 +41,18 @@ void StringBuilder::append(char ch)
m_length += 1;
}
void StringBuilder::appendvf(const char* fmt, va_list ap)
{
printfInternal([this] (char*&, char ch) {
append(ch);
}, nullptr, fmt, ap);
}
void StringBuilder::appendf(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
printfInternal([this] (char*&, char ch) {
append(ch);
}, nullptr, fmt, ap);
appendvf(fmt, ap);
va_end(ap);
}

View file

@ -2,6 +2,7 @@
#include "AKString.h"
#include "Vector.h"
#include <LibC/stdarg.h>
namespace AK {
@ -14,6 +15,7 @@ public:
void append(char);
void append(const char*, size_t);
void appendf(const char*, ...);
void appendvf(const char*, va_list);
String build();
ByteBuffer to_byte_buffer();