C ++ ctype для UTF-8 в mingw - PullRequest
       17

C ++ ctype для UTF-8 в mingw

2 голосов
/ 30 августа 2009

В проекте все внутренние строки хранятся в кодировке utf-8. Проект портирован на Linux и Windows. Сейчас нужна функциональность to_lower.

В ОС POSIX я мог использовать std :: ctype_byname ("ru_RU.UTF-8"). Но в g ++ (Debian 4.3.4-1) ctype :: tolower () не распознает русские символы UTF-8 (латинский текст в нижнем регистре отлично).

В Windows стандартная библиотека mingw выдает исключение «std :: runtime_error: locale :: facet :: _ S_create_c_locale name not valid», когда я пытаюсь создать std :: ctype_byname с аргументом «ru_RU.UTF-8».

Как мне реализовать / найти std :: ctype для utf-8 в Windows? Проект уже зависит от libiconv (на нем основан фасет codecvt), но я не вижу очевидного способа реализовать с ним to_lower.

Ответы [ 3 ]

3 голосов
/ 01 сентября 2009

Попробуйте использовать STLport

  Here is a description of how you can use STLport to read/write utf8 files.
utf8 is a way of encoding wide characters. As so, management of encoding in
the C++ Standard library is handle by the codecvt locale facet which is part
of the ctype category. However utf8 only describe how encoding must be
performed, it cannot be used to classify characters so it is not enough info
to know how to generate the whole ctype category facets of a locale
instance.

In C++ it means that the following code will throw an exception to
signal that creation failed:

#include 
// Will throw a std::runtime_error exception.
std::locale loc(".utf8");

For the same reason building a locale with the ctype facets based on
UTF8 is also wrong:

// Will throw a std::runtime_error exception:
std::locale loc(locale::classic(), ".utf8", std::locale::ctype);

The only solution to get a locale instance that will handle utf8 encoding
is to specifically signal that the codecvt facet should be based on utf8
encoding:

// Will succeed if there is necessary platform support.
locale loc(locale::classic(), new codecvt_byname(".utf8"));

  Once you have obtain a locale instance you can inject it in a file stream to
read/write utf8 files:

std::fstream fstr("file.utf8");
fstr.imbue(loc);

You can also access the facet directly to perform utf8 encoding/decoding operations:

typedef std::codecvt codecvt_t;
const codecvt_t& encoding = use_facet(loc);

Notes:

1. The dot ('.') is mandatory in front of utf8. This is a POSIX convention, locale
names have the following format:
language[_country[.encoding]]

Ex: 'fr_FR'
    'french'
    'ru_RU.koi8r'

2. utf8 encoding is only supported for the moment under Windows. The less common
utf7 encoding is also supported. 
2 голосов
/ 03 сентября 2009

Если все, что вам нужно, это to_lower для символов кириллицы, вы можете написать функцию самостоятельно.

АБВГДЕЖ in UTF8  D0 90 D0 91 D0 92 D0 93 D0 94 D0 95 D0 96 0A
абвгдеж in UTF8  D0 B0 D0 B1 D0 B2 D0 B3 D0 B4 D0 B5 D0 B6 0A

Но не забывайте, что UTF8 - это многобайтовая кодировка.

Также вы можете попытаться преобразовать строку из UTF8 в wchar_t (используя libiconv) и использовать специальную функцию Windows для реализации to_lower.

0 голосов
/ 31 августа 2009

Существует некоторая STL (например, от Apache - STDCXX, например), которая поставляется с несколькими локалями. Но в других ситуациях локаль зависит только от системы.

Если вы могли бы использовать имя "ru_RU.UTF-8" в одной операционной системе, это не значит, что другие системы имеют такое же имя для этой локали. Debian и windows, вероятно, имеют другие имена, и по этой причине у вас есть исключение времени выполнения.

Вы должны установить желаемые локали в системе раньше. Или используйте STL, который уже имеет эту локаль.

Мои центы ...

...