2 #include <samchon/API.hpp> 6 #include <samchon/WeakString.hpp> 56 static auto toMultibyte(
const std::string &source) -> std::string
58 std::wstring &wstr =
toUnicode(source, UTF8);
76 static auto toMultibyte(
const std::wstring &source) -> std::string
79 int len = WideCharToMultiByte(CP_ACP, 0, &source[0], -1, NULL, 0, NULL, NULL);
80 std::string str(len, 0);
81 WideCharToMultiByte(CP_ACP, 0, &source[0], -1, &str[0], len, NULL, NULL);
86 typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
88 locale &loc = locale(
"");
90 codecvt_t
const& codecvt = use_facet<codecvt_t>(loc);
91 mbstate_t state = mbstate_t();
92 vector<char> buf(source.size() * codecvt.max_length());
93 wchar_t const* in_next = source.c_str();
94 char* out_next = &buf[0];
96 codecvt_base::result r =
100 source.c_str(), source.c_str() + source.size(), in_next,
101 &buf[0], &buf[0] + buf.size(), out_next
104 if (r == codecvt_base::error)
105 throw runtime_error(
"can't convert wstring to string");
107 return string(buf.begin(), buf.end());
122 static auto toUTF8(
const std::string &source) -> std::string
124 std::wstring &wstr =
toUnicode(source, MULTIBYTE);
125 std::string &dest =
toUTF8(wstr);
127 if (dest.back() == NULL)
145 static auto toUTF8(
const std::wstring &source) -> std::string
148 int len = WideCharToMultiByte(CP_UTF8, 0, &source[0], -1, NULL, 0, NULL, NULL);
149 std::string str(len, 0);
150 WideCharToMultiByte(CP_UTF8, 0, &source[0], -1, &str[0], len, NULL, NULL);
154 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utf8Converter;
155 return utf8Converter.to_bytes(source);
166 static auto toUnicode(
const std::string &source,
int charset) -> std::wstring
169 if (charset == MULTIBYTE)
171 int nLen = MultiByteToWideChar(CP_ACP, 0, &source[0], (
int)source.size(), NULL, NULL);
172 std::wstring wstr(nLen, 0);
173 MultiByteToWideChar(CP_ACP, 0, &source[0], (
int)source.size(), &wstr[0], nLen);
177 else if (charset == UTF8)
179 int nLen = MultiByteToWideChar(CP_UTF8, 0, &source[0], (
int)source.size(), NULL, NULL);
180 std::wstring wstr(nLen, 0);
181 MultiByteToWideChar(CP_UTF8, 0, &source[0], (
int)source.size(), &wstr[0], nLen);
191 if (charset == MULTIBYTE)
193 locale &loc = locale(
"");
195 typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
196 codecvt_t
const& codecvt = use_facet<codecvt_t>(loc);
197 mbstate_t state = mbstate_t();
198 vector<wchar_t> buf(source.size());
199 char const* in_next = source.c_str();
200 wchar_t* out_next = &buf[0];
202 codecvt_base::result r =
206 source.c_str(), source.c_str() + source.size(), in_next,
207 &buf[0], &buf[0] + buf.size(), out_next
210 if (r == codecvt_base::error)
211 throw runtime_error(
"can't convert string to wstring");
213 return wstring(buf.begin(), buf.end());
215 else if (charset == UTF8)
217 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utf8Converter;
218 wstring &wstr = move(utf8Converter.from_bytes(source));
static auto toMultibyte(const std::string &source) -> std::string
Convert utf-8 to multibyte.
static auto toUTF8(const std::wstring &source) -> std::string
Convert unicode to utf-8.
static auto toMultibyte(const std::wstring &source) -> std::string
Convert unicode to multibyte.
static auto toUTF8(const std::string &source) -> std::string
Convert multibyte to utf-8.
static auto toUnicode(const std::string &source, int charset) -> std::wstring
Convert multibyte or utf-8 to unicode.
A utility class supporting conversion between multiple character-sets.