summaryrefslogtreecommitdiff
path: root/asl/strings/string.hpp
blob: 0f702281231c6aa2cbca865d3f0979f4dfcf4b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause

#pragma once

#include "asl/containers/buffer.hpp"
#include "asl/strings/string_view.hpp"

namespace asl
{

template<allocator Allocator = DefaultAllocator>
class string
{
    buffer<char, Allocator> m_buffer;

    explicit constexpr string(buffer<char, Allocator>&& buffer) :
        m_buffer{std::move(buffer)}
    {}

    template<allocator A>
    friend class StringBuilder;

public:
    constexpr string() requires default_constructible<Allocator> = default;
    explicit constexpr string(Allocator allocator) : m_buffer{std::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(), std::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;
    }

    template<typename H>
    friend H AslHashValue(H h, const string& str)
    {
        return H::combine(h, str.as_string_view());
    }
};

string() -> string<>;

} // namespace asl