Line data Source code
1 : /**
2 : Copyright (c) 2016-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_COMMON_STRING_SPMETASTRING_H_
25 : #define STAPPLER_COMMON_STRING_SPMETASTRING_H_
26 :
27 : #include "SPStringView.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::metastring {
30 :
31 : template <char... Chars>
32 : struct metastring {
33 : template <typename Interface>
34 : static constexpr auto string() -> typename Interface::StringType { return { Chars ... }; }
35 : static std::string std_string() { return { Chars ... }; }
36 : static memory::string memory_string() { return { Chars ... }; }
37 : static constexpr std::array<char, sizeof... (Chars)> array() { return {{ Chars ... }}; }
38 :
39 : template <typename Interface>
40 : auto to_string() -> typename Interface::StringType const { return { Chars ... }; }
41 :
42 : std::string to_std_string() const { return {Chars ...}; }
43 130 : std::u16string to_std_ustring() const { return {char16_t(Chars) ...}; }
44 :
45 : memory::string to_memory_string() const { return {Chars ...}; }
46 : memory::u16string to_memory_ustring() const { return {char16_t(Chars) ...}; }
47 :
48 : constexpr std::array<char, sizeof... (Chars)> to_array() const { return {{Chars...}}; }
49 :
50 : operator memory::string() const { return {Chars ...}; }
51 0 : operator std::string() const { return {Chars ...}; }
52 :
53 : constexpr size_t size() const { return sizeof ... (Chars); }
54 : };
55 :
56 : template <char... Lhs, char ... Rhs>
57 10 : constexpr inline auto merge(const metastring<Lhs ...> &, const metastring<Rhs ...> &) {
58 10 : return metastring<Lhs ..., Rhs ...>();
59 : }
60 :
61 : template <typename ... Args, char... Lhs, char ... Rhs>
62 : constexpr inline auto merge(const metastring<Lhs ...> &lhs, const metastring<Rhs ...> &rhs, Args && ... args) {
63 : return merge(lhs, merge(rhs, args ...));
64 : }
65 :
66 : template <char... Lhs>
67 : constexpr inline auto merge(const metastring<Lhs ...> &lhs) {
68 : return lhs;
69 : }
70 :
71 : constexpr inline auto merge() {
72 : return metastring<>();
73 : }
74 :
75 : constexpr int num_digits (size_t x) { return x < 10 ? 1 : 1 + num_digits (x / 10); }
76 :
77 : template<int size, size_t x, char... args>
78 : struct numeric_builder {
79 : using type = typename numeric_builder<size - 1, x / 10, '0' + (x % 10), args...>::type;
80 : };
81 :
82 : template<size_t x, char... args>
83 : struct numeric_builder<2, x, args...> {
84 : using type = metastring<'0' + x / 10, '0' + (x % 10), args...>;
85 : };
86 :
87 : template<size_t x, char... args>
88 : struct numeric_builder<1, x, args...> {
89 : using type = metastring<'0' + x, args...>;
90 : };
91 :
92 : template<size_t x>
93 : using numeric = typename numeric_builder<num_digits (x), x>::type;
94 :
95 : }
96 :
97 :
98 : namespace STAPPLER_VERSIONIZED stappler {
99 :
100 : #ifdef __clang__
101 : #pragma clang diagnostic push
102 : #pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
103 : #endif
104 :
105 130 : template <typename CharType, CharType ... Chars> auto operator "" _meta() {
106 130 : return metastring::metastring<Chars ...>();
107 : }
108 :
109 : #ifdef __clang__
110 : #pragma clang diagnostic pop
111 : #endif
112 :
113 : }
114 :
115 :
116 : namespace STAPPLER_VERSIONIZED stappler::metastring {
117 :
118 : template <char ... Chars>
119 : inline std::basic_ostream<char> &
120 : operator << (std::basic_ostream<char> & os, const metastring<Chars ...> &str) {
121 : auto arr = str.to_array();
122 : return os.write(arr.data(), arr.size());
123 : }
124 :
125 : }
126 :
127 : #endif /* STAPPLER_CORE_STRING_SPMETASTRING_H_ */
|