0

次の UTF-8 文字列のペアを、Boost::locale (任意のバックエンド) を使用して「同一」レベル (アクセントは適切) で比較し、UTF-8 ロケール (de_DE.utf-8 を試しました) を Debian Testing (boost 1.49) で比較すると、 )、私には意味をなさない結果が得られます。

"Muller" は (予想どおり) "Müller" よりも小さいと見なされますが、名前だけでは結果が異なるにもかかわらず、"Muller 2" は "Müller 1" よりも大きいと見なされます。

私のコード、基礎となるライブラリ、または私の期待にバグがありますか?

#include <locale.h>

#include <boost/locale.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string/join.hpp>
#include <iostream>

int main(int argc, char **argv)
{
    setlocale(LC_ALL, "");

    std::cout << "backends: " <<
        boost::join(boost::locale::localization_backend_manager::global().get_all_backends(),
                    ", ") << std::endl;
    boost::locale::localization_backend_manager::global().select(argc > 2 ? argv[2] : "icu");
    std::locale loc = boost::locale::generator()(argc > 1 ? argv[1] : "de_DE.UTF-8");

    typedef boost::tuple<std::string, std::string> string_pair_t;
    std::vector<string_pair_t> pairs =
        boost::assign::tuple_list_of("Muller", "Müller")
        ("Muller 2", "Müller 1")
        ("Muller B", "Müller A");
    BOOST_FOREACH (const string_pair_t &pair, pairs) {
        const std::string &a = boost::get<0>(pair),
            &b = boost::get<1>(pair);
        int cmp = std::use_facet<boost::locale::collator<char> >(loc).
            compare(boost::locale::collator_base::identical, a, b);
        std::cout <<
            a << " and " << b <<
            " are " <<
            (cmp == 0 ? "identical" : "different") <<
            " (" <<
            (cmp < 0 ? '<' :
                   cmp > 0 ? '>' : '=') <<
            ")" << std::endl;
    }

    return 0;
}

私のシステムでの出力:

$ /tmp/mueller de_DE.utf-8 icu
backends: icu, posix, std
Muller and Müller are different (<)
Muller 2 and Müller 1 are different (>)
Muller B and Müller A are different (>)
4

1 に答える 1

0

予期しない結果は、Unicode 照合アルゴリズムがどのように機能するかを理解していないことに起因します。常に最下位レベルから開始し、そのレベルで文字列が異なると見なされた場合、その結果を使用します。より高いレベルを許可すると、より低いレベルでは文字列間の関連する違いが見つからなかった場合にのみ、異なる結果につながります。

于 2012-08-29T12:45:41.903 に答える