代码之家  ›  专栏  ›  技术社区  ›  brian d foy

Perl的“标准字符串比较顺序”是什么?

  •  19
  • brian d foy  · 技术社区  · 16 年前

    sort sort locale

    Learning Perl Perl_sv_cmp 知道 locale bytes

    /*
    =for apidoc sv_cmp
    
    Compares the strings in two SVs.  Returns -1, 0, or 1 indicating whether the
    string in C<sv1> is less than, equal to, or greater than the string in
    C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
    coerce its args to strings if necessary.  See also C<sv_cmp_locale>.
    
    =cut
    */
    

     pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
    

    pv1

     const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
    

    在我看来,它曾经 pv2 ,他们被迫 char * ,现在只是逐字节比较,因为它们被强制为 void * .事情就是这样吗 memcmp Perl_bytes_to_utf8 没有帮我回答那个问题。

    Unicode Collation Algorithm ?如果是,为什么 Unicode::Collate 排序

    2 回复  |  直到 16 年前
        1
  •  14
  •   hobbs    16 年前

    至于规范化,Perl核心对此一无所知;为了在不同形式之间进行准确的排序和比较,您需要遍历所有字符串 Unicode::Normalize 并将它们全部转换为相同的规范化形式。我无法评论哪种形式最适合任何特定目的,主要是因为我不知道。

    此外,分类和 cmp locale use locale ,8位区域设置和unicode加在一起是灾难的根源,但使用 使用区域设置 、UTF-8区域设置和unicode 应该 工作有益。我不能说我试过。里面有很多信息 perllocale perlunicode

        2
  •  5
  •   Max Lybbert    16 年前

        const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
    

    …看起来曾经 pv1 pv2 ,他们被迫 char * ,现在只是逐字节比较,因为它们被强制为 void * .事情就是这样吗 memcmp

    差不多。主要区别在于 内存比较 strcmp 是:

    1. 一旦看到 NULL '\0' ),Perl允许嵌入标量 空值 s
    2. 内存比较 通常比跑步快一点

    但除此之外,你会得到同样的结果。