mio 1.1.0
Loading...
Searching...
No Matches
string_util.hpp
Go to the documentation of this file.
1/* Copyright 2017 https://github.com/mandreyel
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
4 * software and associated documentation files (the "Software"), to deal in the Software
5 * without restriction, including without limitation the rights to use, copy, modify,
6 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7 * permit persons to whom the Software is furnished to do so, subject to the following
8 * conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies
11 * or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
16 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
18 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20
21/*
22 * Copyright 2026 Maxtek Consulting
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 * SOFTWARE.
41 */
42
43#ifndef MIO_STRING_UTIL_HPP
44#define MIO_STRING_UTIL_HPP
45
46#include <type_traits>
47
48namespace mio { namespace detail {
49
50 template <typename S, typename C = typename std::decay<S>::type,
51 typename = decltype(std::declval<C>().data()),
52 typename = typename std::enable_if<
53 std::is_same<typename C::value_type, char>::value
54#ifdef _WIN32
55 || std::is_same<typename C::value_type, wchar_t>::value
56#endif
57 >::type>
59 {
60 using type = typename C::value_type;
61 };
62
63 template <class T>
64 struct char_type
65 {
67 };
68
69 // TODO: can we avoid this brute force approach?
70 template <>
71 struct char_type<char*>
72 {
73 using type = char;
74 };
75
76 template <>
77 struct char_type<const char*>
78 {
79 using type = char;
80 };
81
82 template <size_t N>
83 struct char_type<char[N]>
84 {
85 using type = char;
86 };
87
88 template <size_t N>
89 struct char_type<const char[N]>
90 {
91 using type = char;
92 };
93
94#ifdef _WIN32
95 template <>
96 struct char_type<wchar_t*>
97 {
98 using type = wchar_t;
99 };
100
101 template <>
102 struct char_type<const wchar_t*>
103 {
104 using type = wchar_t;
105 };
106
107 template <size_t N>
108 struct char_type<wchar_t[N]>
109 {
110 using type = wchar_t;
111 };
112
113 template <size_t N>
114 struct char_type<const wchar_t[N]>
115 {
116 using type = wchar_t;
117 };
118#endif // _WIN32
119
120 template <typename CharT, typename S>
122 {
123 static constexpr bool value = std::is_same<
124 CharT*,
125 // TODO: I'm so sorry for this... Can this be made cleaner?
126 typename std::add_pointer<typename std::remove_cv<typename std::remove_pointer<
127 typename std::decay<S>::type>::type>::type>::type>::value;
128 };
129
130 template <typename S>
131 struct is_c_str
132 {
133 static constexpr bool value = is_c_str_helper<char, S>::value;
134 };
135
136#ifdef _WIN32
137 template <typename S>
138 struct is_c_wstr
139 {
140 static constexpr bool value = is_c_str_helper<wchar_t, S>::value;
141 };
142#endif // _WIN32
143
144 template <typename S>
146 {
147 static constexpr bool value = is_c_str<S>::value
148#ifdef _WIN32
149 || is_c_wstr<S>::value
150#endif
151 ;
152 };
153
154 template <typename String, typename = decltype(std::declval<String>().data()),
155 typename = typename std::enable_if<!is_c_str_or_c_wstr<String>::value>::type>
156 const typename char_type<String>::type* c_str(const String& path) {
157 return path.data();
158 }
159
160 template <typename String, typename = decltype(std::declval<String>().empty()),
161 typename = typename std::enable_if<!is_c_str_or_c_wstr<String>::value>::type>
162 bool empty(const String& path) {
163 return path.empty();
164 }
165
166 template <typename String,
167 typename = typename std::enable_if<is_c_str_or_c_wstr<String>::value>::type>
168 const typename char_type<String>::type* c_str(String path) {
169 return path;
170 }
171
172 template <typename String,
173 typename = typename std::enable_if<is_c_str_or_c_wstr<String>::value>::type>
174 bool empty(String path) {
175 return !path || (*path == 0);
176 }
177
178}} // namespace mio::detail
179
180#endif // MIO_STRING_UTIL_HPP
const char_type< String >::type * c_str(const String &path)
Definition string_util.hpp:156
bool empty(const String &path)
Definition string_util.hpp:162
Definition string_util.hpp:48
char type
Definition string_util.hpp:73
char type
Definition string_util.hpp:85
char type
Definition string_util.hpp:79
char type
Definition string_util.hpp:91
Definition string_util.hpp:59
typename C::value_type type
Definition string_util.hpp:60
Definition string_util.hpp:65
typename char_type_helper< T >::type type
Definition string_util.hpp:66
Definition string_util.hpp:122
static constexpr bool value
Definition string_util.hpp:123
Definition string_util.hpp:146
static constexpr bool value
Definition string_util.hpp:147
Definition string_util.hpp:132
static constexpr bool value
Definition string_util.hpp:133