Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowercase of Unicode character

I am working on a C++ project that need to get data from unicode text. I have a problem that I can't lower some unicode character. I use wchar_t to store unicode character which read from a unicode file. After that, I use _wcslwr to lower a wchar_t string. There are many case still not lower such as:

Đ Â Ă Ê Ô Ơ Ư Ấ Ắ Ế Ố Ớ Ứ Ầ Ằ Ề Ồ Ờ Ừ Ậ Ặ Ệ Ộ Ợ Ự

which lower case is:

đ â ă ê ô ơ ư ấ ắ ế ố ớ ứ ầ ằ ề ồ ờ ừ ậ ặ ệ ộ ợ ự 

I have try tolower and it is still not working.

like image 576
Nguyên Phan Avatar asked Dec 23 '15 10:12

Nguyên Phan


People also ask

What is lowercase character example?

Sometimes abbreviated as LC, lowercase is a typeface of small characters. For example, a, b, and c is lowercase and A, B, and C is uppercase.

What is a lower case letter D in Unicode?

Latin Small Letter D with Stroke. U+0113. ē Latin Small Letter E with Macron. U+0115.

How do you type in lowercase?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.


1 Answers

If you call only tolower, it will call std::tolower from header clocale which will call the tolower for ansi character only.

The correct signature should be:

template< class charT >
charT tolower( charT ch, const locale& loc );

Here below is 2 versions which works well:

#include <iostream>
#include <cwctype>
#include <clocale>
#include <algorithm>
#include <locale>

int main() {
    std::setlocale(LC_ALL, "");
    std::wstring data = L"Đ Â Ă Ê Ô Ơ Ư Ấ Ắ Ế Ố Ớ Ứ Ầ Ằ Ề Ồ Ờ Ừ Ậ Ặ Ệ Ộ Ợ Ự";
    std::wcout << data << std::endl;

    // C std::towlower
    for(auto c: data)
    {
        std::wcout << static_cast<wchar_t>(std::towlower(c));
    }
    std::wcout << std::endl;

    // C++ std::tolower(charT, std::locale)
    std::locale loc("");
    for(auto c: data)
    {
        // This is recommended
        std::wcout << std::tolower(c, loc);
    }
    std::wcout << std::endl;
    return 0;
}

Reference:

  • towlower

  • tolower

like image 105
Danh Avatar answered Sep 28 '22 07:09

Danh