SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
convert.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_CONVERT_HPP
9 #define SIG_UTIL_CONVERT_HPP
10 
11 #include "../helper/container_traits.hpp"
12 
13 
14 #if SIG_MSVC_ENV
15 #define NOMINMAX
16 #include <windows.h>
17 #pragma warning ( disable : 4996 )
18 #endif
19 
20 #if (__GLIBCXX__ || __GLIBCPP__)
21 #define SIG_USE_GLIBCPP 1
22 #endif
23 
24 #if (SIG_MSVC_ENV && SIG_MSVC_VER != 140) && !(SIG_USE_GLIBCPP)
25 #include <codecvt>
27 #define SIG_ENABLE_CODECVT 1
28 #endif
29 
30 
32 
33 namespace sig
34 {
35 
37 
50 inline auto wstr_to_str(std::wstring const& src)
51  ->std::string //Just<std::string>
52 {
53  size_t mbs_size = src.length() * MB_CUR_MAX + 1;
54  if (mbs_size < 2 || src == L"\0") return std::string();
55  char *mbs = new char[mbs_size];
56 
57 #if SIG_MSVC_ENV && SIG_DEBUG_MODE
58  size_t num;
59  int error = wcstombs_s(&num, mbs, mbs_size, src.c_str(), src.length() * MB_CUR_MAX + 1);
60 #else
61  int error = wcstombs(mbs, src.c_str(), src.length() * MB_CUR_MAX + 1);
62 #endif
63  std::string dest(mbs);
64  delete[] mbs;
65 
66  return error == -1 ? std::string() : dest; //error ? Nothing(dest) : Just<std::string>(std::move(dest));
67 }
68 
70 
86 template <class C,
87  class R = typename impl::container_traits<C>::template rebind<std::string>,
88  typename std::enable_if<std::is_same<typename impl::container_traits<C>::value_type, std::wstring>::value>::type*& = enabler
89 >
90 auto wstr_to_str(C const& src) ->R
91 {
92  R result = impl::container_traits<R>::make(src.size());
93 
94  for (auto const& str : src){
95  std::string r = wstr_to_str(str);
96  if (!r.empty()) impl::container_traits<R>::add_element(result, std::move(r));
97  }
98 
99  return result;
100 }
101 
103 
116 inline auto str_to_wstr(std::string const& src) ->std::wstring //Just<std::wstring>
117 {
118  size_t wcs_size = src.length() + 1;
119  if (wcs_size < 2 || src == "\0") return std::wstring();
120  wchar_t *wcs = new wchar_t[wcs_size];
121 
122 #if SIG_MSVC_ENV && SIG_DEBUG_MODE
123  size_t num;
124  int error = mbstowcs_s(&num, wcs, wcs_size, src.c_str(), src.length() + 1);
125 #else
126  int error = mbstowcs(wcs, src.c_str(), src.length() + 1);
127 #endif
128  std::wstring dest(wcs);
129  delete[] wcs;
130  return error == -1 ? std::wstring() : dest; //error ? Nothing(dest) : Just<std::wstring>(std::move(dest));
131 }
132 
134 
150 template <class C,
151  class R = typename impl::container_traits<C>::template rebind<std::wstring>,
152  typename std::enable_if<std::is_same<typename impl::container_traits<C>::value_type, std::string>::value>::type*& = enabler
153 >
154 auto str_to_wstr(C const& src) ->R
155 {
156  R result = impl::container_traits<R>::make(src.size());
157 
158  for (auto const& str : src){
159  std::wstring r = str_to_wstr(str);
160  if (!r.empty()) impl::container_traits<R>::add_element(result, std::move(r));
161  }
162 
163  return result;
164 }
165 
166 #if SIG_ENABLE_CODECVT
167 
175 inline auto utf8_to_utf16(std::string const& src) ->std::u16string
176 {
177  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
178 
179  return convert.from_bytes(src);
180 }
181 
183 
190 inline auto utf16_to_utf8(std::u16string const& src) ->std::string
191 {
192  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
193 
194  return convert.to_bytes(src);
195 }
196 
198 
205 inline auto utf8_to_utf32(std::string const& src) ->std::u32string
206 {
207  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
208 
209  return utf32conv.from_bytes(src);
210 }
211 
213 
220 inline auto utf32_to_utf8(std::u32string const& src) ->std::string
221 {
222  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf32conv;
223 
224  return utf32conv.to_bytes(src);
225 }
226 #endif
227 
228 #if SIG_MSVC_ENV
229 
231 
238 inline auto sjis_to_utf16(std::string const& src) ->std::u16string //Just<std::u16string>
239 {
240  const int dest_size = ::MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, NULL, 0);
241  BYTE* buf = new BYTE[dest_size * 2 + 2];
242 
243  bool is_succeed = ::MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, reinterpret_cast<LPWSTR>(buf), dest_size) > 0 ? true : false;
244  std::u16string dest(reinterpret_cast<char16_t*>(buf));
245  delete[] buf;
246 
247  return is_succeed ? dest : std::u16string(); //is_succeed ? Just<std::u16string>(std::move(dest)) : Nothing(std::u16string());
248 }
249 
251 
258 inline auto utf16_to_sjis(std::u16string const& src) ->std::string //Just<std::string>
259 {
260  LPCWSTR srcp = reinterpret_cast<LPCWSTR>(src.c_str());
261  const int dest_size = ::WideCharToMultiByte(CP_ACP, 0, srcp, -1, NULL, 0, NULL, NULL);
262  BYTE* buf = new BYTE[dest_size * 2];
263 
264  bool is_succeed = ::WideCharToMultiByte(CP_ACP, 0, srcp, -1, reinterpret_cast<LPSTR>(buf), dest_size, NULL, NULL) > 0 ? true : false;
265  std::string dest(reinterpret_cast<char*>(buf));
266  delete[] buf;
267 
268  return is_succeed ? dest : std::string(); //is_succeed ? Just<std::string>(std::move(dest)) : Nothing(std::string());
269 }
270 #endif
271 
272 #ifdef SIG_ENABLE_CODECVT
273 
281 inline auto sjis_to_utf8(std::string const& src) ->std::string
282 {
283  return utf16_to_utf8(sjis_to_utf16(src));
284 }
285 
287 
294 inline auto utf8_to_sjis(std::string const& src) ->std::string
295 {
296  return utf16_to_sjis(utf8_to_utf16(src));
297 }
298 #endif
299 
300 }
301 #endif
auto utf8_to_sjis(std::string const &src) -> std::string
UTF-8 -> ShiftJIS.
Definition: convert.hpp:294
auto utf32_to_utf8(std::u32string const &src) -> std::string
UTF-32 -> UTF-8.
Definition: convert.hpp:220
auto utf8_to_utf32(std::string const &src) -> std::u32string
UTF-8 -> UTF-32.
Definition: convert.hpp:205
void * enabler
auto utf16_to_sjis(std::u16string const &src) -> std::string
UTF-16 -> ShiftJIS.
Definition: convert.hpp:258
auto sjis_to_utf16(std::string const &src) -> std::u16string
ShiftJIS -> UTF-16.
Definition: convert.hpp:238
auto utf16_to_utf8(std::u16string const &src) -> std::string
UTF-16 -> UTF-8.
Definition: convert.hpp:190
auto sjis_to_utf8(std::string const &src) -> std::string
ShiftJIS -> UTF-8.
Definition: convert.hpp:281
auto utf8_to_utf16(std::string const &src) -> std::u16string
UTF-8 -> UTF-16.
Definition: convert.hpp:175
auto wstr_to_str(std::wstring const &src) -> std::string
ワイド文字 -> マルチバイト文字
Definition: convert.hpp:50
auto str_to_wstr(std::string const &src) -> std::wstring
マルチバイト文字 -> ワイド文字
Definition: convert.hpp:116
Definition: array.hpp:15