简体   繁体   English

为什么 boost::locale::to_title 没有返回预期的输出?

[英]Why is boost::locale::to_title not returning the expected output?

So I've already found How to capitalize a word in a C++ string?所以我已经找到了如何在 C++ 字符串中大写一个单词? , but I've tried similar code as has been suggested, including what's provided in the examples for Boost::locale. ,但我已经尝试过与建议类似的代码,包括 Boost::locale 示例中提供的内容。 I'll also include what my code is currently and what the expected and real output are.我还将包括我的代码当前是什么以及预期和实际输出是什么。 So I'm trying to understand why I'm not getting the expected output.所以我试图理解为什么我没有得到预期的输出。

Code代码

#include <iostream>
#include <string>
#include <boost/locale.hpp>
#include <boost/algorithm/string/case_conv.hpp>

int main() {
    using namespace std;
    using namespace boost::locale;

    generator gen;
    auto loc = gen("");
    locale::global(loc);
    cout.imbue(loc);

    ios_base::sync_with_stdio(false);

    cout << to_upper("hello!") << " " << boost::to_upper_copy("hello!"s) << endl;
    cout << to_lower("HELLO!") << " " << boost::to_lower_copy("HELLO!"s) << endl;
    cout << to_title("hELLO!") << endl;
    cout << fold_case("HELLO!") << endl;

    return 0;
}

Expected Output预期产出

HELLO! HELLO!
hello! hello!
Hello!
hello!

Real Output实际输出

HELLO! HELLO!
hello! hello!
hELLO!
hello!

Additional Information附加信息

  • OS: Windows 10 Home 64-bit操作系统:Windows 10 家庭版 64 位
  • Compiler: Microsoft Visual Studio 15.8.0编译器:Microsoft Visual Studio 15.8.0
  • Platform: x64平台:x64
  • Non-default Compilation Options: /std:c++latest非默认编译选项: /std:c++latest
  • BOOST_VERSION: 106700 BOOST_VERSION:106700

EDIT #1编辑#1

It seems that the Boost that is installed by vcpkg doesn't get compiled with ICU, which is apparently required for boost::locale::to_title to function correctly.似乎 vcpkg 安装的 Boost 没有用 ICU 编译,这显然是boost::locale::to_title正常运行所必需的。

vcpkg ( https://github.com/Microsoft/vcpkg ) by default installs Boost without depending on ICU for the Boost::locale and Boost::regex libraries. vcpkg ( https://github.com/Microsoft/vcpkg ) 默认情况下会安装 Boost,而不依赖于 Boost::locale 和 Boost::regex 库的 ICU。

So, instead of installing those like this:所以,而不是像这样安装那些:

vcpkg install boost-locale:x64-windows boost-regex:x64-windows

I had to do the following:我必须执行以下操作:

vcpkg install boost-locale[icu]:x64-windows boost-regex[icu]:x64-windows

This automatically fetches and builds the ICU library, and (since I had already installed Boost without ICU) it automatically rebuilt all of the Boost libraries.这会自动获取并构建 ICU 库,并且(因为我已经在没有 ICU 的情况下安装了 Boost)它会自动重建所有Boost 库。

I wish the Boost documentation for those libraries made it clear that you needed ICU to use the functionality that requires it.我希望这些库的 Boost 文档清楚地表明您需要 ICU 来使用需要它的功能。

title_case is handled only for ICU as per source code of boost locale , while for other platforms like for ex win32 it returns input value as it is. title_case仅根据boost locale 的源代码为ICU处理,而对于其他平台,如 ex win32,它按原样返回输入值。

So in order to use to_title function you have to make sure to use boost locale for ICU因此,为了使用to_title函数,您必须确保对 ICU 使用 boost 语言环境

virtual string_type convert(converter_base::conversion_type how,char_type const *begin,char_type const *end,int flags = 0) const 
{
    icu_std_converter<char_type> cvt(encoding_);
    icu::UnicodeString str=cvt.icu(begin,end);
    switch(how) {
        ...
        case converter_base::title_case:
            str.toTitle(0,locale_);
            break;
        case converter_base::case_folding:
            str.foldCase();
            break;
        default:
            ;
    }
    return cvt.std(str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM