【问题标题】:Why is boost::locale::to_title not returning the expected output?为什么 boost::locale::to_title 没有返回预期的输出?
【发布时间】:2019-01-23 21:33:39
【问题描述】:

所以我已经找到了 How to capitalize a word in a C++ string? ,但我尝试了建议的类似代码,包括 Boost::locale 示例中提供的代码。我还将包括我的代码当前是什么以及预期和实际输出是什么。所以我试图理解为什么我没有得到预期的输出。

代码

#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;
}

预期输出

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

实际输出

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

其他信息

  • 操作系统:Windows 10 家庭版 64 位
  • 编译器:Microsoft Visual Studio 15.8.0
  • 平台:x64
  • 非默认编译选项:/std:c++latest
  • BOOST_VERSION:106700

编辑#1

vcpkg 安装的 Boost 好像没有编译 与 ICU,这显然是 boost::locale::to_title 所必需的 功能正常。

【问题讨论】:

  • 您是否在 ICU 支持下建立了提升?似乎 to_title 仅支持 ICU
  • 在 Linux、GCC 7、Boost 1.67、ICU 上为我工作
  • @miradham 我使用的 boost 是由 vcpkg 自动构建的,所以我不知道,但这似乎很可能。
  • 看起来我可以使用 vcpkg 安装基于 ICU 的 boost 库版本。现在我只需要等待至少几个小时,才能再次完全重新编译 boost。

标签: c++ string boost title-case boost-locale


【解决方案1】:

vcpkg (https://github.com/Microsoft/vcpkg) 默认为 Boost::locale 和 Boost::regex 库安装 Boost,而不依赖 ICU。

所以,不要像这样安装:

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

我必须做到以下几点:

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

这会自动获取并构建 ICU 库,并且(因为我已经安装了没有 ICU 的 Boost)它会自动重建 所有 Boost 库。

我希望这些库的 Boost 文档清楚地表明您需要 ICU 才能使用需要它的功能。

【讨论】:

    【解决方案2】:

    title_case 仅根据boost locale 的源代码处理ICU,而对于其他平台,例如前win32,它按原样返回输入值。

    因此,为了使用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);
    }
    

    【讨论】:

    • 感谢您提供更多信息,我只接受了我的回答,因为它与我的特定用例有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2012-10-19
    • 1970-01-01
    • 2023-03-08
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多