How do I write a std::codecvt facet?

前端 未结 2 1030
死守一世寂寞
死守一世寂寞 2020-12-16 15:27

How do I write a std::codecvt facet? I\'d like to write ones that go from UTF-16 to UTF-8, which go from UTF-16 to the systems current code page (windows, so CP_ACP), and to

2条回答
  •  孤城傲影
    2020-12-16 15:57

    I've written one based on iconv. It can be used on windows or on any POSIX OS. (You will need to link with iconv obviously).

    Enjoy

    The answer for the "how to" question is to follow the codecvt reference. I was not able to find any better instructions in the Internet two years ago.

    Important notices

    • theoretically there is no need for such work. codecvt_byname should be enough on any standard supporting platform. But in reality there are some compilers that don't support or badly support this class. There is also a difference in interfaces of codecvt_byname on different compilers.
    • my working example is implemented with state template parameter of codecvt. Always use standard mbstate type there as this is the only way to use your codecvt with standard iostream classes.
    • std::mbstate_t type can't be used as a pointer on 64bit platforms in a cross-platform way.
    • stateless conversions work for short strings, but may fail if you try to convert a data chunk greater that streambuf internal buffer size (UTF is essentially stateful encoding)

提交回复
热议问题