1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 11:58:13 +00:00

StringBuilder: Use a ByteBuffer internally instead of a Vector<String>.

This commit is contained in:
Andreas Kling 2019-01-18 03:27:51 +01:00
parent 074edffc44
commit 9d7da26b4e
6 changed files with 58 additions and 37 deletions

View file

@ -47,6 +47,8 @@ public:
m_size = size;
}
void grow(size_t size);
private:
enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt };
explicit Buffer(size_t); // For ConstructionMode=Uninitialized
@ -87,7 +89,19 @@ inline Buffer<T>::Buffer(T* elements, size_t size, ConstructionMode mode)
} else if (mode == Wrap) {
m_owned = false;
}
}
template<typename T>
inline void Buffer<T>::grow(size_t size)
{
ASSERT(size > m_size);
ASSERT(m_owned);
T* new_elements = static_cast<T*>(kmalloc(size * sizeof(T)));
memcpy(new_elements, m_elements, m_size * sizeof(T));
T* old_elements = m_elements;
m_elements = new_elements;
m_size = size;
kfree(old_elements);
}
template<typename T>

View file

@ -73,6 +73,14 @@ public:
return copy(offset_pointer(offset), size);
}
void grow(size_t size)
{
if (!m_impl)
m_impl = Buffer<byte>::create_uninitialized(size);
else
m_impl->grow(size);
}
private:
explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl)
: m_impl(move(impl))

View file

@ -38,7 +38,7 @@ bool FileSystemPath::canonicalize(bool resolve_symbolic_links)
StringBuilder builder;
for (auto& cpart : canonical_parts) {
builder.append('/');
builder.append(move(cpart));
builder.append(cpart);
}
m_string = builder.build();
return true;

View file

@ -1,22 +1,30 @@
#include "StringBuilder.h"
#include <LibC/stdarg.h>
#include "printf.cpp"
#include <AK/StdLibExtras.h>
namespace AK {
void StringBuilder::append(String&& str)
inline void StringBuilder::will_append(size_t size)
{
m_strings.append(move(str));
if ((m_length + size) > m_buffer.size())
m_buffer.grow(max(16u, m_buffer.size() * 2 + size));
}
void StringBuilder::append(const String& str)
{
m_strings.append(str);
if (str.is_empty())
return;
will_append(str.length());
memcpy(m_buffer.pointer() + m_length, str.characters(), str.length());
m_length += str.length();
}
void StringBuilder::append(char ch)
{
m_strings.append(StringImpl::create(&ch, 1));
will_append(1);
m_buffer.pointer()[m_length] = ch;
m_length += 1;
}
void StringBuilder::appendf(const char* fmt, ...)
@ -29,27 +37,15 @@ void StringBuilder::appendf(const char* fmt, ...)
va_end(ap);
}
ByteBuffer StringBuilder::to_byte_buffer()
{
m_buffer.trim(m_length);
return m_buffer;
}
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));
return String((const char*)m_buffer.pointer(), m_length);
}
}

View file

@ -11,14 +11,17 @@ public:
~StringBuilder() { }
void append(const String&);
void append(String&&);
void append(char);
void appendf(const char*, ...);
String build();
ByteBuffer to_byte_buffer();
private:
Vector<String> m_strings;
void will_append(size_t);
ByteBuffer m_buffer;
size_t m_length { 0 };
};
}