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