字符串大小写转换的后续操作

发布于 2024-12-12 10:16:05 字数 2357 浏览 1 评论 0原文

在消化完这篇文章后,我将其知识收集到一个独立的 C++ 测试程序中(使用带有标志 -std=gnu++0x 的 GCC-4.6 进行测试,其中包括其中描述的所有替代方法。它经过测试可以处理英文字母。然而,当我输入非 ASCII 大写字母(例如瑞典字母 ÅÄÖ)时,它们都不会改变大小写。为什么?我检查过源代码以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;
}

After having digested this post I collected its knowledge into a self-contained C++ test program (tested with GCC-4.6 with flag -std=gnu++0x) that includes all the alternative methods described in it. It is tested to work on english letters. However, none of them changes the case, when I input non-ASCII capital letters, such as swedish letters ÅÄÖ. Why? I've checked that the source code is saved in 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文