repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
const_string.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cstddef>
4#include <string_view>
5
6namespace librepr {
7
8template <std::size_t N>
10 char value[N + 1]{};
11 constexpr static auto size = N;
12
13 constexpr const_string() = default;
14
15 constexpr explicit(false) const_string(char const (&literal)[N + 1]) { // NOLINT
17 }
18
19 constexpr explicit const_string(std::string_view data) {
20 std::copy(data.data(), data.data() + N, std::begin(value));
21 }
22
23 [[nodiscard]] constexpr explicit operator std::string_view() const noexcept { return std::string_view{value}; }
24 [[nodiscard]] constexpr explicit operator char const*() const noexcept { return value; }
25
26 [[nodiscard]] constexpr auto to_sv() const noexcept { return std::string_view{value}; }
27 [[nodiscard]] constexpr bool empty() const noexcept { return size == 0; }
28
29 [[nodiscard]] constexpr char const& operator[](std::size_t Idx) const { return value[Idx]; }
30 friend constexpr auto operator==(const_string lhs, std::string_view rhs) noexcept { return rhs == lhs.data; }
31};
32
33template <std::size_t N>
34const_string(char const (&)[N]) -> const_string<N - 1>;
35} // namespace librepr::detail
T begin(T... args)
T copy(T... args)
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
Definition const_string.h:9
constexpr auto to_sv() const noexcept
Definition const_string.h:26
constexpr char const & operator[](std::size_t Idx) const
Definition const_string.h:29
constexpr const_string()=default
constexpr const_string(std::string_view data)
Definition const_string.h:19
constexpr bool empty() const noexcept
Definition const_string.h:27
friend constexpr auto operator==(const_string lhs, std::string_view rhs) noexcept
Definition const_string.h:30