Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Memory.h>
28#include <AK/PrintfImplementation.h>
29#include <AK/StdLibExtras.h>
30#include <AK/String.h>
31#include <AK/StringBuilder.h>
32#include <AK/StringView.h>
33
34namespace AK {
35
36inline void StringBuilder::will_append(size_t size)
37{
38 if ((m_length + size) > m_buffer.size())
39 m_buffer.grow(max(static_cast<size_t>(16), m_buffer.size() * 2 + size));
40}
41
42StringBuilder::StringBuilder(size_t initial_capacity)
43{
44 m_buffer.grow((int)initial_capacity);
45}
46
47void StringBuilder::append(const StringView& str)
48{
49 if (str.is_empty())
50 return;
51 will_append(str.length());
52 memcpy(m_buffer.data() + m_length, str.characters_without_null_termination(), str.length());
53 m_length += str.length();
54}
55
56void StringBuilder::append(const char* characters, size_t length)
57{
58 append(StringView { characters, length });
59}
60
61void StringBuilder::append(char ch)
62{
63 will_append(1);
64 m_buffer.data()[m_length] = ch;
65 m_length += 1;
66}
67
68void StringBuilder::appendvf(const char* fmt, va_list ap)
69{
70 printf_internal([this](char*&, char ch) {
71 append(ch);
72 },
73 nullptr, fmt, ap);
74}
75
76void StringBuilder::appendf(const char* fmt, ...)
77{
78 va_list ap;
79 va_start(ap, fmt);
80 will_append(strlen(fmt));
81 appendvf(fmt, ap);
82 va_end(ap);
83}
84
85ByteBuffer StringBuilder::to_byte_buffer() const
86{
87 ByteBuffer buffer_copy = m_buffer.isolated_copy();
88 buffer_copy.trim(m_length);
89 return buffer_copy;
90}
91
92String StringBuilder::to_string() const
93{
94 return String((const char*)m_buffer.data(), m_length);
95}
96
97String StringBuilder::build() const
98{
99 return to_string();
100}
101
102StringView StringBuilder::string_view() const
103{
104 return StringView { (const char*)m_buffer.data(), m_length };
105}
106
107void StringBuilder::clear()
108{
109 m_buffer.clear();
110 m_length = 0;
111}
112
113}