1

So I've already found How to capitalize a word in a C++ string? , but I've tried similar code as has been suggested, including what's provided in the examples for 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
  • Compiler: Microsoft Visual Studio 15.8.0
  • Platform: x64
  • Non-default Compilation Options: /std:c++latest
  • BOOST_VERSION: 106700

EDIT #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.

Drako0812
  • 93
  • 2
  • 9
  • 1
    did you build boost with ICU support? It seems `to_title` is supported only for ICU – miradham Aug 17 '18 at 06:09
  • Works for me on Linux, GCC 7, Boost 1.67, ICU – sehe Aug 17 '18 at 15:48
  • @miradham The boost I was using was automatically built by vcpkg, so I have no idea, but that seems likely. – Drako0812 Aug 17 '18 at 20:47
  • It looks like I can install the ICU based versions of boost libraries using vcpkg. Now I just have to wait at least a couple hours, for boost to be completely recompiled again. – Drako0812 Aug 17 '18 at 20:57

2 Answers2

1

vcpkg (https://github.com/Microsoft/vcpkg) by default installs Boost without depending on ICU for the Boost::locale and Boost::regex libraries.

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.

I wish the Boost documentation for those libraries made it clear that you needed ICU to use the functionality that requires it.

Drako0812
  • 93
  • 2
  • 9
0

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.

So in order to use to_title function you have to make sure to use boost locale for ICU

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);
}
miradham
  • 2,285
  • 16
  • 26
  • Thanks for the additional information, I only accepted my answer because it had more to do with my specific use case. – Drako0812 Aug 21 '18 at 23:48