我想从给定的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来检索代码点,而不是使用诸如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;
}