|
| 1 | +// Copyright (c) Glyn Matthews 2012-2018. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#ifndef SKYR_URL_DETAILS_TRANSLATE_INC |
| 7 | +#define SKYR_URL_DETAILS_TRANSLATE_INC |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <skyr/unicode.hpp> |
| 11 | + |
| 12 | +namespace skyr { |
| 13 | +namespace details { |
| 14 | +template <typename Source> |
| 15 | +struct to_bytes_impl; |
| 16 | + |
| 17 | +template <> |
| 18 | +struct to_bytes_impl<std::string> { |
| 19 | + expected<std::string, std::error_code> operator()(const std::string &source) const { return source; } |
| 20 | +}; |
| 21 | + |
| 22 | +template <int N> |
| 23 | +struct to_bytes_impl<char[N]> { |
| 24 | + expected<std::string, std::error_code> operator()(const char *source) const { return source; } |
| 25 | +}; |
| 26 | + |
| 27 | +template <> |
| 28 | +struct to_bytes_impl<char *> { |
| 29 | + expected<std::string, std::error_code> operator()(const char *source) const { return source; } |
| 30 | +}; |
| 31 | + |
| 32 | +template <> |
| 33 | +struct to_bytes_impl<const char *> { |
| 34 | + expected<std::string, std::error_code> operator()(const char *source) const { return source; } |
| 35 | +}; |
| 36 | + |
| 37 | +template <int N> |
| 38 | +struct to_bytes_impl<const char[N]> { |
| 39 | + expected<std::string, std::error_code> operator()(const char *source) const { return source; } |
| 40 | +}; |
| 41 | + |
| 42 | +template <> |
| 43 | +struct to_bytes_impl<std::wstring> { |
| 44 | + expected<std::string, std::error_code> operator()(const std::wstring &source) const { |
| 45 | + return wstring_to_bytes(source); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +template <int N> |
| 50 | +struct to_bytes_impl<const wchar_t[N]> { |
| 51 | + expected<std::string, std::error_code> operator()(const wchar_t *source) const { |
| 52 | + to_bytes_impl<std::wstring> to_bytes; |
| 53 | + return to_bytes(source); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +template <int N> |
| 58 | +struct to_bytes_impl<wchar_t[N]> { |
| 59 | + expected<std::string, std::error_code> operator()(const wchar_t *source) const { |
| 60 | + to_bytes_impl<std::wstring> to_bytes; |
| 61 | + return to_bytes(source); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +template <> |
| 66 | +struct to_bytes_impl<wchar_t *> { |
| 67 | + expected<std::string, std::error_code> operator()(const wchar_t *source) const { |
| 68 | + to_bytes_impl<std::wstring> to_bytes; |
| 69 | + return to_bytes(source); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +template <> |
| 74 | +struct to_bytes_impl<const wchar_t *> { |
| 75 | + expected<std::string, std::error_code> operator()(const wchar_t *source) const { |
| 76 | + to_bytes_impl<std::wstring> to_bytes; |
| 77 | + return to_bytes(source); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +template <> |
| 82 | +struct to_bytes_impl<std::u16string> { |
| 83 | + expected<std::string, std::error_code> operator()(const std::u16string &source) const { |
| 84 | + return utf16_to_bytes(source); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +template <int N> |
| 89 | +struct to_bytes_impl<const char16_t[N]> { |
| 90 | + expected<std::string, std::error_code> operator()(const char16_t *source) const { |
| 91 | + to_bytes_impl<std::u16string> to_bytes; |
| 92 | + return to_bytes(source); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +template <int N> |
| 97 | +struct to_bytes_impl<char16_t[N]> { |
| 98 | + expected<std::string, std::error_code> operator()(const char16_t *source) const { |
| 99 | + to_bytes_impl<std::u16string> to_bytes; |
| 100 | + return to_bytes(source); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +template <> |
| 105 | +struct to_bytes_impl<char16_t *> { |
| 106 | + expected<std::string, std::error_code> operator()(const char16_t *source) const { |
| 107 | + to_bytes_impl<std::u16string> to_bytes; |
| 108 | + return to_bytes(source); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +template <> |
| 113 | +struct to_bytes_impl<const char16_t *> { |
| 114 | + expected<std::string, std::error_code> operator()(const char16_t *source) const { |
| 115 | + to_bytes_impl<std::u16string> to_bytes; |
| 116 | + return to_bytes(source); |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +template <> |
| 121 | +struct to_bytes_impl<std::u32string> { |
| 122 | + expected<std::string, std::error_code> operator()(const std::u32string &source) const { |
| 123 | + return utf32_to_bytes(source); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +template <int N> |
| 128 | +struct to_bytes_impl<const char32_t[N]> { |
| 129 | + expected<std::string, std::error_code> operator()(const char32_t *source) const { |
| 130 | + to_bytes_impl<std::u32string> to_bytes; |
| 131 | + return to_bytes(source); |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +template <int N> |
| 136 | +struct to_bytes_impl<char32_t[N]> { |
| 137 | + expected<std::string, std::error_code> operator()(const char32_t *source) const { |
| 138 | + to_bytes_impl<std::u32string> to_bytes; |
| 139 | + return to_bytes(source); |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +template <> |
| 144 | +struct to_bytes_impl<char32_t *> { |
| 145 | + expected<std::string, std::error_code> operator()(const char32_t *source) const { |
| 146 | + to_bytes_impl<std::u32string> to_bytes; |
| 147 | + return to_bytes(source); |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +template <> |
| 152 | +struct to_bytes_impl<const char32_t *> { |
| 153 | + expected<std::string, std::error_code> operator()(const char32_t *source) const { |
| 154 | + to_bytes_impl<std::u32string> to_bytes; |
| 155 | + return to_bytes(source); |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +template <typename Source> |
| 160 | +inline expected<std::string, std::error_code> to_bytes(const Source &source) { |
| 161 | + to_bytes_impl<Source> to_bytes; |
| 162 | + return to_bytes(source); |
| 163 | +} |
| 164 | +} // namespace details |
| 165 | +} // namespace skyr |
| 166 | + |
| 167 | +#endif // SKYR_URL_DETAILS_TRANSLATE_INC |
0 commit comments