AK: Remove the m_length member for StringBuilder

Instead we can just use ByteBuffer::size() which already keeps track
of the buffer's size.
This commit is contained in:
Gunnar Beutner 2021-05-31 00:58:07 +02:00 committed by Ali Mohammad Pur
parent 4c32a128ef
commit de0aa44bb6
2 changed files with 5 additions and 9 deletions

View file

@ -18,7 +18,7 @@ namespace AK {
inline void StringBuilder::will_append(size_t size)
{
Checked<size_t> needed_capacity = m_length;
Checked<size_t> needed_capacity = m_buffer.size();
needed_capacity += size;
VERIFY(!needed_capacity.has_overflow());
// Prefer to completely use the existing capacity first
@ -41,7 +41,6 @@ void StringBuilder::append(const StringView& str)
return;
will_append(str.length());
m_buffer.append(str.characters_without_null_termination(), str.length());
m_length += str.length();
}
void StringBuilder::append(const char* characters, size_t length)
@ -53,7 +52,6 @@ void StringBuilder::append(char ch)
{
will_append(1);
m_buffer.append(&ch, 1);
m_length += 1;
}
void StringBuilder::appendvf(const char* fmt, va_list ap)
@ -83,13 +81,12 @@ String StringBuilder::build() const
StringView StringBuilder::string_view() const
{
return StringView { data(), m_length };
return StringView { data(), m_buffer.size() };
}
void StringBuilder::clear()
{
m_buffer.clear();
m_length = 0;
}
void StringBuilder::append_code_point(u32 code_point)

View file

@ -44,9 +44,9 @@ public:
[[nodiscard]] StringView string_view() const;
void clear();
[[nodiscard]] size_t length() const { return m_length; }
[[nodiscard]] bool is_empty() const { return m_length == 0; }
void trim(size_t count) { m_length -= count; }
[[nodiscard]] size_t length() const { return m_buffer.size(); }
[[nodiscard]] bool is_empty() const { return m_buffer.is_empty(); }
void trim(size_t count) { m_buffer.resize(m_buffer.size() - count); }
template<class SeparatorType, class CollectionType>
void join(const SeparatorType& separator, const CollectionType& collection)
@ -68,7 +68,6 @@ private:
static constexpr size_t inline_capacity = 128;
AK::Detail::ByteBuffer<inline_capacity> m_buffer;
size_t m_length { 0 };
};
}