Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution for Turkish-I in C++

Hi all!

In Turkish one of the letters of alphabet has a different behaviour, it's I -and i-. In English I and i are upper & lower cases. In Turkish lowercase of I is not i, instead ı.

So in Turkish environment (ie Windows) "DOMAIN.COM" and "domain.com" are not equal. Since email transport & DNS are completely in English, if mail addresses contain uppercase I, there might be a problem.

In C# we may use InvariantCultureIgnoreCase flag to correct the issue:

        // Mock
        string localDomain = "domain.com";
        string mailAddress = "[email protected]";
        string domainOfAddress = mailAddress.Split('@')[1];
        string localInbox = "";

        //
        // Local inbox check

        //Case insensitive method 
        bool ignoreCase = true; // Equal to StringComparison.CurrentCultureIgnoreCase
        if (System.String.Compare(localDomain, domainOfAddress, ignoreCase) == 0) 
        { 
           // This one fails in Turkish environment
           localInbox = mailAddress.Split('@')[0];
        }


        //Culture-safe version
        if (System.String.Compare(localDomain, domainOfAddress, StringComparison.InvariantCultureIgnoreCase) == 0)
        {
            // This one is the correct/universal method
            localInbox = mailAddress.Split('@')[0];
        }

Since I'm not experienced at C++ what would be the C++ equivalents of these two examples?

like image 723
Nime Cloud Avatar asked Oct 30 '12 19:10

Nime Cloud


1 Answers

If you are programming in Windows, you may change locale of your thread to en_US and then use _stricmp, or create a locale object for en_US and then use _stricmp_l:

setlocale( LC_CTYPE, "English" );
assert( _stricmp("DOMAIN", "domain") == 0 );

_locale_t l = _create_locale( LC_CTYPE, "English" );
assert( _stricmp_l("DOMAIN", "domain", l) == 0 );

If boost is an option for you, a more portable and C++ friendly solution is to use boost::locale library

like image 92
BigBoss Avatar answered Oct 13 '22 05:10

BigBoss