gpt4 book ai didi

c - 为什么附加公共(public)后缀会反转 en_US 语言环境中的整理顺序?

转载 作者:太空狗 更新时间:2023-10-29 15:01:25 26 4
gpt4 key购买 nike

下面的代码

#!/usr/bin/perl

use strict;
use warnings;

my $s1 = 'aaa2000@yahoo.com';
my $s2 = 'aaa_2000@yahoo.com';
my $s3 = 'aaa2000';
my $s4 = 'aaa_2000';

no locale;

print "\nNO Locale:\n\n";

if ($s1 gt $s2) {print "$s1 is > $s2\n";}
if ($s1 lt $s2) {print "$s1 is < $s2\n";}
if ($s1 eq $s2) {print "$s1 is = $s2\n";}

if ($s3 gt $s4) {print "$s3 is > $s4\n";}
if ($s3 lt $s4) {print "$s3 is < $s4\n";}
if ($s3 eq $s4) {print "$s3 is = $s4\n";}

use locale;

print "\nWith 'use locale;':\n\n";

if ($s1 gt $s2) {print "$s1 is > $s2\n";}
if ($s1 lt $s2) {print "$s1 is < $s2\n";}
if ($s1 eq $s2) {print "$s1 is = $s2\n";}

if ($s3 gt $s4) {print "$s3 is > $s4\n";}
if ($s3 lt $s4) {print "$s3 is < $s4\n";}
if ($s3 eq $s4) {print "$s3 is = $s4\n";}

打印出来

NO Locale:

aaa2000@yahoo.com is < aaa_2000@yahoo.com
aaa2000 is < aaa_2000

With 'use locale;':

aaa2000@yahoo.com is > aaa_2000@yahoo.com
aaa2000 is < aaa_2000

我无法真正理解:在同一时间,在使用语言环境下,有一个 AND a@yahoo.com > b@yahoo.com ? !!

我是否遗漏了或多或少明显的东西,或者这是一个错误?其他人能否确认看到相同的行为?

Locale is $ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

提前致谢。

最佳答案

启用语言环境后,整理会分多次进行。每个角色都有四个权重,在连续的传递中进行比较。 @_ 符号与大多数标点符号一样,没有主要、次要或第三次重量,因此它们仅在第四遍中发挥作用。所以,对于你的例子

aaa2000@yahoo.com > aaa_2000@yahoo.com

第一关,真的是比较

aaa2000yahoocom = aaa2000yahoocom

然后在第四遍(第二遍和第三遍没有区别因素)

@. > _@.

因为 @ 在这个语言环境中恰好大于 _。 (这只是语言环境定义做出的选择,大概基于某些 ISO 标准或其他标准。)

您可以查看其实现细节。启用语言环境的比较最终在 C 库中实现为 strxfrm(A) cmp strxfrm(B)。运行这个程序:

use POSIX;

my $s1 = 'aaa2000@yahoo.com';
my $s2 = 'aaa_2000@yahoo.com';

foreach ($s1, $s2) {
printf "%s =>\t%v02x\n", $_, POSIX::strxfrm($_);
}

我得到:

aaa2000@yahoo.com =>    0c.0c.0c.04.02.02.02.24.0c.13.1a.1a.0e.1a.18.01.08.08.08.08.08.08.08.08.08.08.08.08.08.08.08.01.02.02.02.02.02.02.02.02.02.02.02.02.02.02.02.01.08.5d.06.44
# explanation: a a a 2 0 0 0 y a h o o c o m DIV secondary weights ... DIV tertiary weights ... DIV @ .
aaa_2000@yahoo.com => 0c.0c.0c.04.02.02.02.24.0c.13.1a.1a.0e.1a.18.01.08.08.08.08.08.08.08.08.08.08.08.08.08.08.08.01.02.02.02.02.02.02.02.02.02.02.02.02.02.02.02.01.04.36.05.5d.06.44
# explanation: a a a 2 0 0 0 y a h o o c o m DIV secondary weights ... DIV tertiary weights ... DIV _ @ .

这些数字的派生方式是一个实现细节;他们只需要出来使得字节比较产生所需的最终结果。但这个概念在所有支持区域设置的排序的现代编程环境中都是相同的。

关于c - 为什么附加公共(public)后缀会反转 en_US 语言环境中的整理顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3979576/

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