1

在消化了这篇文章后,我将它的知识收集到了一个独立的 C++ 测试程序中(使用带有标志的 GCC-4.6 进行测试-std=gnu++0x),其中包括其中描述的所有替代方法。它经过测试可以处理英文字母。但是,当我输入非 ASCII大写字母(例如 swedish letters )时,它们都没有改变大小写ÅÄÖ。为什么?我检查了源代码是否保存在 UTF-8 中。

/*!
 * \file t_locale.cpp
 * \brief Three Ways of doing case-conversions.
 * \see https://stackoverflow.com/questions/313970/stl-string-to-lower-case
 */

#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <locale>
#include <iomanip>
#include <cassert>
#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>

int main(int argc, const char * argv[], const char * envp[])
{
    using std::cout;
    using std::endl;
    using std::string;

    string a = "ÅÄÖ";
    std::locale loc("sv_SE.UTF-8");

    auto do_assert = true;

    cout << "original: " << a << endl;

    // C++
    {
        auto b = a;
        std::transform(a.begin(), a.end(), b.begin(), 
                       std::bind2nd(std::ptr_fun(&std::tolower<char>), loc));
        cout << "tolower: " << b << endl;
        if (do_assert) assert(a != b);
        string c(b);
        std::transform(b.begin(), b.end(), c.begin(), 
                       std::bind2nd(std::ptr_fun(&std::toupper<char>), loc));
        cout << "back: " << c << endl;
        if (do_assert) assert(a == c);
    }

    // Boost Alternative A
    {
        auto b = a;
        std::transform(a.begin(), a.end(), b.begin(), 
                       boost::bind(std::tolower<char>, _1, loc));
        cout << "tolower: " << b << endl;
        if (do_assert) assert(a != b);
        string c(b);
        std::transform(b.begin(), b.end(), c.begin(), 
                       boost::bind(std::toupper<char>, _1, loc));
        cout << "back: " << c << endl;
        if (do_assert) assert(a == c);
    }

    // Boost Alternative b
    {
        auto b = boost::to_lower_copy(a); // locale safe
        cout << "tolower: " << b << endl;
        if (do_assert) assert(a != b);
        auto c = boost::to_upper_copy(b); // locale safe
        cout << "back: " << c << endl;
        if (do_assert) assert(a == c);
    }

    return 0;
}
4

0 回答 0