Last active
March 22, 2021 13:46
-
-
Save arosh/210bf4ed4e632a6b765d to your computer and use it in GitHub Desktop.
wstring and mbstring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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