sorted as raw strings, not alphanumerics 的个人理解?

本文介绍了Bencode编码中字典类型的编码规则及其特殊排序方式。重点解释了以原始字符串形式进行排序的方法,并通过示例展示了如何理解这种排序机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Dictionaries are encoded as a 'd' followed by a list of alternating keys and their corresponding values followed by an 'e'. For example, d3:cow3:moo4:spam4:eggse corresponds to {'cow': 'moo', 'spam': 'eggs'} and d4:spaml1:a1:bee corresponds to {'spam': ['a', 'b']}. Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics).
  •  

    sorted as raw strings, not alphanumerics: 可以翻译为:以原始字符串来排序,而不是按单个字符或数字来进行排序。

     

    个人查了一下资料,发现 raw string : 是python语言的特性,说明

    A   feature   of   PythonLanguage.   A   rawstring   is   a   string   literal   (prefixed   with   an   r)   in   which   the   normal   escaping   rules   have   been   suspended   so   that   everything   is   a   literal.  
       
      For   example:  
       
      print   r"//*/*//**"   actually   prints   "//*/*//**"  
       
      This   is   incredibly   useful   for   writing   readable   regular   expressions   or   any   string   literals   that   will   be   processed   by   other   escaping   systems   such   as   SQL   parsers.

     

    To   work   around   the   backslash   plague,   you   can   use   what   is   called   a   raw   string,   by   prefixing   the   '...'   with   the   letter   r.   This   tells   Python   that   nothing   in   this   string   should   be   escaped;   '/t'   is   a   tab   character,   but   r'/t'   is   really   the   backslash   character   /   followed   by   the   letter   t.   I   recommend   always   using   raw   strings   when   dealing   with   regular   expressions,   otherwise   things   get   too   confusing   too   quickly   (and   regular   expressions   get   confusing   quickly   enough   all   by   themselves).  
       
      http://www.faqs.org/docs/diveintopython/dialect_re.html  

     

    以r为前缀的字符串中的内容是直接输出该内容,内容中是不带任何转义或控制字符的。

     

    个人对sorted as raw strings, not alphanumerics的理解为:

    以原始字符串整串字符来进行排序,而不单单是按单个字符或数字。因为原始字符串的比较层面是以整串字符来进行,比如说:如果两个串第一字母相同,就比较第二个字母ASCII的大小,还要,字符串中的内容也还包括了ASCII中的128个字符的比较,而不仅仅限于字符或数字。

     

    e.g :

    d5:cowmg4:eggs3:cow3:mooe

    解码后输出:

    cow => moo
    cowmg => eggs

     

     

    d5:+owmg4:eggs3:$ow3:mooe

    解码后输出:

    $ow => moo
    +owmg => eggs

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值