tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

c++: how to print UTF-8 characters in locale?



Hi!

For 54411 (thanks again christos for fixing that one) I wrote a short
test program to print the thousands separator for a couple of locales.

Most work, but one of them is the French one and that one doesn't.

I got feedback on my first version of the program that the thousands
separator does not need to be a valid char for writing to a cstream
(and I'm not sure I believe that since it UTF-8 strings are valid C
strings), but even when I change it to a wide stream, it doesn't work.

The current version is this:

#include <iostream>
#include <locale>
using namespace std;

void print_thousands_separator(const char *localestring)
{
    std::locale loc = std::locale(localestring);
    auto thou_sep = std::use_facet<std::numpunct<wchar_t> >(loc).thousands_sep();
    wcout << "Thousands separator for " << localestring << " is '" << thou_sep << "'" << endl;
    return;
}

int main() {
    print_thousands_separator("en_US");
    print_thousands_separator("de_DE.UTF-8");
    print_thousands_separator("fr_FR.UTF-8");
}

and it prints (both with g++ and clang++ from 9.99.2/20190802):

Thousands separator for en_US is ','
Thousands separator for de_DE.UTF-8 is '.'
Thousands separator for fr_FR.UTF-8 is '%

Where '%' is my shell's idea of telling me the output ended there
without a newline character.

The same program as C programs prints fine:

#include <langinfo.h>
#include <locale.h>
#include <stdio.h>

void locale(const char *locale) {
        setlocale(LC_ALL, locale);
        printf("thousands separator in %s: '%s'\n", locale, nl_langinfo(THOUSEP));
}

int main() {
        locale("C");
        locale("de_DE.UTF-8");
        locale("fr_FR.UTF-8");
}

gives:

thousands separator in C: ''
thousands separator in de_DE.UTF-8: '.'
thousands separator in fr_FR.UTF-8: ' '

where the space is a 0xC2A0 non-breaking space.

So, what is the proper C++ version of this C program?

Thanks,
 Thomas


Home | Main Index | Thread Index | Old Index