How do I get STL std::string to work with unicode on windows?

前端 未结 9 2076
猫巷女王i
猫巷女王i 2021-02-04 13:29

At my company we have a cross platform(Linux & Windows) library that contains our own extension of the STL std::string, this class provides all sort of functionality on top

相关标签:
9条回答
  • 2021-02-04 14:10

    It really platform dependant, Unicode is headache. Depends on which compiler you use. For older ones from MS (VS2010 or older), you would need use API described in MSDN

    for VS2015

    std::string _old = u8"D:\\Folder\\This \xe2\x80\x93 by ABC.txt"s;
    

    according to their docs. I can't check that one.

    for mingw, gcc, etc.

    std::string _old = u8"D:\\Folder\\This \xe2\x80\x93 by ABC.txt";
    std::cout << _old.data();
    

    output contains proper file name...

    0 讨论(0)
  • 2021-02-04 14:11

    In the Windows API and C runtime library, char* parameters are interpreted as being encoded in the "ANSI" code page. The problem is that UTF-8 isn't supported as an ANSI code page, which I find incredibly annoying.

    I'm in a similar situation, being in the middle of porting software from Windows to Linux while also making it Unicode-aware. The approach we've taken for this is:

    • Use UTF-8 as the default encoding for strings.
    • In Windows-specific code, always call the "W" version of functions, converting string arguments between UTF-8 and UTF-16 as necessary.

    This is also the approach Poco has taken.

    0 讨论(0)
  • 2021-02-04 14:14

    Putting UTF-8 code points into an std::string should be fine regardless of platform. The problem on Windows is that almost nothing else expects or works with UTF-8 -- it expects and works with UTF-16 instead. You can switch to an std::wstring which will store UTF-16 (at least on most Windows compilers) or you can write other routines that will accept UTF-8 (probably by converting to UTF-16, and then passing through to the OS).

    0 讨论(0)
提交回复
热议问题