2018-10-10 05:53:07 -04:00
|
|
|
#include "StringBuilder.h"
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::append(char ch)
|
|
|
|
{
|
|
|
|
m_strings.append(StringImpl::create(&ch, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
String StringBuilder::build()
|
|
|
|
{
|
2018-10-28 03:54:20 -04:00
|
|
|
auto strings = move(m_strings);
|
2018-10-10 05:53:07 -04:00
|
|
|
if (strings.isEmpty())
|
|
|
|
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;
|
|
|
|
auto impl = StringImpl::createUninitialized(sizeNeeded, buffer);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|