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

标签 c++ boost unicode locale

我想从给定的 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/

相关文章:

c++ - 排序函数替换值时出现问题

c++ - 头文件中关于单一类型的各种错误

c++ - 数据对齐 : Reason for restriction on memory address being multiple of data type size

c++ - 对 `boost::log_mt_posix::basic_attribute_set<char>::~basic_attribute_set()' 的 undefined reference

python - 将utf-8格式的字符串转换为unicode : Python

emacs 术语模式中的 Unicode 字符

c++ - 初学者关于编译器错误的问题

c++ - 如何在获得 future 值(value)后重用 boost::promise 对象?

boost - 一张图可以有多个边权重属性映射吗?

C# 无法将类型 'string' 隐式转换为 'System.IO.StreamReader