Add StringWriter, and use it instead of StringSink
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
#include "asl/string.hpp"
|
||||
#include "asl/atomic.hpp"
|
||||
#include "asl/format.hpp"
|
||||
#include "asl/string_builder.hpp"
|
||||
|
||||
// @Todo Use custom allocator
|
||||
using Allocator = asl::DefaultAllocator;
|
||||
@ -11,53 +12,14 @@ static Allocator g_allocator{};
|
||||
namespace
|
||||
{
|
||||
|
||||
// @Todo Make a proper string builder, replace the tests's StringSink too
|
||||
class StringSink : public asl::Writer
|
||||
{
|
||||
isize_t m_current_len{};
|
||||
char* m_data{};
|
||||
|
||||
public:
|
||||
~StringSink() override
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
void write(asl::span<const asl::byte> str) override
|
||||
{
|
||||
m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
|
||||
m_data,
|
||||
asl::layout::array<char>(m_current_len),
|
||||
asl::layout::array<char>(m_current_len + str.size())));
|
||||
|
||||
asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT
|
||||
|
||||
m_current_len += str.size();
|
||||
}
|
||||
|
||||
constexpr asl::string_view str() const { return {m_data, m_current_len}; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (m_data != nullptr)
|
||||
{
|
||||
m_current_len = 0;
|
||||
asl::GlobalHeap::dealloc(m_data, asl::layout::array<char>(m_current_len));
|
||||
m_data = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct StatusInternal
|
||||
{
|
||||
asl::string<Allocator> msg;
|
||||
asl::status_code code;
|
||||
asl::atomic<int32_t> ref_count;
|
||||
|
||||
// @Todo Once we have string builder, move the string instead
|
||||
|
||||
constexpr StatusInternal(asl::string_view msg_, asl::status_code code_)
|
||||
: msg{msg_, g_allocator}
|
||||
constexpr StatusInternal(asl::string<Allocator>&& msg_, asl::status_code code_)
|
||||
: msg{ASL_MOVE(msg_)}
|
||||
, code{code_}
|
||||
{
|
||||
ASL_ASSERT(code != asl::status_code::ok);
|
||||
@ -73,9 +35,9 @@ asl::status::status(status_code code, string_view msg)
|
||||
|
||||
asl::status::status(status_code code, string_view fmt, span<format_internals::type_erased_arg> args)
|
||||
{
|
||||
StringSink sink;
|
||||
StringWriter<Allocator> sink{g_allocator};
|
||||
format_internals::format(&sink, fmt, args);
|
||||
m_payload = alloc_new<StatusInternal>(g_allocator, sink.str(), code);
|
||||
m_payload = alloc_new<StatusInternal>(g_allocator, ASL_MOVE(sink).finish(), code);
|
||||
}
|
||||
|
||||
asl::status_code asl::status::code_internal() const
|
||||
|
@ -16,7 +16,7 @@ class string
|
||||
{}
|
||||
|
||||
template<allocator A>
|
||||
friend class string_builder;
|
||||
friend class StringBuilder;
|
||||
|
||||
public:
|
||||
constexpr string() requires default_constructible<Allocator> = default;
|
||||
|
@ -3,26 +3,28 @@
|
||||
#include "asl/buffer.hpp"
|
||||
#include "asl/string.hpp"
|
||||
#include "asl/string_view.hpp"
|
||||
#include "asl/format.hpp"
|
||||
#include "asl/io.hpp"
|
||||
|
||||
namespace asl
|
||||
{
|
||||
|
||||
template<allocator Allocator = DefaultAllocator>
|
||||
class string_builder
|
||||
class StringBuilder
|
||||
{
|
||||
buffer<char, Allocator> m_buffer;
|
||||
|
||||
public:
|
||||
constexpr string_builder() requires default_constructible<Allocator> = default;
|
||||
explicit constexpr string_builder(Allocator allocator) : m_buffer{ASL_MOVE(allocator)} {}
|
||||
constexpr StringBuilder() requires default_constructible<Allocator> = default;
|
||||
explicit constexpr StringBuilder(Allocator allocator) : m_buffer{ASL_MOVE(allocator)} {}
|
||||
|
||||
constexpr ~string_builder() = default;
|
||||
constexpr ~StringBuilder() = default;
|
||||
|
||||
constexpr string_builder(const string_builder&) requires copy_constructible<Allocator> = default;
|
||||
constexpr string_builder(string_builder&&) = default;
|
||||
constexpr StringBuilder(const StringBuilder&) requires copy_constructible<Allocator> = default;
|
||||
constexpr StringBuilder(StringBuilder&&) = default;
|
||||
|
||||
constexpr string_builder& operator=(const string_builder&) requires copy_assignable<Allocator> = default;
|
||||
constexpr string_builder& operator=(string_builder&&) = default;
|
||||
constexpr StringBuilder& operator=(const StringBuilder&) requires copy_assignable<Allocator> = default;
|
||||
constexpr StringBuilder& operator=(StringBuilder&&) = default;
|
||||
|
||||
constexpr string_view as_string_view() const
|
||||
{
|
||||
@ -37,7 +39,7 @@ public:
|
||||
|
||||
// @Todo(C++23) Deducing this
|
||||
|
||||
string_builder& push(string_view sv) &
|
||||
StringBuilder& push(string_view sv) &
|
||||
{
|
||||
isize_t old_size = m_buffer.size();
|
||||
m_buffer.resize_zero(old_size + sv.size());
|
||||
@ -45,7 +47,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
string_builder&& push(string_view sv) &&
|
||||
StringBuilder&& push(string_view sv) &&
|
||||
{
|
||||
isize_t old_size = m_buffer.size();
|
||||
m_buffer.resize_zero(old_size + sv.size());
|
||||
@ -53,13 +55,13 @@ public:
|
||||
return ASL_MOVE(*this);
|
||||
}
|
||||
|
||||
string_builder& push(char c) &
|
||||
StringBuilder& push(char c) &
|
||||
{
|
||||
m_buffer.push(c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
string_builder&& push(char c) &&
|
||||
StringBuilder&& push(char c) &&
|
||||
{
|
||||
m_buffer.push(c);
|
||||
return ASL_MOVE(*this);
|
||||
@ -84,6 +86,71 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
string_builder() -> string_builder<>;
|
||||
StringBuilder() -> StringBuilder<>;
|
||||
|
||||
template<typename Allocator = DefaultAllocator>
|
||||
class StringWriter : public asl::Writer
|
||||
{
|
||||
StringBuilder<Allocator> m_builder;
|
||||
|
||||
public:
|
||||
constexpr StringWriter() requires default_constructible<Allocator> = default;
|
||||
explicit constexpr StringWriter(Allocator allocator) : m_builder{ASL_MOVE(allocator)} {}
|
||||
|
||||
constexpr ~StringWriter() override = default;
|
||||
|
||||
constexpr StringWriter(const StringWriter&) requires copy_constructible<Allocator> = default;
|
||||
constexpr StringWriter(StringWriter&&) = default;
|
||||
|
||||
constexpr StringWriter& operator=(const StringWriter&) requires copy_assignable<Allocator> = default;
|
||||
constexpr StringWriter& operator=(StringWriter&&) = default;
|
||||
|
||||
void write(span<const byte> str) override
|
||||
{
|
||||
m_builder.push(string_view{reinterpret_cast<const char*>(str.data()), str.size()});
|
||||
}
|
||||
|
||||
constexpr string_view as_string_view() const
|
||||
{
|
||||
return m_builder.as_string_view();
|
||||
}
|
||||
|
||||
string<Allocator> finish() &&
|
||||
{
|
||||
return ASL_MOVE(m_builder).finish();
|
||||
}
|
||||
|
||||
template<allocator StringAllocator = Allocator>
|
||||
string<StringAllocator> as_string()
|
||||
requires default_constructible<StringAllocator>
|
||||
{
|
||||
return m_builder.as_string();
|
||||
}
|
||||
|
||||
template<allocator StringAllocator = Allocator>
|
||||
string<StringAllocator> as_string(Allocator allocator)
|
||||
{
|
||||
return m_builder.as_string(ASL_MOVE(allocator));
|
||||
}
|
||||
};
|
||||
|
||||
StringWriter() -> StringWriter<>;
|
||||
|
||||
template<allocator Allocator = DefaultAllocator, formattable... Args>
|
||||
string<Allocator> format_to_string(string_view fmt, const Args&... args)
|
||||
requires default_constructible<Allocator>
|
||||
{
|
||||
StringWriter writer{};
|
||||
format(&writer, fmt, args...);
|
||||
return ASL_MOVE(writer).finish();
|
||||
}
|
||||
|
||||
template<allocator Allocator = DefaultAllocator, formattable... Args>
|
||||
string<Allocator> format_to_string(Allocator allocator, string_view fmt, const Args&... args)
|
||||
{
|
||||
StringWriter writer{ASL_MOVE(allocator)};
|
||||
format(&writer, fmt, args...);
|
||||
return ASL_MOVE(writer).finish();
|
||||
}
|
||||
|
||||
} // namespace asl
|
||||
|
@ -1,121 +1,91 @@
|
||||
#include "asl/format.hpp"
|
||||
#include "asl/testing/testing.hpp"
|
||||
#include "asl/float.hpp"
|
||||
#include "asl/tests/test_types.hpp"
|
||||
#include "asl/string_builder.hpp"
|
||||
|
||||
static_assert(asl::formattable<decltype("Hello")>);
|
||||
|
||||
ASL_TEST(format_args)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
// @Todo Introduce ASL_TEST_EXPECT_EQ, or ASL_TEST_EXPECT_STREQ
|
||||
|
||||
asl::format(&sink, "Hello, world!");
|
||||
ASL_TEST_EXPECT(sink.str() == "Hello, world!"_sv);
|
||||
auto s = asl::format_to_string("Hello, world!");
|
||||
ASL_TEST_EXPECT(s == "Hello, world!"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "");
|
||||
ASL_TEST_EXPECT(sink.str() == ""_sv);
|
||||
s = asl::format_to_string("");
|
||||
ASL_TEST_EXPECT(s == ""_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "Hello, {}!", "world");
|
||||
ASL_TEST_EXPECT(sink.str() == "Hello, world!"_sv);
|
||||
s = asl::format_to_string("Hello, {}!", "world");
|
||||
ASL_TEST_EXPECT(s == "Hello, world!"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "Hello, {}! {}", "world");
|
||||
ASL_TEST_EXPECT(sink.str() == "Hello, world! <ERROR>"_sv);
|
||||
s = asl::format_to_string("Hello, {}! {}", "world");
|
||||
ASL_TEST_EXPECT(s == "Hello, world! <ERROR>"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "Hello, pup!", "world");
|
||||
ASL_TEST_EXPECT(sink.str() == "Hello, pup!"_sv);
|
||||
s = asl::format_to_string("Hello, pup!", "world");
|
||||
ASL_TEST_EXPECT(s == "Hello, pup!"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{}", "CHEESE");
|
||||
ASL_TEST_EXPECT(sink.str() == "CHEESE"_sv);
|
||||
s = asl::format_to_string("{}", "CHEESE");
|
||||
ASL_TEST_EXPECT(s == "CHEESE"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{ ", "CHEESE");
|
||||
ASL_TEST_EXPECT(sink.str() == "<ERROR> "_sv);
|
||||
s = asl::format_to_string("{ ", "CHEESE");
|
||||
ASL_TEST_EXPECT(s == "<ERROR> "_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{", "CHEESE");
|
||||
ASL_TEST_EXPECT(sink.str() == "<ERROR>"_sv);
|
||||
s = asl::format_to_string("{", "CHEESE");
|
||||
ASL_TEST_EXPECT(s == "<ERROR>"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "a{{b");
|
||||
ASL_TEST_EXPECT(sink.str() == "a{b"_sv);
|
||||
s = asl::format_to_string("a{{b");
|
||||
ASL_TEST_EXPECT(s == "a{b"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{{{}}} }", "CHEESE");
|
||||
ASL_TEST_EXPECT(sink.str() == "{CHEESE} }"_sv);
|
||||
s = asl::format_to_string("{{{}}} }", "CHEESE");
|
||||
ASL_TEST_EXPECT(s == "{CHEESE} }"_sv);
|
||||
}
|
||||
|
||||
ASL_TEST(format_integers)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {}", 0, 1, 2);
|
||||
ASL_TEST_EXPECT(sink.str() == "0 1 2"_sv);
|
||||
auto s = asl::format_to_string("{} {} {}", 0, 1, 2);
|
||||
ASL_TEST_EXPECT(s == "0 1 2"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {}", 10, 11, 12);
|
||||
ASL_TEST_EXPECT(sink.str() == "10 11 12"_sv);
|
||||
s = asl::format_to_string("{} {} {}", 10, 11, 12);
|
||||
ASL_TEST_EXPECT(s == "10 11 12"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {}", 100, 101, 102);
|
||||
ASL_TEST_EXPECT(sink.str() == "100 101 102"_sv);
|
||||
s = asl::format_to_string("{} {} {}", 100, 101, 102);
|
||||
ASL_TEST_EXPECT(s == "100 101 102"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {}", 1000, 1001, 1002);
|
||||
ASL_TEST_EXPECT(sink.str() == "1000 1001 1002"_sv);
|
||||
s = asl::format_to_string("{} {} {}", 1000, 1001, 1002);
|
||||
ASL_TEST_EXPECT(s == "1000 1001 1002"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {} {}", -1, -23, -456, -7890);
|
||||
ASL_TEST_EXPECT(sink.str() == "-1 -23 -456 -7890"_sv);
|
||||
s = asl::format_to_string("{} {} {} {}", -1, -23, -456, -7890);
|
||||
ASL_TEST_EXPECT(s == "-1 -23 -456 -7890"_sv);
|
||||
}
|
||||
|
||||
ASL_TEST(format_floats)
|
||||
{
|
||||
StringSink sink;
|
||||
auto s = asl::format_to_string("{} {} {}", 0.0F, 1.0, 2.0F);
|
||||
ASL_TEST_EXPECT(s == "0 1 2"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {}", 0.0F, 1.0, 2.0F);
|
||||
ASL_TEST_EXPECT(sink.str() == "0 1 2"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {} {}", 0.1F, 0.001F, 0.123F);
|
||||
ASL_TEST_EXPECT(sink.str() == "0.1 0.001 0.123"_sv);
|
||||
s = asl::format_to_string("{} {} {}", 0.1F, 0.001F, 0.123F);
|
||||
ASL_TEST_EXPECT(s == "0.1 0.001 0.123"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {}", 1.25F, -22.3);
|
||||
ASL_TEST_EXPECT(sink.str() == "1.25 -22.3"_sv);
|
||||
s = asl::format_to_string("{} {}", 1.25F, -22.3);
|
||||
ASL_TEST_EXPECT(s == "1.25 -22.3"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{}", 1e32);
|
||||
ASL_TEST_EXPECT(sink.str() == "100000000000000000000000000000000"_sv);
|
||||
s = asl::format_to_string("{}", 1e32);
|
||||
ASL_TEST_EXPECT(s == "100000000000000000000000000000000"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{}", 123e-8);
|
||||
ASL_TEST_EXPECT(sink.str() == "0.00000123"_sv);
|
||||
s = asl::format_to_string("{}", 123e-8);
|
||||
ASL_TEST_EXPECT(s == "0.00000123"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {}", asl::infinity<float>(), -asl::infinity<double>());
|
||||
ASL_TEST_EXPECT(sink.str() == "Infinity -Infinity"_sv);
|
||||
s = asl::format_to_string("{} {}", asl::infinity<float>(), -asl::infinity<double>());
|
||||
ASL_TEST_EXPECT(s == "Infinity -Infinity"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{}", asl::nan<float>());
|
||||
ASL_TEST_EXPECT(sink.str() == "NaN"_sv);
|
||||
s = asl::format_to_string("{}", asl::nan<float>());
|
||||
ASL_TEST_EXPECT(s == "NaN"_sv);
|
||||
}
|
||||
|
||||
ASL_TEST(format_boolean)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "{} {}", true, false);
|
||||
ASL_TEST_EXPECT(sink.str() == "true false"_sv);
|
||||
auto s = asl::format_to_string("{} {}", true, false);
|
||||
ASL_TEST_EXPECT(s == "true false"_sv);
|
||||
}
|
||||
|
||||
struct CustomFormat
|
||||
@ -135,8 +105,6 @@ static_assert(asl::formattable<CustomFormat>);
|
||||
|
||||
ASL_TEST(format_custom)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
asl::format(&sink, "{}", CustomFormat{37});
|
||||
ASL_TEST_EXPECT(sink.str() == "(37)"_sv);
|
||||
auto s = asl::format_to_string("{}", CustomFormat{37});
|
||||
ASL_TEST_EXPECT(s == "(37)"_sv);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "asl/status.hpp"
|
||||
#include "asl/testing/testing.hpp"
|
||||
#include "asl/format.hpp"
|
||||
#include "asl/tests/test_types.hpp"
|
||||
#include "asl/string_builder.hpp"
|
||||
|
||||
ASL_TEST(simple_ok)
|
||||
{
|
||||
@ -68,21 +68,15 @@ static_assert(asl::formattable<asl::status>);
|
||||
|
||||
ASL_TEST(format)
|
||||
{
|
||||
StringSink sink;
|
||||
auto s = asl::format_to_string("-{}-", asl::ok());
|
||||
ASL_TEST_EXPECT(s == "-[ok]-"_sv);
|
||||
|
||||
asl::format(&sink, "-{}-", asl::ok());
|
||||
ASL_TEST_EXPECT(sink.str() == "-[ok]-"_sv);
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "-{}-", asl::internal_error("hello"));
|
||||
ASL_TEST_EXPECT(sink.str() == "-[internal: hello]-"_sv);
|
||||
s = asl::format_to_string("-{}-", asl::internal_error("hello"));
|
||||
ASL_TEST_EXPECT(s == "-[internal: hello]-"_sv);
|
||||
}
|
||||
|
||||
ASL_TEST(make_with_format)
|
||||
{
|
||||
StringSink sink;
|
||||
|
||||
sink.reset();
|
||||
asl::format(&sink, "-{}-", asl::internal_error("hello, {}, {}", 45, "world"));
|
||||
ASL_TEST_EXPECT(sink.str() == "-[internal: hello, 45, world]-"_sv);
|
||||
auto s = asl::format_to_string("-{}-", asl::internal_error("hello, {}, {}", 45, "world"));
|
||||
ASL_TEST_EXPECT(s == "-[internal: hello, 45, world]-"_sv);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
ASL_TEST(string_builder)
|
||||
{
|
||||
asl::string_builder b;
|
||||
asl::StringBuilder b;
|
||||
b.push('a');
|
||||
b.push("bcdef");
|
||||
b.push('g');
|
||||
@ -16,7 +16,7 @@ ASL_TEST(string_builder)
|
||||
|
||||
ASL_TEST(string_builder_rvalue)
|
||||
{
|
||||
asl::string s = asl::string_builder{}.push('a').push("bcdef").push('g').finish();
|
||||
asl::string s = asl::StringBuilder{}.push('a').push("bcdef").push('g').finish();
|
||||
|
||||
ASL_TEST_EXPECT(s == "abcdefg");
|
||||
}
|
||||
|
@ -90,40 +90,3 @@ struct DestructorObserver
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class StringSink : public asl::Writer
|
||||
{
|
||||
// @Todo Use string, once we have it, or a buffer
|
||||
isize_t m_current_len{};
|
||||
char* m_data{};
|
||||
|
||||
public:
|
||||
~StringSink() override
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
void write(asl::span<const asl::byte> str) override
|
||||
{
|
||||
m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
|
||||
m_data,
|
||||
asl::layout::array<char>(m_current_len),
|
||||
asl::layout::array<char>(m_current_len + str.size())));
|
||||
|
||||
asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT
|
||||
|
||||
m_current_len += str.size();
|
||||
}
|
||||
|
||||
constexpr asl::string_view str() const { return {m_data, m_current_len}; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (m_data != nullptr)
|
||||
{
|
||||
m_current_len = 0;
|
||||
asl::GlobalHeap::dealloc(m_data, asl::layout::array<char>(m_current_len));
|
||||
m_data = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user