本文介绍了如何写一个std :: codecvt facet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何写一个std :: codecvt facet?我想写一个从UTF-16到UTF-8的,从UTF-16到系统当前代码页(windows,所以CP_ACP),以及到系统的OEM代码页(windows,所以CP_OEM)。

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 the system's OEM codepage (windows, so CP_OEM).

跨平台是首选,但Windows上的MSVC也很好。

Cross-platform is preferred, but MSVC on Windows is fine too. Are there any kinds of tutorials or anything of that nature on how to correctly use this class?

推荐答案

我写了一个基于这个类的教程,在iconv。它可以在Windows或任何POSIX操作系统上使用。
(显然你需要链接到iconv)。

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).

如何到问题是遵循。

重要通知


  • 理论上没有必要这样的工作。 应该足以支持任何标准支持平台。但在现实中,有一些编译器不支持或不够支持这个类。
    在不同的编译器上codecvt_byname的接口也有区别。

  • 我的工作示例是使用codecvt的状态模板参数实现的。始终使用标准mbstate类型,因为这是使用标准iostream类的codecvt的唯一方法。

  • std :: mbstate_t类型不能用作64位平台上的指针

  • 无状态转换适用于短字符串,但如果您尝试转换大于streambuf内部缓冲区大小(UTF本质上是有状态编码)的数据块,可能会失败

  • 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)

这篇关于如何写一个std :: codecvt facet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 22:40