如何将 pugi:char_t* 转换为字符串?

发布于 2024-11-30 23:09:59 字数 539 浏览 2 评论 0原文

可能的重复:
如何将 pugi::char_t* 转换为字符串

我该如何转换pugi:char_t* 类型转换为 wchar_t 字符串?

我想将 child_value() 的结果与某个 utf8 字符串进行比较,如果没有这种转换,我无法做到这一点。

for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
    {
        wchar_t* wordValue = it->child("WORDVALUE").child_value();
    }

此赋值返回错误,因为右侧是 pugi::char_t* 左侧是 whar_t*

谢谢

Possible Duplicate:
How to convert pugi::char_t* to string

how can I convert pugi:char_t* type to wchar_t string?

I want to compare the result of child_value() to some utf8 string and without that convertion I cant do that.

for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
    {
        wchar_t* wordValue = it->child("WORDVALUE").child_value();
    }

this assignment return error because the right side is pugi::char_t* and the left side is whar_t*

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-12-07 23:09:59

这是一个想法。粗略的版本涉及修改库,这可能会在将来破坏一些东西,但让我们开始吧:

我们已经有了这两个函数:

std::string as_utf8(const wchar_t* str);
std::wstring as_wide(const char* str);

我们还知道 pugi::char_tcharwchar_t。因此,为了在 pugi::char_t 上调用转换函数,我们需要的是一个重载,您可以将其添加到标头中:

std::string as_utf8(const char* str) { return str; }
std::wstring as_wide(const wchar_t* str) { return str; }

现在您可以这样写:

pugi::char_t * mystr = get_magic_string();
std::string mystrU8 = as_utf8(mystr);

这将在两种编译器设置中工作,这要归功于重载,并且如果 pugi::char_t 已经是 char,那么这将被完全优化(当然假设编码已经是 UTF-8,而不是任何其他 8 位编码!)。

(您还应该提供采用 std::stringstd::wstring 参数的重载,以支持 pugi::string_t 的相同魔力.)

如果您担心污染库,请编写自己的包装器:

std::string my_utf8(const char * str) { return str; }
std::string my_utf8(const wchar_t * str) { return as_utf8(str); }

Here's an idea. The crude version involves modifying the library, which may break stuff in the future, but let's begin with that:

We already have these two functions:

std::string as_utf8(const wchar_t* str);
std::wstring as_wide(const char* str);

We also know that pugi::char_t is either char or wchar_t. So all we need in order to call the conversion function on pugi::char_t is an overload, which you may add to your headers:

std::string as_utf8(const char* str) { return str; }
std::wstring as_wide(const wchar_t* str) { return str; }

Now you can write this:

pugi::char_t * mystr = get_magic_string();
std::string mystrU8 = as_utf8(mystr);

This will work in both compiler settings thanks to the overload, and if pugi::char_t is already char, then this will be optimized out entirely (assuming of course that the encoding was already UTF-8, and not any other 8-bit encoding!).

(You should also provide the overloads that take std::string and std::wstring arguments to support the same magic for pugi::string_t.)

If you worry about polluting the library, write your own wrapper:

std::string my_utf8(const char * str) { return str; }
std::string my_utf8(const wchar_t * str) { return as_utf8(str); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文