gpt4 book ai didi

c++ - C++ 中 Turkish-I 的解决方案

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:30:39 25 4
gpt4 key购买 nike

大家好!

在土耳其语中,字母表中的一个字母具有不同的行为,即 I - 和 i-。在英语中,I 和 i 是大写和小写。在土耳其语中,I 的小写不是 i,而是 ı

因此在土耳其语环境(即 Windows)中,“DOMAIN.COM”和“domain.com”并不相等。由于电子邮件传输和DNS完全是英文的,如果邮件地址中包含大写的I,可能会出现问题。

在 C# 中,我们可以使用 InvariantCultureIgnoreCase 标志来纠正这个问题:

        // Mock
string localDomain = "domain.com";
string mailAddress = "USER@DOMAIN.COM";
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];
}

由于我没有使用 C++ 的经验,这两个示例的 C++ 等价物 是什么?

最佳答案

如果您在 Windows 中编程,您可以将线程的 locale 更改为 en_US 然后使用 _stricmp,或者为 en_US 创建一个语言环境对象,然后使用 _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 );

如果 boost 是您的选择,一个更便携和 C++ 友好的解决方案是使用 boost::locale图书馆

关于c++ - C++ 中 Turkish-I 的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13146241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com