summaryrefslogtreecommitdiff
path: root/AK/StringBuilder.cpp
blob: f3766cc250b336ad55d3c7f2074bc9cb29ecaf71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "StringBuilder.h"

namespace AK {

void StringBuilder::append(String&& str)
{
    m_strings.append(move(str));
}

void StringBuilder::append(const String& str)
{
    m_strings.append(str);
}

void StringBuilder::append(char ch)
{
    m_strings.append(StringImpl::create(&ch, 1));
}

String StringBuilder::build()
{
    auto strings = move(m_strings);
    if (strings.is_empty())
        return String::empty();

    size_t sizeNeeded = 0;
    for (auto& string : strings)
        sizeNeeded += string.length();

    char* buffer;
    auto impl = StringImpl::create_uninitialized(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));
}

}