| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_STRING_TYPES_HPP |
| 4 |
#define MPT_STRING_TYPES_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/integer.hpp" |
| 10 |
#include "mpt/base/macros.hpp" |
| 11 |
#include "mpt/base/namespace.hpp" |
| 12 |
#include "mpt/detect/mfc.hpp" |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <string_view> |
| 16 |
#include <type_traits> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include <cstddef> |
| 20 |
|
| 21 |
#if MPT_OS_WINDOWS |
| 22 |
#include <windows.h> |
| 23 |
#endif // MPT_OS_WINDOWS |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace mpt { |
| 28 |
inline namespace MPT_INLINE_NS { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
enum class common_encoding { |
| 33 |
utf8, |
| 34 |
ascii, // strictly 7-bit ASCII |
| 35 |
iso8859_1, |
| 36 |
iso8859_15, |
| 37 |
cp437, |
| 38 |
cp737, |
| 39 |
cp775, |
| 40 |
cp850, |
| 41 |
cp852, |
| 42 |
cp855, |
| 43 |
cp857, |
| 44 |
cp860, |
| 45 |
cp861, |
| 46 |
cp862, |
| 47 |
cp863, |
| 48 |
cp864, |
| 49 |
cp865, |
| 50 |
cp866, |
| 51 |
cp869, |
| 52 |
cp874, |
| 53 |
windows1252, |
| 54 |
amiga, |
| 55 |
riscos, |
| 56 |
atarist, |
| 57 |
iso8859_1_no_c1, |
| 58 |
iso8859_15_no_c1, |
| 59 |
amiga_no_c1, |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
enum class logical_encoding { |
| 64 |
locale, // CP_ACP on windows, system configured C locale otherwise |
| 65 |
active_locale, // active C/C++ global locale |
| 66 |
}; |
| 67 |
|
| 68 |
// source code / preprocessor (i.e. # token) |
| 69 |
inline constexpr auto source_encoding = common_encoding::ascii; |
| 70 |
|
| 71 |
// debug log files |
| 72 |
inline constexpr auto logfile_encoding = common_encoding::utf8; |
| 73 |
|
| 74 |
// std::clog / std::cout / std::cerr |
| 75 |
inline constexpr auto stdio_encoding = logical_encoding::locale; |
| 76 |
|
| 77 |
// getenv |
| 78 |
inline constexpr auto environment_encoding = logical_encoding::locale; |
| 79 |
|
| 80 |
// std::exception::what() |
| 81 |
inline constexpr auto exception_encoding = logical_encoding::locale; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
template <typename T> |
| 88 |
struct is_character : public std::false_type { }; |
| 89 |
|
| 90 |
template <> |
| 91 |
struct is_character<char> : public std::true_type { }; |
| 92 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 93 |
template <> |
| 94 |
struct is_character<wchar_t> : public std::true_type { }; |
| 95 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 96 |
#if MPT_CXX_AT_LEAST(20) |
| 97 |
template <> |
| 98 |
struct is_character<char8_t> : public std::true_type { }; |
| 99 |
#endif // C++20 |
| 100 |
template <> |
| 101 |
struct is_character<char16_t> : public std::true_type { }; |
| 102 |
template <> |
| 103 |
struct is_character<char32_t> : public std::true_type { }; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
template <typename T, typename std::enable_if<mpt::is_character<T>::value, bool>::type = true> |
| 110 |
MPT_CONSTEXPRINLINE typename std::make_unsigned<T>::type char_value(T x) noexcept { |
| 111 |
return static_cast<typename std::make_unsigned<T>::type>(x); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
template <typename T> |
| 119 |
struct unsafe_char_converter { }; |
| 120 |
|
| 121 |
template <> |
| 122 |
struct unsafe_char_converter<char> { |
| 123 |
static constexpr char32_t decode(char c) noexcept { |
| 124 |
return static_cast<char32_t>(static_cast<uint32>(static_cast<unsigned char>(c))); |
| 125 |
} |
| 126 |
static constexpr char encode(char32_t c) noexcept { |
| 127 |
return static_cast<char>(static_cast<unsigned char>(static_cast<uint32>(c))); |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 132 |
template <> |
| 133 |
struct unsafe_char_converter<wchar_t> { |
| 134 |
static constexpr char32_t decode(wchar_t c) noexcept { |
| 135 |
return static_cast<char32_t>(static_cast<uint32>(static_cast<std::make_unsigned<wchar_t>::type>(c))); |
| 136 |
} |
| 137 |
static constexpr wchar_t encode(char32_t c) noexcept { |
| 138 |
return static_cast<wchar_t>(static_cast<std::make_unsigned<wchar_t>::type>(static_cast<uint32>(c))); |
| 139 |
} |
| 140 |
}; |
| 141 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 142 |
|
| 143 |
#if MPT_CXX_AT_LEAST(20) |
| 144 |
template <> |
| 145 |
struct unsafe_char_converter<char8_t> { |
| 146 |
static constexpr char32_t decode(char8_t c) noexcept { |
| 147 |
return static_cast<char32_t>(static_cast<uint32>(static_cast<uint8>(c))); |
| 148 |
} |
| 149 |
static constexpr char8_t encode(char32_t c) noexcept { |
| 150 |
return static_cast<char8_t>(static_cast<uint8>(static_cast<uint32>(c))); |
| 151 |
} |
| 152 |
}; |
| 153 |
#endif // C++20 |
| 154 |
|
| 155 |
template <> |
| 156 |
struct unsafe_char_converter<char16_t> { |
| 157 |
static constexpr char32_t decode(char16_t c) noexcept { |
| 158 |
return static_cast<char32_t>(static_cast<uint32>(static_cast<uint16>(c))); |
| 159 |
} |
| 160 |
static constexpr char16_t encode(char32_t c) noexcept { |
| 161 |
return static_cast<char16_t>(static_cast<uint16>(static_cast<uint32>(c))); |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
template <> |
| 166 |
struct unsafe_char_converter<char32_t> { |
| 167 |
static constexpr char32_t decode(char32_t c) noexcept { |
| 168 |
return c; |
| 169 |
} |
| 170 |
static constexpr char32_t encode(char32_t c) noexcept { |
| 171 |
return c; |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
template <typename Tdstchar, typename Tsrcchar> |
| 176 |
constexpr Tdstchar unsafe_char_convert(Tsrcchar src) noexcept { |
| 177 |
return mpt::unsafe_char_converter<Tdstchar>::encode(mpt::unsafe_char_converter<Tsrcchar>::decode(src)); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 185 |
using widestring = std::wstring; |
| 186 |
using widestring_view = std::wstring_view; |
| 187 |
using widechar = wchar_t; |
| 188 |
#define MPT_WIDECHAR(x) L##x |
| 189 |
#define MPT_WIDELITERAL(x) L##x |
| 190 |
#define MPT_WIDESTRING(x) std::wstring(L##x) |
| 191 |
#define MPT_WIDESTRINGVIEW(x) std::wstring_view(L##x) |
| 192 |
#else // MPT_COMPILER_QUIRK_NO_WCHAR |
| 193 |
using widestring = std::u32string; |
| 194 |
using widestring_view = std::u32string_view; |
| 195 |
using widechar = char32_t; |
| 196 |
#define MPT_WIDECHAR(x) U##x |
| 197 |
#define MPT_WIDELITERAL(x) U##x |
| 198 |
#define MPT_WIDESTRING(x) std::u32string(U##x) |
| 199 |
#define MPT_WIDESTRINGVIEW(x) std::u32string_view(U##x) |
| 200 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
#if defined(MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT) |
| 205 |
// Work-around for VS2017 auto template argument ICE. |
| 206 |
// Use as encoding_char_traits<std::remove_const<decltype(foo)>::type, foo> instead of encoding_char_traits<foo> |
| 207 |
template <typename encoding_type, encoding_type encoding_tag> |
| 208 |
struct encoding_char_traits : std::char_traits<char> { |
| 209 |
static constexpr auto encoding() noexcept { |
| 210 |
return encoding_tag; |
| 211 |
} |
| 212 |
}; |
| 213 |
#else |
| 214 |
template <auto encoding_tag, typename encoding_type = decltype(encoding_tag)> |
| 215 |
struct encoding_char_traits : std::char_traits<char> { |
| 216 |
static constexpr auto encoding() noexcept { |
| 217 |
return encoding_tag; |
| 218 |
} |
| 219 |
}; |
| 220 |
#endif |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#if defined(MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT) |
| 225 |
using lstring = std::basic_string<char, mpt::encoding_char_traits<logical_encoding, logical_encoding::locale>>; |
| 226 |
using lstring_view = std::basic_string_view<char, mpt::encoding_char_traits<logical_encoding, logical_encoding::locale>>; |
| 227 |
#else |
| 228 |
using lstring = std::basic_string<char, mpt::encoding_char_traits<logical_encoding::locale>>; |
| 229 |
using lstring_view = std::basic_string_view<char, mpt::encoding_char_traits<logical_encoding::locale>>; |
| 230 |
#endif |
| 231 |
|
| 232 |
#if defined(MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT) |
| 233 |
using utf8string = std::basic_string<char, mpt::encoding_char_traits<common_encoding, common_encoding::utf8>>; |
| 234 |
using utf8string_view = std::basic_string_view<char, mpt::encoding_char_traits<common_encoding, common_encoding::utf8>>; |
| 235 |
#else |
| 236 |
using utf8string = std::basic_string<char, mpt::encoding_char_traits<common_encoding::utf8>>; |
| 237 |
using utf8string_view = std::basic_string_view<char, mpt::encoding_char_traits<common_encoding::utf8>>; |
| 238 |
#endif |
| 239 |
|
| 240 |
#if defined(MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT) |
| 241 |
using source_string = std::basic_string<char, mpt::encoding_char_traits<typename std::remove_const<decltype(source_encoding)>::type, source_encoding>>; |
| 242 |
using source_string_view = std::basic_string_view<char, mpt::encoding_char_traits<typename std::remove_const<decltype(source_encoding)>::type, source_encoding>>; |
| 243 |
#else |
| 244 |
using source_string = std::basic_string<char, mpt::encoding_char_traits<source_encoding>>; |
| 245 |
using source_string_view = std::basic_string_view<char, mpt::encoding_char_traits<source_encoding>>; |
| 246 |
#endif |
| 247 |
|
| 248 |
#if defined(MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT) |
| 249 |
using exception_string = std::basic_string<char, mpt::encoding_char_traits<typename std::remove_const<decltype(exception_encoding)>::type, exception_encoding>>; |
| 250 |
using exception_string_view = std::basic_string_view<char, mpt::encoding_char_traits<typename std::remove_const<decltype(exception_encoding)>::type, exception_encoding>>; |
| 251 |
#else |
| 252 |
using exception_string = std::basic_string<char, mpt::encoding_char_traits<exception_encoding>>; |
| 253 |
using exception_string_view = std::basic_string_view<char, mpt::encoding_char_traits<exception_encoding>>; |
| 254 |
#endif |
| 255 |
|
| 256 |
#if MPT_OS_WINDOWS |
| 257 |
|
| 258 |
template <typename Tchar> |
| 259 |
struct windows_char_traits { }; |
| 260 |
template <> |
| 261 |
struct windows_char_traits<CHAR> { |
| 262 |
using string_type = mpt::lstring; |
| 263 |
using string_view_type = mpt::lstring_view; |
| 264 |
}; |
| 265 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 266 |
template <> |
| 267 |
struct windows_char_traits<WCHAR> { |
| 268 |
using string_type = std::wstring; |
| 269 |
using string_view_type = std::wstring_view; |
| 270 |
}; |
| 271 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 272 |
|
| 273 |
using tstring = windows_char_traits<TCHAR>::string_type; |
| 274 |
using tstring_view = windows_char_traits<TCHAR>::string_view_type; |
| 275 |
|
| 276 |
using winstring = mpt::tstring; |
| 277 |
using winstring_view = mpt::tstring_view; |
| 278 |
|
| 279 |
#endif // MPT_OS_WINDOWS |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
#if MPT_CXX_AT_LEAST(20) |
| 284 |
|
| 285 |
using u8string = std::u8string; |
| 286 |
using u8string_view = std::u8string_view; |
| 287 |
using u8char = char8_t; |
| 288 |
#define MPT_U8CHAR(x) u8##x |
| 289 |
#define MPT_U8LITERAL(x) u8##x |
| 290 |
#define MPT_U8STRING(x) std::u8string(u8##x) |
| 291 |
#define MPT_U8STRINGVIEW(x) std::u8string_view(u8##x) |
| 292 |
|
| 293 |
#else // !C++20 |
| 294 |
|
| 295 |
#if defined(MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT) |
| 296 |
using u8string = std::basic_string<char, mpt::encoding_char_traits<common_encoding, common_encoding::utf8>>; |
| 297 |
using u8string_view = std::basic_string_view<char, mpt::encoding_char_traits<common_encoding, common_encoding::utf8>>; |
| 298 |
#else |
| 299 |
using u8string = std::basic_string<char, mpt::encoding_char_traits<common_encoding::utf8>>; |
| 300 |
using u8string_view = std::basic_string_view<char, mpt::encoding_char_traits<common_encoding::utf8>>; |
| 301 |
#endif |
| 302 |
using u8char = char; |
| 303 |
#define MPT_U8CHAR(x) x |
| 304 |
#define MPT_U8LITERAL(x) x |
| 305 |
#define MPT_U8STRING(x) mpt::u8string(x) |
| 306 |
#define MPT_U8STRINGVIEW(x) mpt::u8string_view(x) |
| 307 |
|
| 308 |
// mpt::u8string is a moderately type-safe string that is meant to contain |
| 309 |
// UTF-8 encoded char bytes. |
| 310 |
// |
| 311 |
// mpt::u8string is not implicitely convertible to/from std::string, but |
| 312 |
// it is convertible to/from C strings the same way as std::string is. |
| 313 |
// |
| 314 |
// The implementation of mpt::u8string is a compromise of compatibilty |
| 315 |
// with implementation-defined STL details, efficiency, source code size, |
| 316 |
// executable bloat, type-safety and simplicity. |
| 317 |
// |
| 318 |
// mpt::u8string is not meant to be used directly though. |
| 319 |
// mpt::u8string is meant as an alternative implementaion to std::wstring |
| 320 |
// for implementing the unicode string type mpt::ustring. |
| 321 |
|
| 322 |
#endif // C++20 |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
#if !defined(MPT_USTRING_MODE_UTF8_FORCE) && !defined(MPT_COMPILER_QUIRK_NO_WCHAR) && (MPT_COMPILER_MSVC || (MPT_DETECTED_MFC && defined(UNICODE))) |
| 327 |
// Use wide strings for MSVC because this is the native encoding on |
| 328 |
// microsoft platforms. |
| 329 |
#define MPT_USTRING_MODE_WIDE 1 |
| 330 |
#define MPT_USTRING_MODE_UTF8 0 |
| 331 |
#else |
| 332 |
#define MPT_USTRING_MODE_WIDE 0 |
| 333 |
#define MPT_USTRING_MODE_UTF8 1 |
| 334 |
#endif |
| 335 |
|
| 336 |
// mpt::ustring |
| 337 |
// |
| 338 |
// mpt::ustring is a string type that can hold unicode strings. |
| 339 |
// It is implemented as a std::basic_string either based on wchar_t (i.e. the |
| 340 |
// same as std::wstring) or a custom-defined char_traits class that is derived |
| 341 |
// from std::char_traits<char>. |
| 342 |
// The selection of the underlying implementation is done at compile-time. |
| 343 |
// MPT_UCHAR, MPT_ULITERAL and MPT_USTRING are macros that ease construction |
| 344 |
// of ustring char literals, ustring char array literals and ustring objects |
| 345 |
// from ustring char literals that work consistently in both modes. |
| 346 |
// Note that these are not supported for non-ASCII characters appearing in |
| 347 |
// the macro argument. |
| 348 |
// Also note that, as both UTF8 and UTF16 (it is less of an issue for UTF32) |
| 349 |
// are variable-length encodings and mpt::ustring is implemented as a |
| 350 |
// std::basic_string, all member functions that require individual character |
| 351 |
// access will not work consistently or even at all in a meaningful way. |
| 352 |
// This in particular affects operator[], find() and substr(). |
| 353 |
// The code makes no effort in preventing these or generating warnings when |
| 354 |
// these are used on mpt::ustring objects. However, compiling in the |
| 355 |
// respectively other mpt::ustring mode will catch most of these anyway. |
| 356 |
|
| 357 |
#if MPT_USTRING_MODE_WIDE && MPT_USTRING_MODE_UTF8 |
| 358 |
#error "MPT_USTRING_MODE_WIDE and MPT_USTRING_MODE_UTF8 are mutually exclusive." |
| 359 |
#endif |
| 360 |
|
| 361 |
#if MPT_USTRING_MODE_WIDE |
| 362 |
|
| 363 |
using ustring = std::wstring; |
| 364 |
using ustring_view = std::wstring_view; |
| 365 |
using uchar = wchar_t; |
| 366 |
#define MPT_UCHAR(x) L##x |
| 367 |
#define MPT_ULITERAL(x) L##x |
| 368 |
#define MPT_USTRING(x) std::wstring(L##x) |
| 369 |
#define MPT_USTRINGVIEW(x) std::wstring_view(L##x) |
| 370 |
|
| 371 |
#endif // MPT_USTRING_MODE_WIDE |
| 372 |
|
| 373 |
#if MPT_USTRING_MODE_UTF8 |
| 374 |
|
| 375 |
using ustring = mpt::u8string; |
| 376 |
using ustring_view = mpt::u8string_view; |
| 377 |
using uchar = mpt::u8char; |
| 378 |
#define MPT_UCHAR(x) MPT_U8CHAR(x) |
| 379 |
#define MPT_ULITERAL(x) MPT_U8LITERAL(x) |
| 380 |
#define MPT_USTRING(x) MPT_U8STRING(x) |
| 381 |
#define MPT_USTRINGVIEW(x) MPT_U8STRINGVIEW(x) |
| 382 |
|
| 383 |
#endif // MPT_USTRING_MODE_UTF8 |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
template <typename T> |
| 388 |
struct make_string_type { }; |
| 389 |
|
| 390 |
template <typename T, typename Ttraits> |
| 391 |
struct make_string_type<std::basic_string<T, Ttraits>> { |
| 392 |
using type = std::basic_string<T, Ttraits>; |
| 393 |
}; |
| 394 |
|
| 395 |
template <typename T, typename Ttraits> |
| 396 |
struct make_string_type<std::basic_string_view<T, Ttraits>> { |
| 397 |
using type = std::basic_string<T, Ttraits>; |
| 398 |
}; |
| 399 |
|
| 400 |
template <typename T> |
| 401 |
struct make_string_type<const T *> { |
| 402 |
using type = std::basic_string<T>; |
| 403 |
}; |
| 404 |
|
| 405 |
template <typename T> |
| 406 |
struct make_string_type<T *> { |
| 407 |
using type = std::basic_string<T>; |
| 408 |
}; |
| 409 |
|
| 410 |
template <typename T, std::size_t N> |
| 411 |
struct make_string_type<T[N]> { |
| 412 |
using type = typename make_string_type<T *>::type; |
| 413 |
}; |
| 414 |
|
| 415 |
#if MPT_DETECTED_MFC |
| 416 |
|
| 417 |
template <> |
| 418 |
struct make_string_type<CStringW> { |
| 419 |
using type = CStringW; |
| 420 |
}; |
| 421 |
|
| 422 |
template <> |
| 423 |
struct make_string_type<CStringA> { |
| 424 |
using type = CStringA; |
| 425 |
}; |
| 426 |
|
| 427 |
#endif // MPT_DETECTED_MFC |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
template <typename T> |
| 432 |
struct make_string_view_type { }; |
| 433 |
|
| 434 |
template <typename T, typename Ttraits> |
| 435 |
struct make_string_view_type<std::basic_string<T, Ttraits>> { |
| 436 |
using type = std::basic_string_view<T, Ttraits>; |
| 437 |
}; |
| 438 |
|
| 439 |
template <typename T, typename Ttraits> |
| 440 |
struct make_string_view_type<std::basic_string_view<T, Ttraits>> { |
| 441 |
using type = std::basic_string_view<T, Ttraits>; |
| 442 |
}; |
| 443 |
|
| 444 |
template <typename T> |
| 445 |
struct make_string_view_type<const T *> { |
| 446 |
using type = std::basic_string_view<T>; |
| 447 |
}; |
| 448 |
|
| 449 |
template <typename T> |
| 450 |
struct make_string_view_type<T *> { |
| 451 |
using type = std::basic_string_view<T>; |
| 452 |
}; |
| 453 |
|
| 454 |
template <typename T, std::size_t N> |
| 455 |
struct make_string_view_type<T[N]> { |
| 456 |
using type = typename make_string_view_type<T *>::type; |
| 457 |
}; |
| 458 |
|
| 459 |
#if MPT_DETECTED_MFC |
| 460 |
|
| 461 |
template <> |
| 462 |
struct make_string_view_type<CStringW> { |
| 463 |
using type = CStringW; |
| 464 |
}; |
| 465 |
|
| 466 |
template <> |
| 467 |
struct make_string_view_type<CStringA> { |
| 468 |
using type = CStringA; |
| 469 |
}; |
| 470 |
|
| 471 |
#endif // MPT_DETECTED_MFC |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
template <typename T> |
| 476 |
struct is_string_type : public std::false_type { }; |
| 477 |
template <> |
| 478 |
struct is_string_type<std::string> : public std::true_type { }; |
| 479 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 480 |
template <> |
| 481 |
struct is_string_type<std::wstring> : public std::true_type { }; |
| 482 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 483 |
#if MPT_CXX_AT_LEAST(20) |
| 484 |
template <> |
| 485 |
struct is_string_type<std::u8string> : public std::true_type { }; |
| 486 |
#endif // C++20 |
| 487 |
template <> |
| 488 |
struct is_string_type<std::u16string> : public std::true_type { }; |
| 489 |
template <> |
| 490 |
struct is_string_type<std::u32string> : public std::true_type { }; |
| 491 |
#if MPT_DETECTED_MFC |
| 492 |
template <> |
| 493 |
struct is_string_type<CStringW> : public std::true_type { }; |
| 494 |
template <> |
| 495 |
struct is_string_type<CStringA> : public std::true_type { }; |
| 496 |
#endif // MPT_DETECTED_MFC |
| 497 |
template <typename T, typename Ttraits> |
| 498 |
struct is_string_type<std::basic_string<T, Ttraits>> : public std::true_type { }; |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
template <typename T> |
| 503 |
struct is_string_view_type : public std::false_type { }; |
| 504 |
template <typename Tchar, typename Ttraits> |
| 505 |
struct is_string_view_type<std::basic_string_view<Tchar, Ttraits>> : public std::true_type { }; |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
template <typename T> |
| 510 |
inline typename mpt::make_string_type<typename std::decay<T>::type>::type as_string(T && str) { |
| 511 |
if constexpr (std::is_pointer<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::value) { |
| 512 |
return str ? typename mpt::make_string_type<typename std::decay<T>::type>::type{std::forward<T>(str)} : typename mpt::make_string_type<typename std::decay<T>::type>::type{}; |
| 513 |
} else if constexpr (std::is_pointer<typename std::decay<T>::type>::value) { |
| 514 |
return typename mpt::make_string_type<typename std::decay<T>::type>::type{std::forward<T>(str)}; |
| 515 |
} else if constexpr (mpt::is_string_view_type<typename std::decay<T>::type>::value) { |
| 516 |
return typename mpt::make_string_type<typename std::decay<T>::type>::type{std::forward<T>(str)}; |
| 517 |
} else { |
| 518 |
return std::forward<T>(str); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
template <typename T> |
| 525 |
inline typename mpt::make_string_view_type<typename std::decay<T>::type>::type as_string_view(T && str) { |
| 526 |
if constexpr (std::is_pointer<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::value) { |
| 527 |
return str ? typename mpt::make_string_view_type<typename std::decay<T>::type>::type{std::forward<T>(str)} : typename mpt::make_string_view_type<typename std::decay<T>::type>::type{}; |
| 528 |
} else if constexpr (std::is_pointer<typename std::decay<T>::type>::value) { |
| 529 |
return typename mpt::make_string_view_type<typename std::decay<T>::type>::type{std::forward<T>(str)}; |
| 530 |
} else if constexpr (mpt::is_string_view_type<typename std::decay<T>::type>::value) { |
| 531 |
return typename mpt::make_string_view_type<typename std::decay<T>::type>::type{std::forward<T>(str)}; |
| 532 |
} else { |
| 533 |
return std::forward<T>(str); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
} // namespace MPT_INLINE_NS |
| 540 |
} // namespace mpt |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
#endif // MPT_STRING_TYPES_HPP |