我正在尝试使用该boost::locale
库在我的代码(版本 1.71)中执行字符串的大写和小写转换。
我对“ß”的大写有疑问。为了符合我的代码库中已经存在的单元测试,我希望将字母“ß”大写为“SS”。这应该不是问题,因为据我了解这是预期的行为(https://www.boost.org/doc/libs/1_71_0/libs/locale/doc/html/conversions.html)。
这是此页面上提供的示例的副本以供参考:
Upper GRÜSSEN
Lower grüßen
Title Grüßen
Fold grüssen
但是,当我在代码中使用该方法时,情况并非如此。应用大写方法时,“ß”保持为“ß”。
boost::locale
我很困惑,在库源中找到了以下示例:
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/locale.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <iostream>
#include <ctime>
int main()
{
using namespace boost::locale;
using namespace std;
// Create system default locale
generator gen;
locale loc=gen("");
locale::global(loc);
cout.imbue(loc);
cout<<"Correct case conversion can't be done by simple, character by character conversion"<<endl;
cout<<"because case conversion is context sensitive and not 1-to-1 conversion"<<endl;
cout<<"For example:"<<endl;
cout<<" German grüßen correctly converted to "<<to_upper("grüßen")<<", instead of incorrect "
<<boost::to_upper_copy(std::string("grüßen"))<<endl;
cout<<" where ß is replaced with SS"<<endl;
cout<<" Greek ὈΔΥΣΣΕΎΣ is correctly converted to "<<to_lower("ὈΔΥΣΣΕΎΣ")<<", instead of incorrect "
<<boost::to_lower_copy(std::string("ὈΔΥΣΣΕΎΣ"))<<endl;
cout<<" where Σ is converted to σ or to ς, according to position in the word"<<endl;
cout<<"Such type of conversion just can't be done using std::toupper that work on character base, also std::toupper is "<<endl;
cout<<"not even applicable when working with variable character length like in UTF-8 or UTF-16 limiting the correct "<<endl;
cout<<"behavior to unicode subset BMP or ASCII only"<<endl;
}
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
// boostinspect:noascii
我尝试编译它,这是我得到的结果:
Correct case conversion can't be done by simple, character by character conversion
because case conversion is context sensitive and not 1-to-1 conversion
For example:
German grüßen correctly converted to GRÜßEN, instead of incorrect GRüßEN
where ß is replaced with SS
Greek ὈΔΥΣΣΕΎΣ is correctly converted to ὀδυσσεύσ, instead of incorrect ὈΔΥΣΣΕΎΣ
where Σ is converted to σ or to ς, according to position in the word
Such type of conversion just can't be done using std::toupper that work on character base, also std::toupper is
not even applicable when working with variable character length like in UTF-8 or UTF-16 limiting the correct
behavior to unicode subset BMP or ASCII only
强调部分:
German grüßen correctly converted to GRÜßEN, instead of incorrect GRüßEN
where ß is replaced with SS
我真的不明白这句话是怎么回事。实际的预期行为是什么?