From eb58edf811a328ddcc5e671a258be208da212630 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 3 Jan 2025 01:22:21 +0100 Subject: Add string --- asl/string.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 asl/string.hpp (limited to 'asl/string.hpp') diff --git a/asl/string.hpp b/asl/string.hpp new file mode 100644 index 0000000..288767f --- /dev/null +++ b/asl/string.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include "asl/buffer.hpp" +#include "asl/string_view.hpp" + +namespace asl +{ + +template +class string +{ + buffer m_buffer; + +public: + constexpr string() requires default_constructible = default; + explicit constexpr string(Allocator allocator) : m_buffer{ASL_MOVE(allocator)} {} + + // NOLINTNEXTLINE(*-explicit-conversions) + constexpr string(string_view sv) + requires default_constructible + : 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 = default; + constexpr string(string&&) = default; + + constexpr string& operator=(const string&) requires copy_assignable = 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 -- cgit