Add string

This commit is contained in:
2025-01-03 01:22:21 +01:00
parent 05de08dab9
commit eb58edf811
6 changed files with 122 additions and 3 deletions

View File

@ -19,6 +19,7 @@ cc_library(
"option.hpp", "option.hpp",
"print.hpp", "print.hpp",
"span.hpp", "span.hpp",
"string.hpp",
"string_view.hpp", "string_view.hpp",
"utility.hpp", "utility.hpp",
], ],
@ -56,6 +57,7 @@ cc_library(
"meta", "meta",
"option", "option",
"span", "span",
"string",
"string_view", "string_view",
"utility", "utility",
]] ]]

View File

@ -192,10 +192,23 @@ private:
public: public:
constexpr buffer() requires default_constructible<Allocator> = default; constexpr buffer() requires default_constructible<Allocator> = default;
explicit constexpr buffer(span<const T> s)
requires default_constructible<Allocator>
: buffer{}
{
copy_range(s);
}
explicit constexpr buffer(Allocator allocator) explicit constexpr buffer(Allocator allocator)
: m_allocator{ASL_MOVE(allocator)} : m_allocator{ASL_MOVE(allocator)}
{} {}
explicit constexpr buffer(span<const T> s, Allocator allocator)
: m_allocator{ASL_MOVE(allocator)}
{
copy_range(s);
}
constexpr buffer(const buffer& other) constexpr buffer(const buffer& other)
requires copy_constructible<Allocator> && copyable<T> requires copy_constructible<Allocator> && copyable<T>
: m_allocator{other.m_allocator} : m_allocator{other.m_allocator}
@ -344,12 +357,22 @@ public:
} }
// @Todo(C++23) Deducing this // @Todo(C++23) Deducing this
operator span<const T>() const // NOLINT(*-explicit-conversions) constexpr operator span<const T>() const // NOLINT(*-explicit-conversions)
{
return as_span();
}
constexpr operator span<T>() // NOLINT(*-explicit-conversions)
{
return as_span();
}
constexpr span<const T> as_span() const
{ {
return span<const T>{data(), size()}; return span<const T>{data(), size()};
} }
operator span<T>() // NOLINT(*-explicit-conversions) constexpr span<T> as_span()
{ {
return span<T>{data(), size()}; return span<T>{data(), size()};
} }

62
asl/string.hpp Normal file
View File

@ -0,0 +1,62 @@
#pragma once
#include "asl/buffer.hpp"
#include "asl/string_view.hpp"
namespace asl
{
template<allocator Allocator = DefaultAllocator>
class string
{
buffer<char, Allocator> m_buffer;
public:
constexpr string() requires default_constructible<Allocator> = default;
explicit constexpr string(Allocator allocator) : m_buffer{ASL_MOVE(allocator)} {}
// NOLINTNEXTLINE(*-explicit-conversions)
constexpr string(string_view sv)
requires default_constructible<Allocator>
: m_buffer{sv.as_span()}
{}
constexpr string(string_view sv, Allocator allocator)
: m_buffer{sv.as_span(), ASL_MOVE(allocator)}
{}
constexpr ~string() = default;
constexpr string(const string&) requires copy_constructible<Allocator> = default;
constexpr string(string&&) = default;
constexpr string& operator=(const string&) requires copy_assignable<Allocator> = default;
constexpr string& operator=(string&&) = default;
constexpr isize_t size() const { return m_buffer.size(); }
constexpr const char* data() const { return m_buffer.data(); }
// NOLINTNEXTLINE(*-explicit-conversions)
constexpr operator string_view() const
{
return as_string_view();
}
constexpr string_view as_string_view() const
{
auto span = m_buffer.as_span();
return string_view{span.data(), span.size()};
}
constexpr bool operator==(const string& other) const
{
return as_string_view() == other.as_string_view();
}
constexpr bool operator==(string_view other) const
{
return as_string_view() == other;
}
};
} // namespace asl

View File

@ -131,6 +131,18 @@ ASL_TEST(push)
ASL_TEST_EXPECT(b[6] == 7); ASL_TEST_EXPECT(b[6] == 7);
} }
ASL_TEST(from_span)
{
int data[] = {1, 2, 4, 8};
asl::buffer<int> b{data};
ASL_TEST_EXPECT(b.size() == 4);
ASL_TEST_EXPECT(b[0] == 1);
ASL_TEST_EXPECT(b[1] == 2);
ASL_TEST_EXPECT(b[2] == 4);
ASL_TEST_EXPECT(b[3] == 8);
}
struct MoveableType struct MoveableType
{ {
int moved{}; int moved{};

View File

@ -0,0 +1,21 @@
#include "asl/string.hpp"
#include "asl/testing/testing.hpp"
#include "asl/format.hpp"
ASL_TEST(default)
{
asl::string s;
ASL_TEST_ASSERT(s.size() == 0);
ASL_TEST_ASSERT(s.as_string_view().size() == 0);
ASL_TEST_ASSERT(s == ""_sv);
ASL_TEST_ASSERT(s == s);
}
ASL_TEST(from_string_view)
{
asl::string s = "hello"_sv;
ASL_TEST_ASSERT(s.size() == 5);
ASL_TEST_ASSERT(s == "hello"_sv);
}
static_assert(asl::formattable<asl::string<>>);

View File

@ -1,4 +1,3 @@
string
hashing hashing
hash_set hash_set
hash_map hash_map