sandbox
Loading...
Searching...
No Matches
string_literal.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_UTILITY_STRING_LITERAL_HPP_
3#define LIBSBX_UTILITY_STRING_LITERAL_HPP_
4
5#include <utility>
6#include <string_view>
7#include <array>
8#include <limits>
9
10#include <fmt/format.h>
11
13
14namespace sbx::utility {
15
16namespace detail {
17
18template<std::forward_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel, std::forward_iterator OutputIterator>
19constexpr auto copy(InputIterator first, Sentinel last, OutputIterator result) -> OutputIterator {
20 while (first != last) {
21 *result++ = *first++;
22 }
23
24 return result;
25}
26
27} // namespace detail
28
29template<typename Character, std::size_t Size>
31
32public:
33
34 using character_type = Character;
35 using size_type = std::size_t;
36 using iterator = const character_type*;
37 using string_view_type = std::basic_string_view<character_type>;
38 using string_type = std::basic_string<character_type>;
39
40 static constexpr auto npos = std::numeric_limits<size_type>::max();
41
42 consteval basic_string_literal(const character_type (&data)[Size]) noexcept {
43 detail::copy(data, data + Size - 1, _data.data());
44 }
45
46 constexpr auto begin() const noexcept -> iterator {
47 return std::begin(_data);
48 }
49
50 constexpr auto end() const noexcept -> iterator {
51 return std::end(_data.data());
52 }
53
54 constexpr auto data() const noexcept -> const character_type* {
55 return _data.data();
56 }
57
58 constexpr auto size() const noexcept -> size_type {
59 return Size - 1;
60 }
61
62 constexpr auto is_empty() const noexcept -> bool {
63 return size() == 0;
64 }
65
66 constexpr auto operator[](const size_type index) const noexcept -> character_type {
67 return _data[index];
68 }
69
70 constexpr auto at(const size_type index) const noexcept -> character_type {
71 return _data.at(index);
72 }
73
74 constexpr auto hash() const noexcept -> std::size_t {
75 return fnv1a_hash<character_type, std::size_t>{}({_data.data(), _data.size()});
76 }
77
78 constexpr operator string_view_type() const noexcept {
79 return string_view_type{_data.data(), Size};
80 }
81
82 constexpr operator string_type() const noexcept {
83 return (Size != 0u) ? string_type{_data.data(), Size} : std::string{};
84 }
85
86 std::array<character_type, Size - 1> _data;
87
88}; // class basic_string_literal
89
90template<std::size_t Size>
92
93template<std::size_t Size>
95
96template<string_literal String>
97constexpr auto string_id() noexcept -> std::size_t {
98 return String.hash();
99}
100
101} // namespace sbx::utility
102
103template<std::size_t Size>
104struct fmt::formatter<sbx::utility::string_literal<Size>> {
105
106 template<typename ParseContext>
107 constexpr auto parse(ParseContext& context) -> decltype(context.begin()) {
108 return context.begin();
109 }
110
111 template<typename FormatContext>
112 auto format(const sbx::utility::string_literal<Size>& value, FormatContext& context) const -> decltype(context.out()) {
113 return fmt::format_to(context.out(), "{}", std::string{value.data(), value.size()});
114 }
115
116}; // struct fmt::formatter<sbx::utility::primitive<Type>>
117
118template<typename Character, size_t Size>
119struct std::hash<sbx::utility::basic_string_literal<Character, Size>> {
120
121 auto operator()(const sbx::utility::basic_string_literal<Character, Size>& literal) const noexcept -> std::size_t {
123 }
124
125}; // struct std::hash
126
127#endif // LIBSBX_UTILITY_STRING_LITERAL_HPP_
Definition: string_literal.hpp:30
Functor that implements the fnv1a hash algorithm.
Definition: hash.hpp:128