gpt4 book ai didi

c++ - 使用 Boost.Locale 库检索代码点

转载 作者:太空狗 更新时间:2023-10-29 23:14:09 46 4
gpt4 key购买 nike

我想从给定的 Unicode 字符串中检索 code points 的列表组成字符串。为此,我从 Boost 的 character iteration example 中复制了以下示例:

#include <boost/locale.hpp>

using namespace boost::locale::boundary;

int main()
{
boost::locale::generator gen;
std::string text = "To be or not to be";

// Create mapping of text for token iterator using global locale.
ssegment_index map(character, text.begin(), text.end(), gen("en_US.UTF-8"));

// Print all "words" -- chunks of word boundary
for (ssegment_index::iterator it = map.begin(), e = map.end(); it != e; ++it) {
std::cout <<"\""<< * it << "\", ";
}
std::cout << std::endl;

return 0;
}

它返回像这样的字符(根据 Boost 的文档,这些字符与代码点不同):

"T", "o", " ", "b", "e", " ", "o", "r", " ", "n", "o", "t", " ", "t", "o", " ", "b", "e",

我在 boost::locale::util::base_converter class 中读到过使用 to_unicode 函数您可以检索给定字符串的代码点。但我不确定如何。我尝试了以下代码,但没有帮助:

for (ssegment_index::iterator it = map.begin(), e = map.end(); it != e; ++it) {
std::cout << "\"" << * it << "\", ";
boost::locale::util::base_converter encoder_decoder;
virtual uint32_t test1 = encoder_decoder.to_unicode(it->begin(), it->end() );
}

它返回“类型不匹配”错误。我认为 to_unicode() 函数的参数必须有所不同

我正在考虑仅使用 Boost 来检索 code points 而不是像 here 这样的现有解决方案或 here因为 Boost 提供了许多有用的函数来识别各种语言中的换行符分词符

最佳答案

要获取代码点,您可以使用 boost::u8_to_u32_iterator。这是因为 UTF-32 字符等于其代码点。

#include <boost/regex/pending/unicode_iterator.hpp>
#include <string>
#include <iostream>

void printCodepoints(std::string input) {
for(boost::u8_to_u32_iterator<std::string::iterator> it(input.begin()), end(input.end()); it!=end; ++it)
std::cout <<"\""<< * it << "\", ";
}

int main() {
printCodepoints("Hello World!");
return 0;
}

关于c++ - 使用 Boost.Locale 库检索代码点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477192/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com