Skip to content

Instantly share code, notes, and snippets.

@arosh
Last active March 22, 2021 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arosh/210bf4ed4e632a6b765d to your computer and use it in GitHub Desktop.
Save arosh/210bf4ed4e632a6b765d to your computer and use it in GitHub Desktop.
wstring and mbstring
// http://simd.jugem.jp/?eid=122
// このあたりの説明が丁寧でおすすめ(仕様に準拠しているかどうかは別として)
wstring to_wstring(const string & mbs) {
// http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/mbstowcs.3.html
size_t ws_len = mbstowcs(NULL, mbs.c_str(), 0);
wstring ws(ws_len, '\0');
mbstowcs(&ws[0], mbs.c_str(), ws.size());
return ws;
}
wstring to_wstring(const char & mbc) {
string mbs;
mbs += mbc;
return to_wstring(mbs);
}
string to_mbstring(const wstring & ws) {
// http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/wcstombs.3.html
size_t mb_len = wcstombs(NULL, ws.c_str(), 0);
string mbs(mb_len, '\0');
wcstombs(&mbs[0], ws.c_str(), mbs.size());
return mbs;
}
string to_mbstring(const wchar_t & wc) {
wstring ws;
ws += wc;
return to_mbstring(ws);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment