Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare wide characters "H2Oリテイリング" and "H2Oリテイリング"

I am trying to compare some strings which are similar, for example:

  1. The comparison of strings "H2Oリテイリング" and "H2Oリテイリング" should return true

  2. The comparison of strings "H2Oリテイリング" and "H2Oリテイリング" should return true

I will get these strings by TCHAR array. Is there any way to compare these type of strings?

I tried by compare using character by character, but it doesn't work.

#define MAX_STRING 256
bool IsStringsEqual(TCHAR* str1, TCHAR* str2)
{
    if (_tcscmp(str1, str2) == 0)
        return true;
    else
        return false;   
}

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR original [MAX_STRING];
    ZeroMemory(original, sizeof(TCHAR) * MAX_STRING);
    _stprintf_s(original, TEXT("%s"), _T("H2Oリテイリング㈱"));

    TCHAR str1[MAX_STRING];
    ZeroMemory(str1, sizeof(TCHAR) * MAX_STRING);
    _stprintf_s(str1, TEXT("%s"), _T("H2Oリテイリング㈱"));
    if (IsStringsEqual(original,str1))
        wcout << endl << " Equal";

    TCHAR str2[MAX_STRING];
    ZeroMemory(str2, sizeof(TCHAR) * MAX_STRING);
    _stprintf_s(str2, TEXT("%s"), _T("H2Oリテイリング㈱"));
    if (IsStringsEqual(original, str2))
        wcout << endl << " Equal";

    TCHAR str3[MAX_STRING];
    ZeroMemory(str3, sizeof(TCHAR) * MAX_STRING);
    _stprintf_s(str3, TEXT("%s"), _T("H2Oリテイリング㈱"));
    if (IsStringsEqual(original, str3))
        wcout <<endl<< " Equal";

    return 0;
}

I'm expecting it to print Equal for all of the above comparisons.

like image 361
vagdevi pedalenka Avatar asked May 10 '19 04:05

vagdevi pedalenka


1 Answers

It looks like what you're looking for is the Unicode Compatibility normalized forms. You can achieve it by using NormalizeString prior to the comparison (the code doesn't handle errors, and strings that don't fit):

bool IsStringsEqual(wchar_t* str1, wchar_t* str2)
{
    wchar_t buf1[MAX_STRING], buf2[MAX_STRING];
    NormalizeString(NormalizationKD, str1, -1, buf1, MAX_STRING);
    NormalizeString(NormalizationKD, str2, -1, buf2, MAX_STRING);
    return wcscmp(buf1, buf2) == 0;
}
like image 134
Yakov Galka Avatar answered Nov 15 '22 14:11

Yakov Galka