| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_FORMAT_HELPERS_HPP |
| 4 |
#define MPT_FORMAT_HELPERS_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/string/types.hpp" |
| 11 |
#include "mpt/string_transcode/transcode.hpp" |
| 12 |
|
| 13 |
#include <string> |
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
namespace mpt { |
| 19 |
inline namespace MPT_INLINE_NS { |
| 20 |
|
| 21 |
|
| 22 |
template <typename Tdststring, typename Tsrcstring> |
| 23 |
inline Tdststring convert_formatted_simple(const Tsrcstring & src) { |
| 24 |
if constexpr (std::is_same<Tdststring, Tsrcstring>::value) { |
| 25 |
return src; |
| 26 |
} else { |
| 27 |
Tdststring dst; |
| 28 |
dst.reserve(src.length()); |
| 29 |
for (std::size_t i = 0; i < src.length(); ++i) { |
| 30 |
dst.push_back(mpt::unsafe_char_convert<typename Tdststring::value_type>(src[i])); |
| 31 |
} |
| 32 |
return dst; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
template <typename Tchar> |
| 38 |
struct select_format_char_type { |
| 39 |
using type = char; |
| 40 |
}; |
| 41 |
|
| 42 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 43 |
template <> |
| 44 |
struct select_format_char_type<wchar_t> { |
| 45 |
using type = wchar_t; |
| 46 |
}; |
| 47 |
#if MPT_USTRING_MODE_WIDE |
| 48 |
#if MPT_CXX_AT_LEAST(20) |
| 49 |
template <> |
| 50 |
struct select_format_char_type<char8_t> { |
| 51 |
using type = wchar_t; |
| 52 |
}; |
| 53 |
#endif // C++20 |
| 54 |
template <> |
| 55 |
struct select_format_char_type<char16_t> { |
| 56 |
using type = wchar_t; |
| 57 |
}; |
| 58 |
template <> |
| 59 |
struct select_format_char_type<char32_t> { |
| 60 |
using type = wchar_t; |
| 61 |
}; |
| 62 |
#endif // MPT_USTRING_MODE_WIDE |
| 63 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 64 |
|
| 65 |
|
| 66 |
template <typename Tstring> |
| 67 |
struct select_format_string_type { |
| 68 |
using type = mpt::ustring; |
| 69 |
}; |
| 70 |
|
| 71 |
template <> |
| 72 |
struct select_format_string_type<std::string> { |
| 73 |
using type = std::string; |
| 74 |
}; |
| 75 |
|
| 76 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 77 |
template <> |
| 78 |
struct select_format_string_type<std::wstring> { |
| 79 |
using type = std::wstring; |
| 80 |
}; |
| 81 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 82 |
|
| 83 |
#if MPT_CXX_AT_LEAST(20) |
| 84 |
template <> |
| 85 |
struct select_format_string_type<std::u8string> { |
| 86 |
using type = std::u8string; |
| 87 |
}; |
| 88 |
#endif // C++20 |
| 89 |
|
| 90 |
template <> |
| 91 |
struct select_format_string_type<std::u16string> { |
| 92 |
using type = std::u16string; |
| 93 |
}; |
| 94 |
|
| 95 |
template <> |
| 96 |
struct select_format_string_type<std::u32string> { |
| 97 |
using type = std::u32string; |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
} // namespace MPT_INLINE_NS |
| 102 |
} // namespace mpt |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#endif // MPT_FORMAT_HELPERS_HPP |