2018-10-10 05:53:07 -04:00
|
|
|
#include "StringBuilder.h"
|
2019-01-17 20:41:27 -05:00
|
|
|
#include <LibC/stdarg.h>
|
|
|
|
#include "printf.cpp"
|
2018-10-10 05:53:07 -04:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
void StringBuilder::append(String&& str)
|
|
|
|
{
|
2018-10-28 03:54:20 -04:00
|
|
|
m_strings.append(move(str));
|
2018-10-10 05:53:07 -04:00
|
|
|
}
|
|
|
|
|
2018-11-18 08:57:41 -05:00
|
|
|
void StringBuilder::append(const String& str)
|
|
|
|
{
|
|
|
|
m_strings.append(str);
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
void StringBuilder::append(char ch)
|
|
|
|
{
|
|
|
|
m_strings.append(StringImpl::create(&ch, 1));
|
|
|
|
}
|
|
|
|
|
2019-01-17 20:41:27 -05:00
|
|
|
void StringBuilder::appendf(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
printfInternal([this] (char*&, char ch) {
|
|
|
|
append(ch);
|
|
|
|
}, nullptr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
String StringBuilder::build()
|
|
|
|
{
|
2018-10-28 03:54:20 -04:00
|
|
|
auto strings = move(m_strings);
|
2018-12-20 20:10:45 -05:00
|
|
|
if (strings.is_empty())
|
2018-10-10 05:53:07 -04:00
|
|
|
return String::empty();
|
|
|
|
|
2018-10-29 16:54:11 -04:00
|
|
|
size_t sizeNeeded = 0;
|
2018-10-28 03:54:20 -04:00
|
|
|
for (auto& string : strings)
|
|
|
|
sizeNeeded += string.length();
|
|
|
|
|
|
|
|
char* buffer;
|
2018-12-20 20:10:45 -05:00
|
|
|
auto impl = StringImpl::create_uninitialized(sizeNeeded, buffer);
|
2018-10-28 03:54:20 -04:00
|
|
|
if (!impl)
|
|
|
|
return String();
|
|
|
|
|
|
|
|
for (auto& string : strings) {
|
|
|
|
memcpy(buffer, string.characters(), string.length());
|
|
|
|
buffer += string.length();
|
|
|
|
}
|
|
|
|
*buffer = '\0';
|
|
|
|
return String(move(impl));
|
2018-10-10 05:53:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|