mimic++ v10.0.1
Loading...
Searching...
No Matches
StaticString.hpp
Go to the documentation of this file.
1// Copyright Dominic (DNKpp) Koepke 2024-2026.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef MIMICPP_UTILITIES_STATIC_STRING_HPP
7#define MIMICPP_UTILITIES_STATIC_STRING_HPP
8
9#include "mimic++/Fwd.hpp"
11
12#ifndef MIMICPP_DETAIL_IS_MODULE
13 #include <algorithm>
14 #include <array>
15 #include <concepts>
16 #include <cstddef>
17 #include <string>
18 #include <string_view>
19#endif
20
22{
23 template <typename Char, std::size_t length>
25 {
26 public:
27 // We intentionally keep the null-terminator.
28 std::array<Char, length + 1u> data;
29
30 [[nodiscard]] //
31 explicit(false) consteval StaticString(Char const (&arr)[length + 1u]) noexcept
32 {
33 std::ranges::copy(arr, data.begin());
34 }
35
36 [[nodiscard]]
37 static constexpr bool empty() noexcept
38 {
39 return length == 0u;
40 }
41
42 [[nodiscard]]
43 static constexpr std::size_t size() noexcept
44 {
45 return length;
46 }
47
48 [[nodiscard]]
49 constexpr auto begin() const noexcept
50 {
51 return data.cbegin();
52 }
53
54 [[nodiscard]]
55 constexpr auto end() const noexcept
56 {
57 return begin() + size();
58 }
59
60 [[nodiscard]]
61 constexpr std::basic_string_view<Char> view() const noexcept
62 {
63 return {begin(), end()};
64 }
65
66 [[nodiscard]]
67 constexpr std::basic_string<Char> str() const noexcept
68 {
69 return {begin(), end()};
70 }
71
72 [[nodiscard]]
73 friend bool operator==(StaticString const&, StaticString const&) = default;
74
75 template <std::convertible_to<std::basic_string_view<Char>> String>
76 [[nodiscard]]
77 constexpr bool operator==(String const& other) const noexcept
78 {
79 return view() == std::basic_string_view<Char>{other};
80 }
81 };
82
83 template <typename Char, std::size_t length>
84 StaticString(Char const(&)[length]) -> StaticString<Char, length - 1u>;
85}
86
87#endif
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Definition StaticString.hpp:25
constexpr std::basic_string< Char > str() const noexcept
Definition StaticString.hpp:67
friend bool operator==(StaticString const &, StaticString const &)=default
static constexpr bool empty() noexcept
Definition StaticString.hpp:37
constexpr auto end() const noexcept
Definition StaticString.hpp:55
constexpr auto begin() const noexcept
Definition StaticString.hpp:49
constexpr bool operator==(String const &other) const noexcept
Definition StaticString.hpp:77
std::array< Char, length+1u > data
Definition StaticString.hpp:28
static constexpr std::size_t size() noexcept
Definition StaticString.hpp:43
constexpr std::basic_string_view< Char > view() const noexcept
Definition StaticString.hpp:61
Definition Fwd.hpp:451